无法连接GitHub-SHH443

之前用的好好的,从github拉取代码时突然出现ssh: connect to host ssh.github.com port 443: Connection timed out,不知道是自己不小心改了哪里的配置还是什么原因,现在记录一下处理过程。

  • 之前的.ssh\config的配置是这样的:
1
2
3
4
Host github.com
HostName ssh.github.com
Port 443
User git

使用ssh -T git@github.com命令,得到的返回的是ssh: connect to host http://ssh.github.com port 443: Connection refused。没有任何有价值的信息。

  • 于是在使用ssh -vT git@github.com看看详细情况,得到下面的返回

    1
    2
    3
    4
    5
    6
    7
    8
    9
    OpenSSH_9.1p1, OpenSSL 1.1.1s  1 Nov 2022
    debug1: Reading configuration data /c/Users/Andy/.ssh/config
    debug1: /c/Users/Andy/.ssh/config line 1: Applying options for github.com
    debug1: Reading configuration data /etc/ssh/ssh_config
    debug1: Connecting to ssh.github.com [::1] port 443.
    debug1: connect to address ::1 port 443: Connection refused
    debug1: Connecting to ssh.github.com [127.0.0.1] port 443.
    debug1: connect to address 127.0.0.1 port 443: Connection refused
    ssh: connect to host ssh.github.com port 443: Connection refused

    从上面的返回信息来看,是因为github.com的HostName被修改了,导致ssh连接失败。

  • 这个意思是系统把 http://ssh.github.com 解析成了 127.0.0.1/::1(本地回环地址),而不是 GitHub 服务器的真实 IP。找到原因了!开始解决:

打开系统hosts文件

1
C:\Windows\System32\drivers\etc\hosts

在文件的末尾加入下面的第一行的设置(如果有在用copilot,额外增加一条):

1
2
20.205.243.166    github.com
20.205.243.168 api.github.com

保存文件,然后刷新DNS缓存。
以管理员权限打开PowerShell,运行

1
ipconfig /flushdns

再将.ssh\config改为

1
2
3
4
Host github.com
HostName github.com
Port 22
User git

测试一下,成功了!

-T
1
>$ ssh -T [email protected]
1
2
Hi sunwaychan! You've successfully authenticated, but GitHub does not provide shell access.