Aug 17

记一次sshd异常:无法通过建立信任关系登录 不指定

felix021 @ 2012-8-17 23:52 [IT » 软件] 评论(4) , 引用(0) , 阅读(28859) | Via 本站原创 | |
对ssh用得比较多的同学应该知道通过建立信任关系来免除输入密码的麻烦:

在A机器上执行:

$ ssh-keygen -t rsa (各种回车)
$ ssh-copy-id -i ~/.ssh/id_rsa.pub USER@B_ip

然后在A机器上 ssh USER@B_ip 就可以免密码使用USER用户登录B机器了。

实际上第二步操作是将 A 机器该用户的公钥(id_rsa.pub)追加到B机器的 ~/.ssh/authorized_keys 文件末尾中去。

当A机器访问B时,如果B机器的sshd能够在/home/USER/.ssh/authorized_keys中找到对应的公钥,就认为A机器具有B机器的USER用户访问权限,于是就直接让A机器以USER身份登录。

但是上周在线上某台机器进行操作时却发现这一机制失效了。通过该机制,A=>B可登录,但是B=>A失败,甚至A=>A也失败(B=>B却成功)。虽然问题很奇怪,但说明问题出在A机器上。

首先是 diff 了A、B两台机器的 /etc/ssh ,发现完全相同,所以不是配置的问题。

然后查看 ssh -vv localhost
引用
....
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/felix021/.ssh/id_rsa
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/felix021/.ssh/id_dsa
debug1: Trying private key: /home/felix021/.ssh/id_ecdsa
debug2: we did not send a packet, disable method
debug1: Next authentication method: password

而在B机器上,we sent a public key packet, wait for reply 之后则是紧跟着"debug1: Server accepts key: pkalg ssh-rsa blen 279"。由此可以看出,是A机器的sshd不认可publickey。

至于为什么不认可,在google上查了许多,毫无头绪,直到使用类似“ssh publickey ignore debug diagnose”这样的关键词,发现这个页面,其中的第二条和第六条给出了解答:
引用
2. Debugging on the remote host by running sshd in debug mode: Run ‘/usr/sbin/sshd -d -p 2222′ on the remote host and connect to it. ’2222′ here is the port number of the sshd process you started on the remote host.

6. Check the permissions on your home directory, .ssh directory, and the authorized_keys file: If your ssh server is running with ‘StrictModes on’, it will refuse to use your public keys in the ~/.ssh/authorized_keys file. Your home directory should be writable only by you, ~/.ssh should be 700, and authorized_keys should be 600.


通过执行 /usr/sbin/sshd -d -p 2222 (在2222端口启动一个带debug输出的sshd) ,然后 ssh -vv localhost -p 2222 ,可以看到sshd输出了一行
引用
Authentication refused: bad ownership or modes for directory /home/felix021

正好与那第六条相对应,再检查一下 /home/felix021 ,其权限是其他组可写。

最终解决方案:将用户home目录的权限改为0755,登录成功。



欢迎扫码关注:




转载请注明出自 ,如是转载文则注明原出处,谢谢:)
RSS订阅地址: https://www.felix021.com/blog/feed.php
hardy
2018-3-14 12:36
赞.很有用,解决了我的问题
6
2017-9-5 16:53
666
test1231231
2016-8-9 11:05
赞!
czyhd Email Homepage
2012-8-18 00:50
生产上也有机器是这样的,被坑了。。
分页: 1/1 第一页 1 最后页
发表评论
表情
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
emotemotemotemotemot
打开HTML
打开UBB
打开表情
隐藏
记住我
昵称   密码   *非必须
网址   电邮   [注册]