阿毛
It's me !
想你所想

ssh使用公钥连接失败

 ~/.ssh  ssh aliyun.beijing
root@aliyun.beijing: Permission denied (publickey,gssapi-keyex,gssapi-with-mic).

当我 ssh 我的主机时,报了如上的错误。错误很简略,不利于排查,这时可以带上`-v`参数再 ssh,会打出详细的 DEBUG 日志,经过和 ssh 其他主机成功的日志对比发现

异常的日志

debug1: Local version string SSH-2.0-OpenSSH_9.0
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1
......
debug1: Will attempt key: /Users/humh/.ssh/id_rsa RSA SHA256:xxxx explicit
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/humh/.ssh/id_rsa RSA SHA256:xxxx explicit
debug1: send_pubkey_test: no mutual signature algorithm
debug1: No more authentication methods to try.

成功的日志

debug1: Local version string SSH-2.0-OpenSSH_9.0
debug1: Remote protocol version 2.0, remote software version OpenSSH_8.0
......
debug1: Will attempt key: /Users/humh/.ssh/id_rsa RSA SHA256:xxxx explicit
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,gssapi-keyex,gssapi-with-mic
debug1: Next authentication method: publickey
debug1: Offering public key: /Users/humh/.ssh/id_rsa RSA SHA256:xxxx explicit
debug1: Server accepts key: /Users/humh/.ssh/id_rsa RSA SHA256:xxxx explicit
Authenticated to aliyun.beijing ([xx.xx.xx.xx]:22) using "publickey".

遇到 “send_pubkey_test: no mutual signature algorithm” 的错误通常意味着 SSH 客户端和服务器之间没有找到共同支持的签名算法。SSH 协议在进行公钥认证时,需要客户端和服务器都支持相同的签名算法。

这里我需要知道几点基础知识:

如何知道公钥文件的类型,如算法类型?
ssh-keygen -l -f ~/.ssh/id_rsa.pub

一个公钥是RSA算法生成的并且指纹使用SHA256计算的,但在连接时也可以使用其他算法吗?
当客户端提供一个使用 RSA 算法生成的公钥,并且该公钥的指纹使用 SHA256 计算时,这并不意味着连接时只能使用 ssh-rsa 算法。客户端和服务端之间的协商过程允许使用多种算法,包括公钥认证算法、加密算法等。即使客户端提供了一个使用 RSA 算法生成的公钥,它仍然可以提供其他类型的公钥供服务端选择。

尽管客户端提供的公钥是使用 RSA 算法生成的,但是最终使用的算法取决于客户端和服务器之间的协商结果。
具体来说:

客户端提供的算法:
客户端会向服务器提供一系列它支持的算法,包括公钥认证算法、加密算法等。
客户端通过发送这些算法列表来告知服务器它可以支持哪些算法。

服务器选择的算法:
服务器会从客户端提供的算法列表中选择它支持的算法。
服务器可能会根据自己的配置来进一步限制可用的算法,例如通过 /etc/ssh/sshd_config 文件中的 PubkeyAcceptedAlgorithms 选项来指定支持的公钥算法。

协商过程
客户端提供算法列表:
客户端向服务器发送一个算法列表,这些算法包括客户端支持的所有算法。

服务器选择算法:
服务器从客户端提供的算法列表中选择它支持的算法。
服务器可能会进一步过滤这个列表,以符合其自身的配置。

协商结果:
最终使用的算法是客户端和服务端共同支持的那个算法。

示例
假设客户端提供了以下算法列表:
ssh-rsa
ssh-ed25519
ecdsa-sha2-nistp256
如果服务器配置为:
PubkeyAcceptedAlgorithms ssh-rsa,ssh-ed25519
则最终协商的结果将是:
如果服务器支持 ssh-rsa 和 ssh-ed25519,则最终使用的算法可能是 ssh-rsa 或 ssh-ed25519,具体取决于服务器的优先级设置。
控制权
客户端:提供算法列表,但不能控制最终使用的算法。
服务器:从客户端提供的算法列表中选择算法,并根据自己的配置来进一步限制可用的算法。

总结
协商过程中可以使用的算法:是由客户端和服务端共同决定的。
控制权:服务器具有最终的决策权,因为它可以从客户端提供的算法列表中选择算法,并根据自己的配置来进一步限制可用的算法。
通过这种方式,客户端和服务端能够协商出双方都支持的最佳算法,从而确保连接的安全性和兼容性。

sshd -T 可以查询服务端支持哪些算法

humh

文章作者

站长本人,一个憨批!

发表回复

textsms
account_circle
email

想你所想

ssh使用公钥连接失败
~/.ssh  ssh aliyun.beijing root@aliyun.beijing: Permission denied (publickey,gssapi-keyex,gssapi-with-mic). 当我 ssh 我的主机时,报了如上的错误。错误很简略,不利于排查,…
扫描二维码继续阅读
2024-08-02