SSH connect to host github.com port 22 - Connection refused

问题

Windows系统,在个人电脑上使用Git命令操作GitHub上的仓库,突然提示错误:ssh: connect to host github.com port 22: Connection refused

例如:

1
2
3
4
5
6
$ 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.

解决方法

使用GitHub的443端口

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
$ 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

1
2
3
Host github.com
  Hostname ssh.github.com
  Port 443

注:如果~/.ssh目录下没有config文件,新建一个即可。Windows操作方法类似

修改完~/.ssh/config文件后,使用ssh -T git@github.com来测试和GitHub的网络通信是否正常,如果提示Hi xxxxx! You've successfully authenticated, but GitHub does not provide shell access. 就表示一切正常了。

这个方案有效的前提是: 执行命令ssh -T -p 443 git@ssh.github.com后不再提示connection refused,所以要尝试这个方案可先执行这条命令测试下。