*注意:本文以root用户为例进行设置
现在有两台CentOS6.5,hostname分别为hadoop001(IP:192.168.226.138)和hadoop002(IP:192.168.226.128)
1、生成ssh的公钥和私钥(两台机器都要操作),如果已经生成过就不需要再次生成
ssh-keygen -t rsa -P '' -f ~/.ssh/id_rsa
2、将hadoop001的公钥传给hadoop002(此操作在hadoop001中进行)
scp ~/.ssh/id_rsa.pub 192.168.226.128:~/.ssh/id_rsa.pub.hadoop001
3、将hadoop001的公钥添加到hadoop002的~/.ssh/authorized_keys文件中
(此操作在hadoop002中进行)
cat ~/.ssh/id_rsa.pub.hadoop001 >> ~/.ssh/authorized_keys
4、将hadoop002的公钥传给hadoop001
(此操作在hadoop002中进行)
scp ~/.ssh/id_rsa.pub 192.168.226.138:~/.ssh/id_rsa.pub.hadoop002
5、将hadoop002的公钥添加到hadoop001的~/.ssh/authorized_keys文件中
(此操作在hadoop001中进行)
cat ~/.ssh/id_rsa.pub.hadoop002 >> ~/.ssh/authorized_keys
6、配置完成并检查
在hadoop001中sshhadoop002,此时已经不用再输入密码
[root@hadoop001 ~]# ssh 192.168.226.128
Last login: Sat May 19 06:54:39 2018 from 192.168.226.138
[root@hadoop002 ~]#
在hadoop002中sshhadoop001,也已经不用再输入密码
[root@hadoop002 ~]# ssh 192.168.226.138
Last login: Sat May 19 06:54:39 2018 from 192.168.226.128
[root@hadoop001 ~]#
如果是非root用户进行ssh免密配置,将各自的公钥添加到对应用户的~/.ssh/authorized_keys文件中即可,不过非root用户需要修改~/.ssh/authorized_keys文件的权限
chmod 0600 ~/.ssh/authorized_keys