1、在集群的其中一台机器中进入根目录
[root@vm001 ~]# cd
2、执行ssh-keygen -t rsa命令生成密钥,执行后直接回车回车回车
[root@vm001 ~]# ssh-keygen -t rsa
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:
0c:fa:a1:32:50:b7:ac:23:00:97:cf:26:e8:a3:4b:e6 root@vm001
The key's randomart image is:
+--[ RSA 2048]----+
| |
| . |
|. + . . |
|.+ = o o |
|+ . B . S |
|o. + o . |
|.== . . |
|=..+ |
|oE |
+-----------------+
3、之后进入ssh目录
[root@vm001 ~]# cd /root/.ssh/
4、拷贝密钥到authorized_keys,到此本机的免密登录就可以了
[root@vm001 ~]# cp id_rsa.pub authorized_keys
[root@vm001 ~]# ssh vm001
Last login: Sun Jul 14 06:47:21 2019 from vm003
[root@vm001 ~]#
5、接下来将密钥拷贝到其他几台虚拟机中,拷贝时需要输入密码
[root@vm001 ~]# ssh-copy-id -i vm002
root@vm002's password:
Now try logging into the machine, with "ssh 'vm002'", and check in:
.ssh/authorized_keys
to make sure we haven't added extra keys that you weren't expecting.
[root@vm001 ~]#
6、将其他几台服务的密钥先拷贝到其中一台中,这样这一台服务器就有其他几台服务器的密钥,可以免密登录其他几台服务器,然后我们在这台服务器上执行【scp authorized_keys hostname:/root/.ssh】,将密钥拷贝到其他几台服务器中,这样这几台服务器就可以互相免密登录了
[root@vm001 ~]# scp authorized_keys vm002:/root/.ssh