參考文章:https://www.cnblogs.com/xiaoaofengyue/p/8080639.html
一、在/etc/hosts文件中配置需要ssh免密登錄的主機:
[root@worker1-presto .ssh]# cat /etc/hosts
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.152.129 coordinate
192.168.152.130 worker1
192.168.152.131 worker2
192.168.152.132 worker3
二、在主機中生成ssh祕鑰對
安裝ssh命令:
yum install -y openssh-server openssh-clients
啓動ssh:
service sshd start 或 /etc/init.d/sshd start
配置開機啓動:
chkconfig --level 2345 sshd on
-------------------------------------------------------------------------------
生成RSA祕鑰對命令:
1、ssh-keygen -t rsa
2、然後一直回車
3、進入個人目錄;
4、進入.ssh目錄中
[root@worker1-presto ~]# ssh-keygen -t rsa
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:J8d1UhDYTXCKIeRsH9KaNYB6Ia0qL0R/YbPH+Ea7q6Y root@worker1-presto
The key's randomart image is:
+---[RSA 2048]----+
| . o+ .o+*+ |
| . +o +.o.+. |
| + .= * + . |
| . o+.. B + o |
|. ....* S = |
|.... + + + |
|.o . + . |
|. . . + |
| . Eo.ooo |
+----[SHA256]-----+
[root@worker1-presto ~]# cd /root/.ssh/
[root@worker1-presto .ssh]# ll
總用量 12
-rw-------. 1 root root 1675 10月 17 10:07 id_rsa
-rw-r--r--. 1 root root 401 10月 17 10:07 id_rsa.pub
-rw-r--r--. 1 root root 342 10月 15 10:12 known_hosts
-----------------------------------
如果希望ssh公鑰生效需滿足至少下面兩個條件:
1) .ssh目錄的權限必須是700
2) .ssh/authorized_keys文件權限必須是600
三、相互拷貝公鑰到需要免密登錄的主機中,比如coordinate需要免密登錄worker1、worker2、workder3中,則將coordinate中的公鑰拷貝到worker1/2/3中,命令如下:
scp -p ~/.ssh/id_rsa.pub root@worker1:/root/.ssh/authorized_keys
scp -p ~/.ssh/id_rsa.pub root@worker2:/root/.ssh/authorized_keys
scp -p ~/.ssh/id_rsa.pub root@worker3:/root/.ssh/authorized_keys
檢查效果:
ssh worker1
exit
ssh worker2
exit
ssh worker3
exit