标题: ssh: connect to host github.com port 22: Connection refused [打印本页] 作者: 麻花痒 时间: 2024-8-17 07:37 标题: ssh: connect to host github.com port 22: Connection refused ssh: connect to host github.com port 22: Connection refused
题目现象
本文以Windows系统为例进行说明,在个人电脑上使用Git命令来操作GitHub上的项目,本来都很正常,突然某一天开始,会提示如下错误ssh: connect to host github.com port 22: Connection refused。
$ git pull ssh: connect to host github.com port 22: Connection refused
fatal: Could not read from remote repository. Please make sure you
have the correct access rights and the repository exists.
排查思绪
ssh: connect to host github.com port 22: Connection refused这个错误提示的是毗连github.com的22端口被拒绝了。
原本以为http://github.com挂了,但是欣赏器访问http://github.com齐备正常。
网上搜刮这个报错,发现很多人遇到这个题目,大概有2个缘故原由和对应解决方案:
1、使用GitHub的443端口
22端口可能被防火墙屏蔽了,可以尝试毗连GitHub的443端口。
打开git bash
$ vim ~/.ssh/config
# Add section below to it
Host github.com
Hostname ssh.github.com
Port 443
复制代码
$ ssh -T git@github.com
Hi xxxxx! You’ve successfully authenticated, but GitHub does not
provide shell access.
这个解决方案的思绪是:给~/.ssh/config文件里添加如下内容,这样ssh毗连GitHub的时候就会使用443端口。
Host github.com
Hostname ssh.github.com
Port 443
如果~/.ssh目录下没有config文件,新建一个即可。
修改完~/.ssh/config文件后,使用ssh -T git@github.com来测试和GitHub的网络通信是否正常,如果提示Hi xxxxx! You’ve successfully authenticated, but GitHub does not provide shell access. 就表现齐备正常了。
但是,这个方案在我这里行不通,修改后还是提示ssh: connect to host github.com port 443: Connection refused。 这个方案有效的条件是:实行命令ssh -T -p 443 git@ssh.github.com后不再提示connection refused,所以要尝试这个方案的小同伴先实行这条命令测试下。
2、使用https协议,不要使用ssh协议
在你的GitHub的本地repo目录,实行如下命令:
$ git config --local -e
然后把里面的url配置项从git格式
url = git@github.com:username/repo.git
修改为https格式
url = https://github.com/username/repo.git
这个实在修改的是repo根目录下的./git/config文件。 但是这个方法在我这里同样不见效。
解决方案
网上的招都没用,只能独立更生了。既然和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.113.4 github.com
查找http://github.com的ip地点可以使用https://www.ipaddress.com/来查询,也可以使用nslookup命令
nslookup github.com 8.8.8.8
nslookup是域名解析工具,8.8.8.8是Google的DNS服务器地点。直接使用
nslookup github.com
就会使用本机已经设置好的DNS服务器进行域名解析,ipconfig /all可以检察本机DNS服务器地点。
这个题目实在就是DNS解析被污染了,有2种可能: