欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

使用 SSH 免密码登录时出现文件所有权或模式错误的解决方案

最编程 2024-03-20 15:25:56
...

记一次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 read/write for the user, and not accessible by others.

(图片可点击放大查看)

5、解决方法

chown -R root:root /root

(图片可点击放大查看)

6、验证问题是否解决

可以看到SSH免密登录恢复正常

(图片可点击放大查看)

推荐阅读