Linux设置ssh免密登录

目录

1.在/root目录下输入命令

2.进入.ssh目录

3.将公钥id_rsa.pub写入到一个认证文件夹中

4.开启远程免密登录配置

5.免密远程登录本机


1.在/root目录下输入命令

[root@localhost ~]# ssh-keygen -t rsa -P ""
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):

回车,生成.ssh文件,里面有秘钥

[root@localhost ~]# ll -a

Linux设置ssh免密登录_第1张图片

2.进入.ssh目录

[root@localhost ~]# cd .ssh/
[root@localhost .ssh]# ll
total 8
-rw-------. 1 root root 1675 Feb  1 16:39 id_rsa
-rw-r--r--. 1 root root  408 Feb  1 16:39 id_rsa.pub

id_rsa是私钥;id_rsa.pub是公钥

3.将公钥id_rsa.pub写入到一个认证文件夹中

[root@localhost .ssh]# cat ./id_rsa.pub >> authorized_keys
[root@localhost .ssh]# ls
authorized_keys  id_rsa  id_rsa.pub

4.开启远程免密登录配置

[root@localhost .ssh]# ssh-copy-id -i ./id_rsa.pub -p22 [email protected]
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "./id_rsa.pub"
The authenticity of host '192.168.180.148 (192.168.180.148)' can't be established.
ECDSA key fingerprint is SHA256:iB/yQiRE36xUpEMQe6DJsBtVIATFRUxNu3N0pUw9YvY.
ECDSA key fingerprint is MD5:e9:be:0d:3f:91:ad:d4:ea:7e:fc:9d:c6:6e:74:93:e2.
Are you sure you want to continue connecting (yes/no)? yes
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out

/usr/bin/ssh-copy-id: WARNING: All keys were skipped because they already exist on 
		(if you think this is a mistake, you may want to use -f option)

5.免密远程登录本机

[root@localhost .ssh]# ssh -p22 [email protected]
Last login: Wed Feb  1 16:29:09 2023 from 192.168.180.1

备注:如果其他虚拟机也进行了同样的设置,也可以远程免密登录到其他虚拟机上,如果其他虚拟机没有设置远程免密登录,输入上条命令后还要输入密码:

[root@localhost .ssh]# ssh -p22 [email protected]
The authenticity of host '192.168.180.141 (192.168.180.141)' can't be established.
ECDSA key fingerprint is SHA256:FCfpjPPSJxiNAqFuA88T/g56iWS1XUijN6htNp2DC4M.
ECDSA key fingerprint is MD5:ca:88:88:58:45:48:d8:d9:08:6a:b8:69:78:d1:59:ae.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.180.141' (ECDSA) to the list of known hosts.
[email protected]'s password: 
Last login: Wed Feb  1 14:36:34 2023 from 192.168.180.147

输入ip addr命令可以检测成功登录另一台虚拟机 

[root@localhost ~]# ip addr

Linux设置ssh免密登录_第2张图片

6.远程无法免密登录解决

删除.ssh下的所有文件,然后重复上述操作

[root@hadoop104 .ssh]# rm -rf ./*

你可能感兴趣的:(Linux,linux,ssh,服务器)