配置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免密码登录就正常了。

参考:
使用SSH遇到Authentication Refused: Bad Ownership or Modes for Directory

你可能感兴趣的:(服务器配置)