记录一下有关 Linux下SSH免密码登录配置步骤

环境准备

node1:192.168.0.103

node2:192.168.0.102

1. 关闭防火墙

查看防火状态

systemctl status firewalld
service iptables status

暂时关闭防火墙

systemctl stop firewalld
service iptables stop

永久关闭防火墙

systemctl disable firewalld
chkconfig iptables off

重启防火墙

systemctl enable firewalld
service iptables restart

2. 关闭SELinux

永久生效

修改 /etc/selinux/config 文件中的 SELINUX=enforcing 修改为 SELINUX=disabled ,然后重启

临时生效

 setenforce 0

3. 分别在两台机器上生成私钥/公钥对

[root@localhost ~]# ssh-keygen -t rsa

4. 将node1主机下的id_rsa.pub复制到node2主机下

[root@localhost ~]$ scp .ssh/id_rsa.pub root@192.168.0.102:~

此时还没有免密码登录,所以要输入node2的密码

5. node2主机须将复制过来的id_rsa.pub文件中的内容添加到/home/user/.ssh/authorzied_keys文件中

[root@localhost ~]$ cat id_rsa.pub >> .ssh/authorized_keys
[root@localhost ~]$ chmod 600 .ssh/authorized_keys

说明:第4、5步可以用一个命令搞定:ssh-copy-id 192.168.0.102

6. 验证是否可以免密码登录

[root@localhost ~]$ ssh 192.168.0.102

7. node2 登录node1 在nod2主机上重复上面4,5步骤