解决 Github SSH 连接超时

今天新建了一个 Github 仓库,结果发现pull不下来,提示:

1
2
ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.

使用 ssh -T 测试连接,超时。

1
2
ssh -T
ssh: connect to host github.com port 22: Connection timed out

但是到浏览器访问 https://github.com 却没有问题。

到终端ping一下 github.com,发现ping不通。

1
ping github.com

ping github.com

但ping ssh.github.com 是可以的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ ping github.com

正在 Ping github.com [20.205.243.166] 具有 32 字节的数据:
请求超时。
请求超时。
请求超时。
请求超时。

20.205.243.166 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 0,丢失 = 4 (100% 丢失),

$ ping ssh.github.com

正在 Ping ssh.github.com [20.205.243.160] 具有 32 字节的数据:
来自 20.205.243.160 的回复: 字节=32 时间=104ms TTL=112
来自 20.205.243.160 的回复: 字节=32 时间=104ms TTL=112
来自 20.205.243.160 的回复: 字节=32 时间=99ms TTL=112
来自 20.205.243.160 的回复: 字节=32 时间=97ms TTL=112

20.205.243.160 的 Ping 统计信息:
数据包: 已发送 = 4,已接收 = 3,丢失 = 1 (25% 丢失),
往返行程的估计时间(以毫秒为单位):
最短 = 97ms,最长 = 104ms,平均 = 100ms

于是可以修改一下 hosts 文件,将 github.com 指向 ssh.github.com

1
notepad $env:USERPROFILE\.ssh\config
1
2
3
4
# GitHub Start
Host github.com
Hostname ssh.github.com
# GitHub End
1
notepad %USERPROFILE%\.ssh\config
1
2
3
4
# GitHub Start
Host github.com
Hostname ssh.github.com
# GitHub End
1
vim ~/.ssh/config
1
2
3
4
# GitHub Start
Host github.com
Hostname ssh.github.com
# GitHub End

再次测试连接,成功。

1
2
3
$ ssh -T -p 443 git@ssh.github.com
> Hi USERNAME! You've successfully authenticated, but GitHub does not
> provide shell access.

端口 443 后首次与 GitHub 链接时,会提示是否继续连接,输入 yes 即可。

1
2
3
4
5
The authenticity of host '[ssh.github.com]:443 ([140.82.112.36]:443)' can't be established.
ED25519 key fingerprint is SHA256:+DiY3wvvV6TuJJhbpZisF/zLDA0zPMSvHdkr4UvCOqU.
This host key is known by the following other names/addresses:
~/.ssh/known_hosts:32: github.com
Are you sure you want to continue connecting (yes/no/[fingerprint])?

参考