1. 程式人生 > >配置ssh公鑰後免密碼ssh登入失敗

配置ssh公鑰後免密碼ssh登入失敗

在公司的伺服器上配置過ssh公鑰,之前一直可以免密碼登入。但是最近登入時,每次都提示要輸入密碼。嘗試了刪除known_hosts,重新把id_rsa.pub新增到伺服器~/.ssh/authorized_keys下也沒有用。
通過檢視系統的登入驗證日誌檔案/var/log/auth.log,看到每次登入時有以下報錯:

Mar  7 11:52:10 coffeeserver sshd[6761]: Authentication refused: bad ownership or modes for directory /home/coffee/.ssh

原來是.ssh目錄的許可權設定不正確。
網上查了一下資料:

SSH doesn’t like it if your home or ~/.ssh directories have group write permissions. Your home directory should be writable only by you, ~/.ssh should be 700, and authorized_keys should be 600
You can also get around this by adding StrictModes off to your ssh_config file, but I’d advise against it - fixing permissions is the way to go.

執行一下:

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

這時ssh免密碼登入就正常了。