1、实验环境中的3台服务器
host1:192.168.34.7
host2:192.168.34.17
host3:192.168.34.27
2、在分别在3台服务器上设置私钥和公钥
[root@localhost ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
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:
SHA256:f+O5f1071XEtKqNrZd6JjQABxJR9XDSkZIIqdXssNQ4 root@localhost.localdomain
The key's randomart image is:
+---[RSA 2048]----+
| ++=..o+= |
| . E =+o. . |
| . o * +. .|
|. . o = ..o|
| . o .S . .+|
| ..= . +|
| *.Bo. =|
| o +o+o oo|
| ... +o...|
+----[SHA256]-----+
3、分别将公钥copy到host1的/root/ssh/authorized_keys文件中
[root@localhost ~]# ssh-copy-id 192.168.34.7
/usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/root/.ssh/id_rsa.pub"
The authenticity of host '192.168.34.7 (192.168.34.7)' can't be established.
ECDSA key fingerprint is SHA256:zY3dBJSXwTi+9yOOIvsqR/31Ze3jOJ9Kcau7lLqtRsQ.
ECDSA key fingerprint is MD5:6a:b0:5d:39:c0:d8:7e:20:a7:4b:9c:77:1e:cf:d9:00.
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 any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@192.168.34.7's password:
Number of key(s) added: 1
Now try logging into the machine, with: "ssh '192.168.34.7'"
and check to make sure that only the key(s) you wanted were added.
4、将host1的/root/ssh/authorized_keys文件copy到其他几台服务器上。
[root@localhost ~]# scp /root/.ssh/authorized_keys 192.168.34.27:/root/.ssh/
authorized_keys 100% 1224 874.9KB/s 00:00
[root@localhost ~]# scp /root/.ssh/authorized_keys 192.168.34.17:/root/.ssh/
authorized_keys 100% 1224 667.0KB/s 00:00