记一次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
这时无需输入密码可以直接免密登录
(图片可点击放大查看)
(图片可点击放大查看)
ssh root@192.168.31.127
(图片可点击放大查看)
(图片可点击放大查看)
可以看到ssh client登录时服务器端有如下报错日志
sshd[3039]: Authentication refused: bad ownership or modes for file /root/.ssh/authorized_keys
(图片可点击放大查看)
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 read/write for the user, and not accessible by others.
(图片可点击放大查看)
chown -R root:root /root
(图片可点击放大查看)
可以看到SSH免密登录恢复正常
(图片可点击放大查看)