Debugging SSH public key authentication problems-关于ssh公钥认证问题的排查

原文转自 https://blog.codefront.net/2007/02/28/debugging-ssh-public-key-authentication-problems/

虽然最终实现ssh免密登录了,当经历的挣扎却超出想象,所以觉得有必要通过blog来提醒自己不要犯同样的错误。下面是你在面对排查ssh公钥认证问题的痛苦时可以做的:

  • 当通过客户端连接时获取更多的debug信息:通过给ssh命令增加 ‘-v' 参数(eg: ssh chuyeow@remotehost -v -v -v或者 ssh -vv chuyeow@remotehost). -v 参数增加越多,可以获得越详细的调试信息
  • 通过让sshd以debug模式运行在目标机器上调试:在目标机器上执行“/usr/sbin/sshd -d -p 2222" 并另开一个目标机终端做ssh连接“ssh -vv localhost 2222". 
  • tail 命令查看ssh登录的鉴权日志,在目标机上执行 'tail -f /var/log/auth.log'(我在centos7上是/var/log/secure) ,然后在请求机器上做ssh连接,可以输出相关的日志
  • Make sure your ssh key agent is running: Do a ‘ps aux|grep ssh-agent’. Make sure your key agent is running. If you’re not using ssh-agent (I like keychain from Gentoo, or SSHKeyChain for Mac OS X), do whatever you have to do to ensure that your keychain is running.
  • Make sure your private key is added to the ssh key agent: Do a ‘ssh-add -l’ to check that ssh-agent has your key. Likewise, if you are using something else, check your keychain application has your private key.
  • 检查home目录,.ssh目录,authorized_keys的权限:如果sshd以‘StrictModes on’模式运行(在/etc/ssh/sshd_config文件里配置,默认是注释掉的), 目标机器会拒绝使用public keys(在 ~/.ssh/authorized_files文件中). 相关目录的权限一般为700,文件的权限一般为600(只要group,others 用户没有w权限就可以的)
通过tail目标机器的鉴权日志(/var/log/secure)对我本次解决问题有关键性作用-我的问题就是用户家目录在同组用户(g)权限的设置上除了问题(我从auth.log or secure  获取的错误信息是: ‘Authentication refused: bad ownership or modes for directory /home/chuyeow’). chmod g-w  /home/chuyeow就好了。当然也可以通过设置“StrictModes off" (在/etc/ssh/sshd_config)来解决,但并不推荐这样做,而且你也不一定有root权限来修改这个文件

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章