无法使 SSH 公钥身份验证正常工作

45 ssh

我的服务器运行 CentOS 5.3。我在运行 Leopard 的 Mac 上。我不知道谁对此负责:

我可以通过密码验证登录到我的服务器。我已经完成了设置 PKA 的所有步骤(如http://www.centos.org/docs/5/html/Deployment_Guide-en-US/s1-ssh-beyondshell.html 所述),但是当我使用 SSH,它甚至拒绝尝试公钥验证。使用命令

ssh -vvv user@host
Run Code Online (Sandbox Code Playgroud)

(其中 -vvv 将详细程度提高到最大级别)我得到以下相关输出:

debug2: key: /Users/me/.ssh/id_dsa (0x123456)
debug1: Authentications that can continue: publickey,gssapi-with-mic,password
debug3: start over, passed a different list publickey,gssapi-with-mic,password
debug3: preferred keyboard-interactive,password
debug3: authmethod_lookup password
debug3: remaining preferred: ,password
debug3: authmethod_is_enabled password
debug1: Next authentication method: password
Run Code Online (Sandbox Code Playgroud)

然后提示我输入密码。如果我试图用

ssh -vvv -o PreferredAuthentications=publickey user@host
Run Code Online (Sandbox Code Playgroud)

我得到

debug2: key: /Users/me/.ssh/id_dsa (0x123456)
debug1: Authentications that can continue: publickey,gssapi-with-mic,password
debug3: start over, passed a different list publickey,gssapi-with-mic,password
debug3: preferred publickey
debug3: authmethod_lookup publickey
debug3: No more authentication methods to try.
Run Code Online (Sandbox Code Playgroud)

所以,即使服务器说它接受公钥认证方法,而且我的 SSH 客户端坚持使用它,我还是被反驳了。(注意上面明显没有“提供公钥:”行。)有什么建议吗?

小智 49

检查您的 Centos 机器是否具有:

RSAAuthentication yes
PubkeyAuthentication yes
Run Code Online (Sandbox Code Playgroud)

在 sshd_config 中

并确保您对 centos 机器的 ~/.ssh/ 目录具有适当的权限。

chmod 700 ~/.ssh/
chmod 600 ~/.ssh/*
Run Code Online (Sandbox Code Playgroud)

应该做的伎俩。

  • 如果还不是这样,您可能还需要 `chmod go-w ~/`。 (7认同)
  • 文件 **authorized_keys** 的权限是非常重要的提示。谢谢。 (4认同)

小智 18

我遇到了类似的问题 - 远程 PC 无法使用公钥身份验证登录 CentOs 6 服务器。在我的情况下,问题与 SELinux 相关 - 尝试登录的用户的主目录具有安全上下文消息。我通过使用该restorecon工具解决了这个问题:

restorecon -Rv /home
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,加雷斯!“restorecon -Rv /root/.ssh”很好地解决了这个问题。 (2认同)

Jin*_*Liu 16

1- 检查你的 /etc/ssh/sshd_config,确保你有

RSAAuthentication 是
PubkeyAuthentication 是

2- 从远程机器检查安全日志,查找详细的 sshd 守护程序错误日志。例如在我的 Ubuntu 中

# grep 'sshd' /var/log/secure | grep '身份验证被拒绝' | 尾-5
8 月 4 日 06:20:22 xxx sshd[16860]:身份验证被拒绝:目录 /home/xxx 的所有权或模式不正确
8 月 4 日 06:20:22 xxx sshd[16860]:身份验证被拒绝:目录 /home/xxx 的所有权或模式不正确
8 月 4 日 06:21:21 xxx sshd[17028]:身份验证被拒绝:目录 /home/xxx 的所有权或模式不正确
8 月 4 日 06:21:21 xxx sshd[17028]:身份验证被拒绝:目录 /home/xxx 的所有权或模式不正确
8 月 4 日 06:27:39 xxx sshd[20362]:身份验证被拒绝:目录 /home/xxx 的所有权或模式不正确

然后检查目录/home/xxx的所有权和模式,也许你需要运行这个

chmod 755 /家/xxx

  • 检查系统日志文件是一个非常重要的提示。 (2认同)
  • 755 的主目录 perms 帮助了我 - 绝对需要! (2认同)

Dan*_*son 11

仔细检查本地和远程机器的权限是否正确以及文件结构(特别是拼写)是否正确。您引用的 URL 说明了所有内容,但值得检查您所拥有的内容是否匹配。通常,权限会抛出相关错误。

您是否检查过 CentOS 5.3 机器上的 sshd_config 设置为允许 PubkeyAuthentication 或 RSAAuthentication ?

检查 CentOS 系统上的 SSH 服务器日志 - 它可能提供更多信息。我不确定 CentOS 是否会执行 debian 所做的列入黑名单的 ssh 密钥检查,但我已经看到 ssh 公钥拒绝就 -vvv 输出而言相对安静,但日志非常清楚地解释了发生了什么


小智 8

知道了!原来这是一个客户端问题。(我认为任何服务器端问题都会产生更有用的调试输出。)出于我不知道的原因,在我的 Mac 上,文件 /etc/ssh_config 有一行

PubkeyAuthentication = no
Run Code Online (Sandbox Code Playgroud)

我注释掉了这一行,现在一切正常。