problem phenomenon
This article uses the Windows system as an example to illustrate. It is normal to use Git commands on a personal computer to operate projects on GitHub. Suddenly, one day, the following error will be prompted ssh: connect to host github.com port 22: Connection refused
.
$ 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.
Troubleshoot ideas
ssh: connect to host github.com port 22: Connection refused
This error indicates that the connection github.com
on port 22 was refused.
I thought that github.com was down, but everything works fine when the browser accesses github.com.
Searching for this error on the Internet, I found that many people encountered this problem. There are about 2 reasons and corresponding solutions:
Use GitHub's port 443
Port 22 may be blocked by firewall, you can try to connect to port 443 of GitHub.
$ 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.
The idea of this solution is to add the following content to the ~/.ssh/config
file, so that port 443 will be used when ssh connects to GitHub.
Host github.com
Hostname ssh.github.com
Port 443
If there is no config file in the ~/.ssh
directory, create a new one.
After modifying the ~/.ssh/config
file, use ssh -T git@github.com
to test whether the network communication with GitHub is normal, if it prompts `Hi xxxxx! You've successfully authenticated, but GitHub does not
provide shell access.` means everything is fine.
However, this solution doesn't work for me, and it still prompts ssh: connect to host github.com port 443: Connection refused
after modification.
The premise of this scheme is effective : after executing the command ssh -T -p 443 git@ssh.github.com
, it will no longer prompt connection refused
, so the friends who want to try this scheme first execute this command to test.
Use https protocol, don't use ssh protocol
In your local GitHub repo directory, execute the following command:
$ git config --local -e
Then put the url configuration item inside from git format
url = git@github.com:username/repo.git
Modify to https format
url = https://github.com/username/repo.git
This actually modifies the .git/config
file in the root directory of the repo.
But this method doesn't work for me either .
solution
The online tricks are useless, you can only rely on yourself. Since the prompt connection refused
is displayed when establishing an ssh connection with GitHub, let's take a detailed look at what happened during the establishment of the ssh connection. You can use the ssh -v
command, -v
Indicates verbose, and a detailed log will be printed.
$ ssh -vT git@github.com
OpenSSH_9.0p1, OpenSSL 1.1.1o 3 May 2022
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to github.com [::1] port 22.
debug1: connect to address ::1 port 22: Connection refused
debug1: Connecting to github.com [127.0.0.1] port 22.
debug1: connect to address 127.0.0.1 port 22: Connection refused
ssh: connect to host github.com port 22: Connection refused
From the above information, I immediately found the strange place. The addresses connecting to github.com are actually ::1
and 127.0.0.1
. The former is the localhost address of IPV6, and the latter is the localhost address of IPV4.
At this point, the problem is very clear. There is a problem with DNS resolution, which causes the github.com domain name to be resolved to the IP address of localhost, and naturally cannot connect to GitHub.
Execute under Windows ipconfig /flushdns
It is useless after clearing the DNS cache. Finally, modify the hosts file and add a domain name mapping of github.com.
140.82.113.4 github.com
To find the ip address of github.com, you can use https://www.ipaddress.com/ to query, or you can use the nslookup
command
nslookup github.com 8.8.8.8
nslookup
is the domain name resolution tool, 8.8.8.8
is Google's DNS server address. Use directly
nslookup github.com
It will use the DNS server that has been set up on this machine for domain name resolution. ipconfig /all
You can view the DNS server address of this machine.
This problem is actually that the DNS resolution is polluted. There are two possibilities:
- DNS resolution hijacked by carrier
- used scientific internet tools
Follow the solution I wrote above to solve it. As for why the use of scientific Internet tools will be modified DNS resolution, you can pay attention to my official account, and I will write an article in the official account to explain the underlying principles in detail.
open source address
Articles and sample code are open sourced on GitHub: Beginner, Intermediate, and Advanced Tutorials in Go .
Official account: coding advanced. Follow the official account to get the latest Go language interview questions and Internet technology stack.
Personal website: Jincheng's Blog .
Zhihu: Wuji .
References
- https://chaxuri.com/archives/43.html
- https://stackoverflow.com/questions/15589682/ssh-connect-to-host-github-com-port-22-connection-timed-out
- https://docs.github.com/en/authentication/troubleshooting-ssh/error-permission-denied-publickey
- https://gist.github.com/Tamal/1cc77f88ef3e900aeae65f0e5e504794
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。