栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

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

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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解析被运营商劫持了
使用了科学上网工具
按照我上面写的解决方案操作即可解决。

[原文链接](https://zhuanlan.zhihu.com/p/521340971#:~:text=ssh:%20connect%20to%20host%20github.com%20port%2022:%20Connection,%E7%9A%8422%E7%AB%AF%E5%8F%A3%E8%A2%AB%E6%8B%92%E7%BB%9D%E4%BA%86%E3%80%82%20%E5%8E%9F%E6%9C%AC%E4%BB%A5%E4%B8%BA%20github.com%20%E6%8C%82%E4%BA%86%EF%BC%8C%E4%BD%86%E6%98%AF%E6%B5%8F%E8%A7%88%E5%99%A8%E8%AE%BF%E9%97%AE%20github.com%20%E4%B8%80%E5%88%87%E6%AD%A3%E5%B8%B8%E3%80%82%20%E7%BD%91%E4%B8%8A%E6%90%9C%E7%B4%A2%E8%BF%99%E4%B8%AA%E6%8A%A5%E9%94%99%EF%BC%8C%E5%8F%91%E7%8E%B0%E5%BE%88%E5%A4%9A%E4%BA%BA%E9%81%87%E5%88%B0%E8%BF%99%E4%B8%AA%E9%97%AE%E9%A2%98%EF%BC%8C%E5%A4%A7%E6%A6%82%E6%9C%892%E4%B8%AA%E5%8E%9F%E5%9B%A0%E5%92%8C%E5%AF%B9%E5%BA%94%E8%A7%A3%E5%86%B3%E6%96%B9%E6%A1%88%EF%BC%9A%20%E4%BD%BF%E7%94%A8GitHub%E7%9A%84443%E7%AB%AF%E5%8F%A3)


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/939563.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号