无法使用 SSH-RSA 密钥登录

无法使用 SSH-RSA 密钥登录

这篇文章是在回答这个问题:身份验证被拒绝:文件 /var/git/.ssh/authorized_keys 的所有权或模式错误

那里暴露的问题已经解决(关于文件.ssh夹的文件模式)。

但另一个问题仍然存在,所以我创建了一个新问题:

当我尝试登录(使用详细选项)时,一切似乎都工作正常,但最后,发生了以下情况:

debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/remi/.ssh/id_rsa
debug2: we sent a publickey packet, wait for reply
debug1: Authentications that can continue: publickey,password
debug1: Trying private key: /home/remi/.ssh/id_dsa
debug1: Trying private key: /home/remi/.ssh/id_ecdsa
debug1: Trying private key: /home/remi/.ssh/id_ed25519
debug2: we did not send a packet, disable method
debug1: Next authentication method: password

我不明白,因为这些行对我来说似乎毫无意义:

  • we sent a publickey packet, wait for reply
  • we did not send a packet, disable method

答案1

如果用户的文件模式,您将得到此行为目标主机上的目录设置不正确。需要正确设置的不仅仅是 .ssh 目录的模式!

ssh 到主机并输入密码登录,然后

chmod 755 ~
logout

然后再次 ssh 并假设您已正确设置其他所有内容(请参阅其他答案),您应该能够登录。

这就是主目录完全打开时的样子 (777)。请注意,它不会尝试 rsa 密钥:

ssh -v user@host
...
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/iwoolf/.ssh/id_rsa
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic,password
debug1: Trying private key: /home/iwoolf/.ssh/id_dsa
debug1: Trying private key: /home/iwoolf/.ssh/id_ecdsa
debug1: Trying private key: /home/iwoolf/.ssh/id_ed25519
debug1: Next authentication method: password
...

然后正确设置主目录权限(755):

ssh -v user@host
...
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/iwoolf/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 279
debug1: Authentication succeeded (publickey).

答案2

我遇到了类似的问题,当将 StrictModes 从 yes 更改为 no 时解决了。打开/etc/ssh/sshd_config并添加

StrictModes no

答案3

在 Ubuntu 22.04 中(使用 RSA 密钥而不是默认用户“ubuntu”的 ED25519 密钥在 AWS EC2 上运行此程序)我收到相同的错误: Permission denied (publickey).

原因是 RSA-SHA1 算法很快就被弃用了。

所以我把这个放进去/var/log/auth.log: userauth_pubkey: key type ssh-rsa not in PubkeyAcceptedAlgorithms [preauth]

上面的同一链接中提到了该问题的解决方案。

HostKeyAlgorithms +ssh-rsa
PubkeyAcceptedKeyTypes +ssh-rsa

/etc/ssh/sshd_config文件中,后跟一个systemctl restart ssh.service

如果您使用 Ubuntu 22.04 作为具有 RSA-SHA1 密钥的服务器的 SSH 客户端,则必须在文件中使用相同的 2 行~/.ssh/config。如果您必须在各个版本中使用相同的密钥集,则可以在两侧的任何地方启用这两行,具体取决于 SSH 的用途。这是一种解决方法,并且在技术上不安全,因此应将其视为过渡的权宜之计,而不是永久解决方案。我想随着时间的推移,您应该开始更新/升级您的系统并转向更强的密钥,例如 ED25519 密钥。

答案4

服务器是否配置为接受 RSA 密钥?

确保RSAAuthentication yessshd_config文件中。

相关内容