记一次SSH免密码登陆时出现bad ownership or modes for file报错的解决过程
问题场景前置条件
例如A服务器192.168.31.232远程到服务器B192.168.31.127,做了SSH免密登录
ssh-keygen -b 4096 ssh-copy-id root@192.168.31.127 ssh root@192.168.31.127
这时无需输入密码可以直接免密登录
1、但后来出现ssh登录仍要输入密码的情况
ssh root@192.168.31.127
2、在客户端ssh -v 命令未能找到原因
3、在SSH服务器端查看secure日志继续排查
可以看到ssh client登录时服务器端有如下报错日志
sshd[3039]: Authentication refused: bad ownership or modes for file /root/.ssh/authorized_keys
4、问题原因
ls -al /root/.ssh/authorized_keys
文件的属主变成了非root用户,导致该问题出现
man ssh查看该解释
~/.ssh/authorized_keys Lists the public keys (DSA, ECDSA, Ed25519, RSA) that can be used for logging in as this user. The format of this file is described in the sshd(8) manual page. This file is not highly sensitive, but the recommended permissions are
5、解决方法
chown -R root:root /root
6、验证问题是否解决
可以看到SSH免密登录恢复正常