linux建立用户后无法ssh,解决ssh添加用户公钥到~/.ssh/authorized_keys文件后任然无法无密码登录的问题...

最近一开发同事找我帮忙,说他在一台服务器上添加了自己电脑的ssh公钥,但是登录服务器的时候仍然要求输入密码。刚开始我以为是他添加ssh公钥的时候粘贴有问题,比如,ssh公钥本来应该是一行,结果粘贴成了多行,或者是粘贴出现了其他问题。经过我重新添加一次公钥后,仍然让输入密码,我就开始怀疑是~/.ssh/authorized_keys文件权限的问题。再通过/var/log/secure查看日志,提示authorized_keys的权限不是600。然后我再查看authorized_keys这个文件的权限被修改成了664,再查看/etc/ssh/sshd_config文件中设置有StrictModes yes 难怪添加了公钥仍然被要求输入密码。这里提一点,如果自己能够看懂英文说明的话,出了问题,可以首先看看相关服务的man手册,软件自身的说明文档才是最权威,最原始的,其他的资料都只是参考而已,参杂着各种片面的理解。

通过man ssh查看ssh相关的说明

The file ~/.ssh/authorized_keys lists the public keys that are permitted for logging in. When the userlogs in, the ssh program tells the server which key pair it would like to use for authentication.  Theclient proves that it has access to the private key and the server checks that the corresponding publickey is authorized to accept the account.

~/.ssh/authorized_keys

Lists the public keys (RSA/DSA) that can be used for logging in as this user.  The format ofthis file is described in the sshd(8) manual page.  This file is not highly sensitive, but therecommended permissions are read/write for the user, and not accessible by others。If this file, the ~/.ssh directory, or the user’s home directory are writable by other users,then the file could be modified or replaced by unauthorized users.  In this case, sshd will notallow it to be used unless the StrictModes option has been set to “no”.

查看man sshd_config

StrictModes

Specifies whether sshd(8) should check file modes and ownership of the user’s files and homedirectory before accepting login.  This is normally desirable because novices sometimes accidentally leave their directory or files world-writable.  The default is “yes”.

通过man手册可以知道系统默认~/.ssh/authorized_keys这个文件的权限应该是owner具有读写权限,其他的都不能读写,即权限应该是600.如果在/etc/ssh/sshd_config 中设置了StrictModes yes则sshd会去检查~/.ssh/authorized_keys这个文件的文件权限。如果~/.ssh目录和owner的home目录可以被其他用户写,那也会出现问题。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值