解决 macOS Ventura 使用 ssh、git 等无法正常使用的问题

7,821 阅读3分钟

关键词:macOS Ventura、Ventura、SSH、git、Permission denied

若移动端访问不佳,请使用 –> GithubPage 版

问题描述

升级到 macOS Ventura 后,无法使用 SSH 命令登入服务器,开启日志后,可能会看到 no matching host key type found 的报错信息。 如果是使用了基于 SSH 协议的相关指令,也会发生错误。比如使用 git clonegit pull 等去同步基于 SSH 地址的 git 仓库代码时,会提示 Permission denied (publickey)

定位问题

经过查证,macOS Ventura 内置使用了 OpenSSH_9.0p1,根据 OpenSSH 发行说明 可以得知,从 OpenSSH 8.8/8.8p1 版本开始,就默认关闭了 ssh-rsa 算法。那么 macOS Ventura 内置使用的 OpenSSH_9.0p1 也是默认关闭了 ssh-rsa 算法。

原文内容如下:

OpenSSH 8.8/8.8p1 (2021-09-26)
Incompatibility is more likely when connecting to older SSH
implementations that have not been upgraded or have not closely tracked
improvements in the SSH protocol. For these cases, it may be necessary
to selectively re-enable RSA/SHA1 to allow connection and/or user
authentication via the HostkeyAlgorithms and PubkeyAcceptedAlgorithms
options. For example, the following stanza in ~/.ssh/config will enable
RSA/SHA1 for host and user authentication for a single destination host:

    Host old-host
        HostkeyAlgorithms +ssh-rsa
	    PubkeyAcceptedAlgorithms +ssh-rsa

We recommend enabling RSA/SHA1 only as a stopgap measure until legacy
implementations can be upgraded or reconfigured with another key type
(such as ECDSA or Ed25519).

OpenSSH 8.7/8.7p1 (2021-08-20)
OpenSSH will disable the ssh-rsa signature scheme by default in the
next release.

解决方案

解决方案有 2 个:

  1. 基于更安全的 ed25519 哈希算法生成新的密钥,并配置到对应的服务器上、Github|Gitlab 的后台等
  2. 本地重新启用 ssh 对 ssh-rsa 算法的支持

方案一:重新生成 ed25519 算法的密钥

ssh-keygen -t ed25519

执行上述命令后,按照提示输入信息,并记录好生成的密钥文件名信息(密码,可选)。 后续操作

  1. 如果是用于登录服务器,替换或者新增密钥的公钥信息到服务器上
  2. 如果是 Github|Gitlab,添加新的公钥信息到账号的SSH-Key里

方案二:重新启用 RSA/SHA1

如果替换新的密钥成本比较大,可以考虑重新启用 RSA/SHA1 以允许连接或者进行用户认证。 具体的配置,参考 OpenSSH 8.8/8.8p1 的 Release Notes 可知,可以选择仅启用单个密钥的 RSA/SHA1 支持,也可无差别全部启用。

都是针对 ~/.ssh/config 文件(不存在的话则新增此文件)进行操作:

启用单个主机或者地址:(如果不懂 Host xxx-host 的含义,建议使用启用全部场景)

# 在 ~/.ssh/config 文件的对应主机配置里新增2行:
Host xxx-host
    HostkeyAlgorithms +ssh-rsa
    PubkeyAcceptedAlgorithms +ssh-rsa

默认全部场景都启用

# 配置里有 Host * 配置的,需要写到 Host * 内
Host *
    HostkeyAlgorithms +ssh-rsa
    PubkeyAcceptedAlgorithms +ssh-rsa
# 没有的,在 ~/.ssh/config 文件的顶部插入或者尾部新增2行:
HostkeyAlgorithms +ssh-rsa
PubkeyAcceptedAlgorithms +ssh-rsa

PS:如果 Windows 11 或者 Linux 遇到类似的情况,解决思路也是如此。

如果有什么建议或者问题可以随时联系我,共同探讨学习: