恭喜,成功入坑 GitHub 。。。

打印 上一主题 下一主题

主题 546|帖子 546|积分 1638

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

大家好,我是杰森。GitHub 对大家来说一定不陌生,无论是学习还是交(爬)朋(项)友(目)。但是今天,我好像和它失联了……

当我像往常一样clone项目时,却得到了这样的报错
  1. $ git clone git@github.com:appletdevelop/full-stack.git
  2. Cloning into ‘full-stack’...
  3. ssh: connect to host github.com port 22: Connection refused
  4. fatal: Could not read from remote repository.
  5. Please make sure you have the correct access rights and the repository exists.
复制代码
什么都不能阻止打工人搬砖,必须要解决。经过一番排查,终于找到了问题的根源。分享两种解决方案,大家注意避坑。
方案一:配置 DNS

因为错误信息显示 Connection refused ,所以我们需要去看看建立连接时发生了什么,为什么会出错。查看日志,果然发现端倪
  1. $ ssh -vT git@github.com
  2. OpenSSH_9.0p1, OpenSSL 1.1.1o  5 July 2022
  3. debug1: Reading configuration data /c/Users/jason/.ssh/config
  4. debug1: Reading configuration data /etc/ssh/ssh_config
  5. debug1: Connecting to github.com [::1] port 22.
  6. debug1: connect to address ::1 port 22: Connection refused
  7. debug1: Connecting to github.com [127.0.0.1] port 22.
  8. debug1: connect to address 127.0.0.1 port 22: Connection refused
  9. ssh: connect to host github.com port 22: Connection refused
复制代码
日志显示,IPv6 和 IPv4 的 localhost 地址分别为 ::1 和 127.0.0.1,这意味着我们在连接 github 时,其域名将会被解析为 localhost 地址,当然也就无法连接。
打开查询网站,找到 github.com 的 IP 地址

Windows 下,打开本机 hosts 文件
C:\Windows\System32\drivers\etc
添加域名映射,并在 cmd 窗口刷新 DNS 配置
  1. 140.82.112.4 github.com
  2. # Refreshing DNS configurations
  3. $ ipconfig /flushdns
复制代码
重新拉取,成功。
方案二:修改端口号

从上面的报错信息中可以发现,重点在这一句
  1. ssh: connect to host github.com port 22: Connection refused
复制代码
ssh 连接 GitHub 的 22 号端口被拒绝。但是 ping 一下 github.com 能通,浏览器访问也没有问题,那有可能是该端口被防火墙蔽掉了。既然 22 端口拒绝访问,我们不妨尝试使用 443 端口进行连接。
使用 vim 指令编辑 ssh 配置文件,添加以下端口信息
  1. $ vim ~/.ssh/config
  2. # Add the following configuration information
  3. Host github.com
  4.   Hostname ssh.github.com
  5.   Port 443
复制代码
测试访问是否成功,通常不出意外的话意外就来了……
  1. $ ssh -T git@github.com
  2. The authenticity of host ‘[ssh.github.com]:443([unknown ip address]:443)’ can’t be established.
  3. xxx key fingerprint is xxx:xxx.
  4. This host key is known by the following other names/addresses:
  5.     # Delete the RSA information in line 8
  6.     ~/.ssh/known_hosts:8: github.com
  7. Host key verification failed.
复制代码
这与 ssh 的运行机制有关,ssh 会将本机访问过的计算机的 public key 记录在 ~/.ssh/known_hosts 下。当下次访问相同计算机时,若公钥不同则会发出警告,避免受到攻击。这里只需要找到 known_hosts 文件中对应 ip 的 RSA 并删除便可解决。
再次测试,看到以下信息则表示访问成功
  1. $ ssh -T git@github.com
  2. Hi xxx! You’ve successfully authenticated, but GitHub does not provide shell access.
复制代码
这样访问 GitHub 时,ssh 就会连接 443 端口,不会报错。

总结

总结下本次踩坑的原因,主要有两点:
总之:“网上冲浪也要注意暗礁,低头走路也要抬头看路”,以上就是本期分享啦,希望可以帮到您!

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

怀念夏天

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表