github ssh连接方式及Connection closed by 198.xx.xx.xx port 22错误解决

在mac系统中,使用ssh连接GitHub

  1. 英文步骤详见:Setting up Git | The Odin Project
  2. 详细中文步骤可见:【GitHub从入门到精通】GitHub配置SSH公钥实现免密码拉取代码和上传代码保姆级详细教程-CSDN博客

跟随1教程过程中,使用private GitHub email address。
在2.5testing连接时出现超时,无法获得响应。

随后更换邮箱设置,不使用private邮箱,改为自己邮箱重新连接。此时terminal使用ssh -T git@github.com尝试连接时反馈connection closed

jasonwu@JasonWudeMacBook-Air ~ % ssh -T git@github.com    
Connection closed by 198.xx.xx.xx port 22

使用下述指令查看更多调试信息

ssh -vT git@github.com

得到结果如下

jasonwu@JasonWudeMacBook-Air ~ % ssh -vT git@github.com
OpenSSH_9.6p1, LibreSSL 3.3.6
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 21: include /etc/ssh/ssh_config.d/* matched no files
debug1: /etc/ssh/ssh_config line 54: Applying options for *
debug1: Authenticator provider $SSH_SK_PROVIDER did not resolve; disabling
debug1: Connecting to github.com port 22.
debug1: Connection established.
debug1: identity file /Users/jasonwu/.ssh/id_rsa type 0
debug1: identity file /Users/jasonwu/.ssh/id_rsa-cert type -1
debug1: identity file /Users/jasonwu/.ssh/id_ecdsa type -1
debug1: identity file /Users/jasonwu/.ssh/id_ecdsa-cert type -1
debug1: identity file /Users/jasonwu/.ssh/id_ecdsa_sk type -1
debug1: identity file /Users/jasonwu/.ssh/id_ecdsa_sk-cert type -1
debug1: identity file /Users/jasonwu/.ssh/id_ed25519 type 3
debug1: identity file /Users/jasonwu/.ssh/id_ed25519-cert type -1
debug1: identity file /Users/jasonwu/.ssh/id_ed25519_sk type -1
debug1: identity file /Users/jasonwu/.ssh/id_ed25519_sk-cert type -1
debug1: identity file /Users/jasonwu/.ssh/id_xmss type -1
debug1: identity file /Users/jasonwu/.ssh/id_xmss-cert type -1
debug1: identity file /Users/jasonwu/.ssh/id_dsa type -1
debug1: identity file /Users/jasonwu/.ssh/id_dsa-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_9.6
kex_exchange_identification: Connection closed by remote host
Connection closed by 198.xx.xx.xx port 22

该结果可能是由于防火墙和IP限制(可能的原因是国内环境使用代理IP导致GitHub的连接受限)
GitHub 允许使用端口 443 作为 SSH 的替代端口(通常用于 HTTPS 流量),这可以绕过一些防火墙限制。可以使用以下命令来通过另一端口测试:

ssh -T -p 443 git@ssh.github.com

最终连接成功

jasonwu@JasonWudeMacBook-Air ~ % ssh -T -p 443 git@ssh.github.com

The authenticity of host '[ssh.github.com]:443 ([198.18.0.89]:443)' can't be established.

ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.

This key is not known by any other names.

Are you sure you want to continue connecting (yes/no/[fingerprint])? yes

Warning: Permanently added '[ssh.github.com]:443' (ED25519) to the list of known hosts.

Hi <username>! You've successfully authenticated, but GitHub does not provide shell access.

要想一劳永逸,可以:

  1. 更改防火墙规则,允许22端口使用ssh。
  2. 或者修改ssh连接的config,强行让ssh连接走http流量的443端口。
    具体步骤:修改~/.ssh/config文件(没有的话就新建一个),添加以下内容:
    Host github.com
    Hostname ssh.github.com
    Port 443
    User git
    
    再次使用下述命令,成功连接~
    ssh -T git@github.com
    

真正的解法在这:
https://docs.github.com/en/authentication/troubleshooting-ssh/using-ssh-over-the-https-port

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值