CentOS系统SSH免密后依然需要输入密码(已解决)

1、问题

通过ssh-keygen -t rsa和ssh-copy-id -i node1操作后,免密登录依然需要输入密码。

[root@node1 ~]# ssh node1
The authenticity of host 'node1 (192.168.1.160)' can't be established.
ECDSA key fingerprint is SHA256:CzXPsWonK4Fxx8mQpRHXL1K8R8eqyQr+Zn95HGS8zQ4.
ECDSA key fingerprint is MD5:61:95:b5:57:d3:bb:d0:9d:bf:9c:e1:d4:40:4f:07:d6.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'node1,192.168.1.160' (ECDSA) to the list of known hosts.
root@node1's password: 

2、原因与解决办法

一般来讲失败的原因有两个:目录文件的权限和目录的属主。
(1)目录文件的权限
.ssh父目录的权限是755(我的是/root),.ssh目录权限是700,authorized_keys文件 600

[root@node1 ~]# chmod 755 /root
[root@node1 ~]# chmod 700 .ssh
[root@node1 ~]# chmod 600 .ssh/authorized_keys

(2)目录的属主
如果上面方法还没有解决问题,那可能是.ssh父目录的属主存在问题。

[root@node1 ~]# ls -al .ssh
total 12
drwx------   2 root root    80 Nov  3 10:07 .
drwxr-xr-x. 11 hdfs users 4096 Nov  3 09:38 ..
-rw-------   1 root root     0 Nov  3 10:06 authorized_keys
-rw-------   1 root root  1679 Nov  3 09:38 id_rsa
-rw-r--r--   1 root root   390 Nov  3 09:38 id_rsa.pub
-rw-r--r--   1 root root     0 Nov  3 10:07 known_hosts

这里发现.ssh父目录(..)的属主存在问题。

修改如下

[root@node1 ~]# chown root:root /root
[root@node1 ~]# ls -al .ssh
total 16
drwx------   2 root root   80 Nov  3 10:07 .
drwxr-xr-x. 11 root root 4096 Nov  3 09:38 ..
-rw-------   1 root root 1135 Nov  3 10:11 authorized_keys
-rw-------   1 root root 1679 Nov  3 09:38 id_rsa
-rw-r--r--   1 root root  390 Nov  3 09:38 id_rsa.pub
-rw-r--r--   1 root root    0 Nov  3 10:07 known_hosts
[root@node1 ~]# 

3、验证

[root@node2 ~]# ssh-copy-id -i node1
/usr/bin/ssh-copy-id: INFO: attempting to log in with the new key(s), to filter out any that are already installed
/usr/bin/ssh-copy-id: INFO: 1 key(s) remain to be installed -- if you are prompted now it is to install the new keys
root@node1's password: 

Number of key(s) added: 1

Now try logging into the machine, with:   "ssh 'node1'"
and check to make sure that only the key(s) you wanted were added.

[root@node2 ~]# ssh node1
Last login: Fri Nov  3 10:04:03 2017 from hadron
[root@node1 ~]# 

你可能感兴趣的:(Linux)