解决Push代码到Github时发生【connect to host github.com port 2/2:Operation timed outfatal】错误的问题

今天在使用 Hexo 推送博客到 Github 仓库的时候,一直推送不成功,老是报如下错误:

1
2
3
4
5
6
ssh: connect to host github.com port 22: Operation timed out
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

问题场景还原:

查看 ssh 链接,打开终端,执行如下命令
1
ssh -T git@github.com

等待一会,终端大概发生如下错误信息

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

解决步骤:

1、新建 config 文件:

找到.ssh 文件夹,Mac 中路径是 ~/.ssh,新建一个 config 文件,不带后缀。

1
touch config
2、修改 config 文件:

打开 config 文件,添加下列代码,注意 User 后面的邮箱改为你的邮箱,其他配置不需改变

1
2
3
4
5
6
Host github.com
User xxx@qq.com
Hostname ssh.github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
Port 443
3、再次执行下面代码,通过 443 端口尝试连接:
1
ssh -T git@github.com

显示下面提示:

1
2
3
4
The authenticity of host '[ssh.github.com]:443 ([20.200.200.100]:443)' can't be established.
ECDSA key fingerprint is SHA256:XXXXXXXX/XXXXXX/XXXXXXX.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
Warning: Permanently added '[ssh.github.com]:443,[20.200.200.100]:443' (ECDSA) to the list of known hosts.
4、再次执行下面代码:
1
ssh -T git@github.com
5、回应下面消息,说明成功了
1
Hi xxx! You've successfully authenticated, but GitHub does not provide shell access.

接下来就可以正常推送代码了。