创建三台虚拟机主机名分别为node01、node02、node03
在node01虚拟机上生成密钥对
[root@node01 ~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): Created directory '/root/.ssh'. Enter passphrase (empty for no passphrase): Enter same passphrase again: Your identification has been saved in /root/.ssh/id_rsa. Your public key has been saved in /root/.ssh/id_rsa.pub. The key fingerprint is: b5:64:87:c6:c3:cc:2b:3e:a8:60:4b:03:d1:5d:c9:48 root@node01.linux .comThe key's randomart image is: +--[ RSA 2048]----+ | .Eo.. | | . ...o = . | |. . . % . | | . = = | |. S o | | . o . | | = . o | | o + . . | | . . | +-----------------+
然后将生成的公钥分别复制到node02、node03的虚拟机上(前提三台虚拟机能互相ping通)
[root@node01 ~]# scp -r /root/.ssh/ root@192.168.22.130:/root/.ss h/root@192.168.22.130's password: id_rsa 100% 1679 1.6KB/s 00:00 id_rsa.pub 100% 403 0.4KB/s 00:00 known_hosts 100% 352 0.3KB/s 00:00 authorized_keys 100% 403 0.4KB/s 00:00
[root@node01 ~]# scp -r /root/.ssh/ root@192.168.22.100:/root/.ss h/The authenticity of host '192.168.22.100 (192.168.22.100)' can't be established.ECDSA key fingerprint is a2:25:2d:c3:ef:90:24:5b:ab:e3:13:b6:0f:e a:a0:de.Are you sure you want to continue connecting (yes/no)? yes Warning: Permanently added '192.168.22.100' (ECDSA) to the list o f known hosts.root@192.168.22.100's password: id_rsa 100% 1679 1.6KB/s 00:00 id_rsa.pub 100% 403 0.4KB/s 00:00 known_hosts 100% 528 0.5KB/s 00:00 authorized_keys 100% 403 0.4KB/s 00:00
查看node02、node03虚拟机上的/root/.ssh/目录里是否有authorized_keys文件
[root@node02 ~]# ls /root/.ssh/
authorized_keys id_rsa id_rsa.pub known_hosts
[root@node03 ~]# ls /root/.ssh/
authorized_keys id_rsa id_rsa.pub known_hosts
验证登录
[root@node01 ~]# ssh root@192.168.22.130 Last failed login: Thu Jan 7 13:46:23 CST 2021 from 192.168.22.1 29 on ssh:nottyThere was 1 failed login attempt since the last successful login. Last login: Thu Jan 7 13:44:40 2021 from 192.168.22.1 [root@node02 ~]# exit 登出 Connection to 192.168.22.130 closed. [root@node01 ~]# ssh root@192.168.22.100 Last login: Thu Jan 7 00:35:18 2021 from 192.168.22.1
[root@node03 ~]#
[root@node02 ~]# ssh root@192.168.22.129 Last login: Thu Jan 7 13:38:41 2021 from 192.168.22.1 [root@node02 ~]# ssh root@192.168.22.100 Last login: Thu Jan 7 00:52:21 2021 from 192.168.22.129
[root@node03 ~]# ssh root@192.168.22.129 Last login: Thu Jan 7 13:52:40 2021 from 192.168.22.130 [root@node03 ~]# ssh root@192.168.22.130 Last login: Thu Jan 7 13:54:23 2021 from 192.168.22.129
这样就完成了三台虚拟机之间的ssh免密登录
古之立大事者,不惟有超世之才,亦必有坚忍不拔之志。