使用 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免密登录恢复正常
(图片可点击放大查看)
推荐阅读
-
紧急模式问题处理 - 图 1 紧急模式 根本原因分析 应急模式提供了尽可能小的环境,即使无法进入应急模式,也可以在其中修复系统。在应急模式下,系统只安装根文件系统供读取,不尝试安装任何其他本地文件系统,不激活网络接口,只启动一些基本服务。 进入应急模式的原因通常是 /etc/fstab 文件中存在错误,导致文件系统挂载失败。 文件系统中存在错误,导致。 约束和限制 本节适用于 Linux 操作系统紧急模式。程序涉及修复文件系统。修复文件系统有丢失数据的风险,因此请先备份数据,然后再执行修复操作。 处理方法 输入根密码,然后进入修复模式。 在应急模式下,根分区以只读模式挂载。要修改根目录中的文件,需要执行以下命令以读写模式重新挂载根分区。# mount -o rw,remount / 请执行以下命令首先检查 fstab 文件是否有误,然后尝试挂载所有未挂载的文件系统。# mount -a 如果挂载点不存在,请创建一个挂载点。 如果不存在此类设备,请注释或删除挂载行。 如果指定了不正确的挂载选项,请将挂载参数更改为正确的参数。 如果没有发生错误,但出现 UNEXPECTED INCONSISTENCY;RUN fsck MANUALLY 消息(通常是由文件系统错误引起的),请跳至第 7 步。 执行以下命令打开 /etc/fstab 以修改相应的错误。# vi /etc/fstab /etc/fstab 文件包含以下字段,以空格分隔:[文件系统] [dir] [type] [options] [dump] [fsck] 表 1 /etc/fstab 参数 说明 参数 说明 [文件系统] 要挂载的分区或存储设备。 文件系统]列建议以 UUID 的形式写入。执行 blkid 命令可查询设备文件系统 UUID。 参考格式如下: # <device> <dir> <type> <options> <dump> <fsck>; UUID=b411dc99-f0a0-4c87-9e05-184977be8539 /home ext4 defaults 0 2 使用 UUID 的好处是,它们与磁盘顺序无关。如果你在 BIOS 中更改了存储设备的顺序,或重新插入了存储设备,或者因为某些 BIOS 可能会随机更改存储设备的顺序,那么使用 UUID 会更有效率。 [文件系统] 文件系统]的挂载位置。 类型 挂载设备或分区的文件系统类型,支持多种不同的文件系统:ext2、ext3、ext4、reiserfs、xfs、jfs、smbfs、iso9660、vfat、ntfs、swap 和 auto。 设置为自动类型后,挂载命令会猜测所使用的文件系统类型,这对 CDROM 和 DVD 等移动设备非常有用。 选项 挂载时要使用的参数,有些参数是特定文件系统特有的。例如,默认值参数使用文件系统的默认挂载参数,ext4 的默认参数为:rw、suid、dev、exec、auto、nouser、async。 有关更多参数,请执行以下命令查看 man 手册:# man mount
-
使用 SSH 免密码登录时出现文件所有权或模式错误的解决方案