修复 ssh 的 rsa 身份认证


ssh-rsa 身份认证失败”no matching host key type found. Their offer: ssh-rsa”

某日,我笔记本的 Debian 进行了大版本升级,然后当我尝试 ssh 到服务器时提示我身份认证失败,错误信息为:
no matching host key type found. Their offer: ssh-rsa

原来,从 openssh 8.8开始,默认禁用了使用 SHA-1 算法的 RSA 密钥。

1
2
3
4
5
6
7
8
9
10
11
12
13
OpenSSH 8.8 was released on 2021-09-26. It is available from the
mirrors listed at https://www.openssh.com/.

...
...

Potentially-incompatible changes
================================

This release disables RSA signatures using the SHA-1 hash algorithm
by default. This change has been made as the SHA-1 hash algorithm is
cryptographically broken, and it is possible to create chosen-prefix
hash collisions for <USD$50K [1]

上文大意为因为 SHA-1 希哈算法可以被爆力破解了,所以自此版本 (8.8) 开始默认禁用使用了 SHA-1 算法的 RSA 密钥。

重新启用 ssh-RSA 认证

如果服务器端的 SSH 只支持 ssh-RSA 认证,那么我们只能重新启用它。

openssh 8.8的 README 中,给出了重新启用 RSA 密钥的方法,如下:

1
2
3
4
5
6
7
8
9
Host haven200
HostName haven200.com
HostKeyAlgorithms=+ssh-rsa
PubkeyAcceptedKeyTypes=+ssh-rsa
User haven200
Port 22
Protocol 2
SendEnv upnpcv
IdentityFile ~/.ssh/haven200_rsa

即使用 HostKeyAlgorithms=+ssh-rsa PubkeyAcceptedKeyTypes=+ssh-rsa 来重新启用 RSA 密钥。

升级 SSH 认证

为了安全,我们使用更安全的 ecdsa 密钥来替代 RSA 密钥进行身份认证。

1
~$ ssh-keygen -t ecdsa -f ~/.ssh/haven200_ecdsa

References:
openssh