ssh: connect to host github.com port 22: Connection refused

项目场景:

配置好SSH公私钥后,Git 通过SSH 协议与github.com 连接


问题描述

SSH 链接代码

ssh -T git@github.com

给出的错误信息

ssh: connect to host github.com port 22: Connection refused

原因分析:

1.尝试与gitee.com进行连接,成功,因此应该不是公私钥的问题.
2.更改端口

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

同样失败

ssh: connect to host github.com port 443: Connection refused

若是成功连接,可以通过修改.ssh/config配置文件从而用端口443连接,增加如下信息
Host github.com
Hostname ssh.github.com
Port 443

3.尝试使用http协议克隆仓库

git clone https://github.com/yourname/yourRepositoryname.git

同样失败

fatal: unable to access 'https://github.com/yourname/yourRepositoryname.git/': failed to connect to github.com port 22 after 2076 ms: connection refused

而在gitee.com上却成功进行了克隆

解决方案:

这里的解决方案都来自于网络,特此声明,不是原创,只是记录一个找bug的过程,对我启发挺大。

网上的招都没用,只能自力更生了。
既然和GitHub建立ssh连接的时候提示connection refused,那我们就详细看看建立ssh连接的过程中发生了什么
可以使用ssh -v命令,-v表示verbose,会打出详细日志。

$ ssh -vT git@github.com
OpenSSH_9.0p1, OpenSSL 1.1.1o  3 May 2022
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to github.com [::1] port 22.
debug1: connect to address ::1 port 22: Connection refused
debug1: Connecting to github.com [127.0.0.1] port 22.
debug1: connect to address 127.0.0.1 port 22: Connection refused
ssh: connect to host github.com port 22: Connection refused

从上面的信息马上就发现了诡异的地方,连接http://github.com的地址居然是::1和127.0.0.1。前者是IPV6的localhost地址,后者是IPV4的localhost地址。

到这里问题就很明确了,是DNS解析出问题了,导致http://github.com域名被解析成了localhost的ip地址,就自然连不上GitHub了。

Windows下执行ipconfig /flushdns 清楚DNS缓存后也没用,最后修改hosts文件,增加一条github.com的域名映射搞定。

140.82.112.4 github.com

查找http://github.com的ip地址可以使用https://www.ipaddress.com/来查询
给出了正确地址140.82.112.4(这是目前2022年5月的ip地址,看起来应该是会改变的,因为我看到别人3月份写的文章是140.82.113.4)

也可以使用nslookup命令
nslookup github.com 8.8.8.8
nslookup是域名解析工具,8.8.8.8是Google的DNS服务器地址。
但是给出的却是错误地址

服务器:  dns.google
Address:  8.8.8.8

非权威应答:
名称:    github.com
Addresses:  ::1
          127.0.0.1

直接使用 nslookup github.com
就会使用本机已经设置好的DNS服务器进行域名解析
本机给出的也是错误地址

ipconfig /all可以查看本机DNS服务器地址。

这个问题其实就是DNS解析被污染了,有2种可能:

DNS解析被运营商劫持了
使用了科学上网工具
按照我上面写的解决方案操作即可解决。

原文链接

  • 7
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值