git遇到Connection closed by 140.82.113.3 port 22解决思路

451 阅读1分钟

1. 检查 SSH 密钥配置:

  • 确保你已经配置了 SSH 密钥并将其添加到 SSH 代理中:

    ssh-add -l
    
  • 确保你的公钥(~/.ssh/id_rsa.pub)已添加到 GitHub 账户的“SSH 和 GPG 密钥”中。

2. 验证 SSH 连接:

  • 使用详细输出测试与 GitHub 的连接,以获取更多信息:

    ssh -vT git@github.com
    
  • 此命令将提供有关连接失败位置的更详细信息。

3. 检查防火墙或网络限制:

  • 如果你在防火墙或阻止 SSH(端口 22)的网络后面,可以尝试通过 HTTPS(端口 443)连接。GitHub 支持通过 HTTPS 端口的 SSH 连接:

    ssh -T -p 443 git@ssh.github.com
    
  • 如果这个方法有效,你可能需要调整网络设置,或者经常使用这个命令。

4. 检查 SSH 配置:

  • 打开你的 SSH 配置文件(~/.ssh/config),确保没有冲突的设置:

    Host github.com
      Hostname ssh.github.com
      User git
      Port 22
      IdentityFile ~/.ssh/id_rsa
      TCPKeepAlive yes
      IdentitiesOnly yes
    
  • 如果需要,可以将端口调整为443

6. 检查 SSH 密钥权限:

  • 确保你的 SSH 密钥文件有正确的权限:

    chmod 600 ~/.ssh/id_rsa
    chmod 644 ~/.ssh/id_rsa.pub
    

7. 重启 SSH 代理:

  • 重启 SSH 代理有时可以解决连接问题:

    eval "$(ssh-agent -s)"
    ssh-add ~/.ssh/id_rsa
    

如果这些步骤都无法解决问题,你可能需要根据 ssh -vT git@github.com 命令的详细输出进行进一步的调查。