Skip to content

问题背景

在通过 SSH 方式连接 GitHub 时遇到连接问题,执行 ssh -T git@github.com 报错:

kex_exchange_identification: read: Connection reset

排查过程

1. 初步分析

这个错误是网络连接层面的问题,而不是 SSH key 配置问题。错误信息表明 TCP 连接被对方重置,通常原因包括:

  • SSH 端口 22 被防火墙或运营商阻断
  • 网络环境限制(如公司内网、代理等)

2. 尝试显式指定密钥

由于默认 SSH 密钥可能存在路径或权限问题,尝试显式指定密钥:

bash
ssh -i ~/.ssh/id_ed25519_xxx -T git@github.com

指定密钥后连接成功,说明密钥本身是有效的,问题出在默认密钥的查找或网络层面。

解决方案:配置 SSH Config

为避免每次都要手动指定密钥,可以配置 SSH 配置文件 ~/.ssh/config

配置文件内容

plaintext
Host github.com
    HostName github.com
    User git
    IdentityFile ~/.ssh/id_ed25519_xxx

配置参数说明:

  • Host:匹配的 SSH 目标主机
  • HostName:实际连接的主机地址
  • User:SSH 用户名(GitHub 固定为 git)
  • IdentityFile:指定使用的私钥路径

配置完成后,直接运行 ssh -T git@github.com 即可自动使用指定密钥。

补充:多密钥管理建议

如果存在多个 SSH 密钥(工作、个人、服务器等),建议:

  1. 命名规范:如 id_ed25519_workid_ed25519_personal
  2. 按用途分组配置
    plaintext
    Host github-work
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_ed25519_work
    
    Host github-personal
        HostName github.com
        User git
        IdentityFile ~/.ssh/id_ed25519_personal

总结

遇到 Connection reset 错误时,优先排查网络连通性。如果确认是密钥问题,显式指定密钥可以快速验证。长期解决方案是配置 SSH config 文件,实现自动化密钥选择。

Released under the MIT License.