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權限來修改這個文件

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