关于github:坑ssh-connect-to-host-githubcom-port-22-Connection-refused

5次阅读

共计 2567 个字符,预计需要花费 7 分钟才能阅读完成。

问题景象

本文以 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 端口被回绝了。

本来认为 github.com 挂了,然而浏览器拜访 github.com 一切正常。

网上搜寻这个报错,发现很多人遇到这个问题,大略有 2 个起因和对应解决方案:

应用 GitHub 的 443 端口

22 端口可能被防火墙屏蔽了,能够尝试连贯 GitHub 的 443 端口。

$ 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,所以要尝试这个计划的小伙伴先执行这条命令测试下。

应用 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

从下面的信息马上就发现了诡异的中央,连贯 github.com 的地址竟然是 ::1127.0.0.1。前者是 IPV6 的 localhost 地址,后者是 IPV4 的 localhost 地址。

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

Windows 下执行ipconfig /flushdns 分明 DNS 缓存后也没用,最初批改 hosts 文件,减少一条 github.com 的域名映射搞定。

140.82.113.4 github.com

查找 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 种可能:

  • DNS 解析被运营商劫持了
  • 应用了迷信上网工具

依照我下面写的解决方案操作即可解决。

开源地址

文章和示例代码开源在 GitHub: Go 语言高级、中级和高级教程。

公众号:coding 进阶。关注公众号能够获取最新 Go 面试题和技术栈。

集体网站:Jincheng’s Blog。

知乎:无忌。

References

  • https://chaxuri.com/archives/…
  • https://stackoverflow.com/que…
  • https://docs.github.com/en/au…
  • https://gist.github.com/Tamal…
正文完
 0