解决ssh:connect to host github.com port 22: Connection timed out与kex_exchange_identification

1. Problem

Clone projects and other Git operations cannot be performed. ssh -T git@github,comAn error occurs when executing the detection connection command .
ssh:connect to host github.com port 22: Connection timed out
即:连接22端口超时
Insert image description here

涉及到的文件
C:\Users\JIACHENGER.ssh\config
C:\Users\JIACHENGER.ssh\github_id_rsa
C:\Users\JIACHENGER.ssh\github_id_rsa.pub

C:\Users\JIACHENGER\.ssh\known_hostsGenerate SSH connection logs

host文件
As long as C:\Windows\System32\drivers\etc\hosts
IP and C:\Windows\System32\drivers\etc\ 域名hosts 本地映射文件are included 在本机中查到了指定的域名, the search will not continue DNS(域名系统).
Windows sets local DNS domain name resolution hosts file configuration

配置SSH公私钥可参考我这篇: Detailed process of GitHub&Gitee&Gitlab&JihuLab simultaneously generating, configuring and detecting different SSH public and private keys

2. Solve problems

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

//详细连接过程,-v表示verbose  
ssh -vT [email protected]
或者
ssh -Tvvv [email protected]

//nslookup是域名解析工具,8.8.8.8是Google的DNS服务器地址
nslookup github.com 8.8.8.8

//使用本机已经设置好的DNS服务器进行域名解析
nslookup github.com

Insert image description here
Here ::1it is IPV6的localhost地址, 127.0.0.1is IPV4的localhost地址. This can basically be considered as DNS域名解析出了问题causing GitHub的域名被解析成了本地localhost的ip地址the inability to connect to GitHub.

2.2 kex_exchange_identification: Connection closed by remote host

此时又出现了一个问题

kex_exchange_identification: Connection closed by remote host
Connection closed by ::1 port 22

Insert image description here

Will C:\Users\JIACHENGER\.ssh\known_hostsback up and do it again known_hostslater , but after a while . Other platforms where I configured SSH public and private keys at the same time can reconnect normally (need to indicate, during the process ), and are in , as shown below: The detailed process of GitHub&Gitee&Gitlab&JihuLab simultaneously generating, configuring, and detecting different SSH public and private keys内容清空重新执行检测连接命令 ssh -T git@github,com偶尔可以连接成功还是会报同样的错误
Insert image description here
除了GitHub以外(Gitee&Gitlab&极狐(JihuLab))输入yes确认确认添加主机到可信任列表本机在之前第一次生成SSH公私钥的时候,没有配置访问密码C:\Users\JIACHENGER\.ssh\known_hosts生成连接日志

Insert image description here

2.3 Exclude ports

C:\Users\JIACHENGER\.ssh\config, if no port is specified in the config, port 22 will be used by default for SSH connection.
使用443端口(默认情况)config配置如下:
注意: GitHub port 443 主机名Hostnameis ssh.github.com, instead github.com.

# github
# ssh -T [email protected]
Port 443    
#Port 22 
Host github.com
Hostname ssh.github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

使用22端口(默认情况,不配置也是使用22端口)config配置如下:

# github
# ssh -T [email protected]
#Port 443    
Port 22 
Host github.com
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

仍然还会报同样的错误说明大概率和端口没有关系:
Insert image description here

2.4 Manually configure GitHub domain name mapping in hosts to solve the problem

In host文件C:\Windows\System32\drivers\etc\hosts手动配置GitHub域名映射 , 末尾add a line to the file 140.82.113.4 github.com. github.comThe domain name here is consistent with the value C:\Users\JIACHENGER\.ssh\configin the file GitHub配置的Hostname, both are github.com.

# github
# ssh -T [email protected]
#Port 443  
#注意:GitHub端口 443 的主机名Hostname为 ssh.github.com,而不是 github.com。  
#Port 22  此处注释,默认也使用22端口
Host github.com
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

IPAnd , as long as 域名, we will not continue to search in. Reference: Windows sets local DNS domain name resolution hosts file configuration本地映射文件在本机中查到了指定的域名DNS(域名系统)

# Added by Docker Desktop
192.168.1.14 host.docker.internal
192.168.1.14 gateway.docker.internal
# To allow the same kube context to work on the host and the container:
127.0.0.1 kubernetes.docker.internal
# End of section


#2023-9-19 19:08:03 配置
#解决ssh:connect to host github.com port 22: Connection timed out等问题
#在hosts中手动配置GitHub域名映射

140.82.113.4 github.com

When C:\Windows\System32\drivers\etc\hosts中manually configuring GitHub domain name mapping, you do not need to C:\Users\JIACHENGER\.ssh\configconfigure the port in the file, 默认使用22端口,
如下:

# github
# ssh -T [email protected]
#Port 443    
#Port 22 
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

如果配置了端口, the following error will be reported:

Insert image description here

在hosts中手动配置GitHub域名映射后检测GitHub连接(The port is not configured in config, and port 22 is used by default) 成功连接:

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

Insert image description here

3. Reference

Test SSH connection
on HTTPS port using SSH
GitHub&Gitee&Gitlab&JihuLab to generate, configure and detect different SSH public and private keys at the same time Detailed process Git
problem: Solve the "ssh:connect to host github.com port 22: Connection timed out"
pit: ssh: connect to host github.com port 22: Connection refused
Windows set up local DNS domain name resolution hosts file configuration
ssh remote login error: kex_exchange_identification: Connection closed by remote host

Guess you like

Origin blog.csdn.net/qyfx123456/article/details/133038067