如何建立SSH互信:假设有二台主机135,136
1 关闭防火墙和SELinux
-
关闭防火墙
[root@localhost ~]# serviceiptables stop
该命令可以关闭防火墙,但是当重启后,防火墙会重新开启,输入下面的命令,让防火墙在重启后也不会开启。
[root@localhost ~]#chkconfig--level 35 iptables off
-
关闭SELINUX
[root@localhost ~]#vim/etc/selinux/config
编辑,令SELINUX=disabled。保存退出。
立即生效。
分别在135,136上执行上述命令。
2 修改SSH配置文件
-
打开配置文件
[root@localhost ~]#vim /etc/ssh/sshd_config
-
修改配置文件
找到下列行 去掉注释井号#
RSAAuthentication yes //
字面意思..允许RSA认证
PubkeyAuthentication yes //
允许公钥认证
AuthorizedKeysFile .ssh/authorized_keys //
公钥存放在.ssh/au..文件中
保存退出。
-
修改后需要重启ssh
[root@localhost ~]#/etc/init.d/sshd restart
分别在135,136上执行上述命令。
3 生成密码对
-
生成密码对
[root@localhost ~]# ssh-keygen -t rsa
直接回车几次,可在默认路径~/.ssh/下生成私钥id_rsa公钥id_rsa.pub。
分别在135,136上执行上述命令
4 生成authorized_keys
-
将136的公钥传到135上。
-
在136上输入
[root@localhost~]# scp /root/.ssh/id_rsa.pub root@10.40.0.135:/root/.ssh/id_rsa.pub.136
以上命令目前还需要输入目标机用户密码。
-
在135上输入
[root@localhost ~]#cd ~/.ssh/
[root@localhost .ssh]#ls
查看idrsa.pub.136是否已经传输过来。
[root@localhost .ssh]## catid_rsa.pub >> authorized_keys
[root@localhost .ssh]## catid_rsa.pub.136 >> authorized_keys
-
生成authorized_keys。
-
给authorized_keys修改权限
[root@localhost .ssh]#chmod 644authorized_keys
-
利用scp把该文件传送到136的.ssh/下
[root@localhost .ssh]# scp/root/.ssh/authorized_keys root@10.40.0.136:/root/.ssh/
5 验证
-
测试在135下输入
[root@localhost .ssh]# ssh10.40.0.136
-
测试在136下输入
[root@localhost ~]# ssh10.40.0.135
应该都不需要密码。 这二个互联都应该不需要密码。