错误信息
userauth_pubkey: signature algorithm ssh-rsa not in PubkeyAcceptedAlgorithms [preauth]
也可能输出为
- userauth_pubkey: key type ssh-rsa not in PubkeyAcceptedAlgorithms [preauth]
- userauth_pubkey: key type ssh-rsa not in PubkeyAcceptedKeyTypes [preauth]
- userauth_pubkey: signature algorithm ssh-ed25519 not in PubkeyAcceptedAlgorithms [preauth]
各 sshd 版本的措辞(已对照 OpenSSH 源代码核实):9.0 及更高版本为“signature algorithm … not in PubkeyAcceptedAlgorithms”;8.5~8.9 为“key type … not in PubkeyAcceptedAlgorithms”;8.4 及更早版本为“key type … not in PubkeyAcceptedKeyTypes”(该选项在 8.5 中改了名)。消息中的算法取决于客户端以及服务器的限制。
OpenSSH server (sshd) log — the client sees only "Permission denied (publickey)."
含义
服务器日志中的这条消息解释了客户端看到的“Permission denied (publickey)”:客户端提供的签名算法不被服务器接受。如果是 `ssh-rsa`,那就是基于 SHA-1 的 RSA 签名方案,自 OpenSSH 8.8(2021年)起默认禁用。RSA 密钥本身没有问题——同一个密钥配合 `rsa-sha2-256`/`rsa-sha2-512` 算法就能正常使用——但客户端(旧版 OpenSSH、过时的库或嵌入式设备)只支持 SHA-1 这一变体。
原因
旧版 SSH 客户端或库用 ssh-rsa 签名
常见7.2 之前的 OpenSSH、2.9 之前的 Paramiko(Ansible 的 paramiko 连接插件也基于它)、原版 JSch 0.1.55(Jenkins 插件、IDE)、较旧的 golang.org/x/crypto 以及许多网络设备都只能生成 `ssh-rsa` 签名。
服务器进一步限制了算法
偶见安全加固指南会把 `PubkeyAcceptedAlgorithms` 设成一个很短的列表(例如只保留 Ed25519 和 ECDSA);不在列表中的 RSA 或 Ed25519 密钥都会被拒绝,带 `ssh-ed25519` 的那条别名消息就说明了这一点。
DSA 密钥
偶见`ssh-dss` 自 OpenSSH 7.0 起已被禁用,并在 10.0 中被移除。DSA 密钥必须更换。
修复方法
- 1.
改用现代密钥或现代客户端
Ed25519 密钥可以彻底避开 RSA/SHA-1 问题;否则就更新客户端库,让它能协商 `rsa-sha2-*`。
bash # Best: a modern key type, which every current client signs correctly ssh-keygen -t ed25519 -C "deploy@ci" -f ~/.ssh/deploy_ed25519 # Or keep the RSA key and use a client that supports rsa-sha2-256/512 # (OpenSSH >= 7.2, Paramiko >= 2.9, JSch fork 0.2.x, golang.org/x/crypto >= 2022, PuTTY >= 0.75) - 2.
如果确有必要,只在最小范围内允许 ssh-rsa
只为某一台老旧设备单独写一个 `Match` 块,其他所有客户端仍然不允许使用 SHA-1 签名。只把它当作临时措施。
text # /etc/ssh/sshd_config.d/legacy-device.conf - only for one legacy client, and remove it afterwards Match Address 10.0.5.17 PubkeyAcceptedAlgorithms +ssh-rsa
相关错误
Permission denied (publickey).OpenSSH client (ssh, scp, sftp, git over SSH)Unable to negotiate with 127.0.0.1 port 22: no matching host key type found. Th…OpenSSH client (8.8+) connecting to a server that only offers legacy host key algorithmsssh: handshake failed: ssh: unable to authenticate, attempted methods [none pub…golang.org/x/crypto/ssh (Go)