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

45 篇文章 0 订阅
11 篇文章 1 订阅

一、问题

无法进行clone项目和其他Git操作。执行检测连接命令 ssh -T git@github,com报错
ssh:connect to host github.com port 22: Connection timed out
即:连接22端口超时
在这里插入图片描述

涉及到的文件
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_hosts生成SSH连接日志

host文件
C:\Windows\System32\drivers\etc\hosts
IP域名本地映射文件,只要在本机中查到了指定的域名,就不会继续去DNS(域名系统)中继续查找。
Windows设置本地DNS域名解析hosts文件配置

配置SSH公私钥可参考我这篇GitHub&Gitee&Gitlab&极狐(JihuLab)同时生成并配置和检测不同SSH公私钥详细过程

二、解决问题

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

//详细连接过程,-v表示verbose  
ssh -vT git@github.com
或者
ssh -Tvvv git@github.com

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

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

在这里插入图片描述
这里::1IPV6的localhost地址127.0.0.1IPV4的localhost地址。这里基本可以认为是DNS域名解析出了问题,导致GitHub的域名被解析成了本地localhost的ip地址,导致无法连接上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

在这里插入图片描述

C:\Users\JIACHENGER\.ssh\known_hosts备份,后将known_hosts内容清空,再重新执行检测连接命令 ssh -T git@github,com,这样做偶尔可以连接成功,但过了一会儿还是会报同样的错误
在这里插入图片描述
我同时配置SSH公私钥的除了GitHub以外的其他平台(Gitee&Gitlab&极狐(JihuLab))都可以重新正常连接(过程中需要输入yes确认,表示确认添加主机到可信任列表本机在之前第一次生成SSH公私钥的时候,没有配置访问密码),并且在C:\Users\JIACHENGER\.ssh\known_hosts生成连接日志,如下图:
GitHub&Gitee&Gitlab&极狐(JihuLab)同时生成并配置和检测不同SSH公私钥详细过程
在这里插入图片描述

2.3 排除端口

C:\Users\JIACHENGER\.ssh\config 中,若config中没有指定端口,默认使用22端口,进行SSH连接。
使用443端口(默认情况)config配置如下:
注意:GitHub端口 443 的主机名Hostnamessh.github.com,而不是 github.com

# github
# ssh -T git@github.com
Port 443    
#Port 22 
Host github.com
Hostname ssh.github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

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

# github
# ssh -T git@github.com
#Port 443    
Port 22 
Host github.com
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

仍然还会报同样的错误说明大概率和端口没有关系:
在这里插入图片描述

2.4 在hosts中手动配置GitHub域名映射解决问题

host文件C:\Windows\System32\drivers\etc\hosts手动配置GitHub域名映射,在文件末尾处增加一行 140.82.113.4 github.com。此处的域名github.comC:\Users\JIACHENGER\.ssh\config文件中GitHub配置的Hostname值一致,均为github.com

# github
# ssh -T git@github.com
#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

IP域名本地映射文件,只要在本机中查到了指定的域名,就不会继续去DNS(域名系统)中继续查找。
参考:Windows设置本地DNS域名解析hosts文件配置

# 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

C:\Windows\System32\drivers\etc\hosts中手动配置GitHub域名映射使用时,同时不需要在C:\Users\JIACHENGER\.ssh\config 文件中配置端口,默认使用22端口
如下

# github
# ssh -T git@github.com
#Port 443    
#Port 22 
Host github.com
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/github_id_rsa

如果配置了端口,就会报下面的错误:

在这里插入图片描述

在hosts中手动配置GitHub域名映射后检测GitHub连接(config中不配置端口,默认使用22端口),成功连接

$ ssh -T git@github.com
Hi DJCKING! You've successfully authenticated, but GitHub does not provide shell access.

在这里插入图片描述

三、参考

测试 SSH 连接
在 HTTPS 端口使用 SSH
GitHub&Gitee&Gitlab&极狐(JihuLab)同时生成并配置和检测不同SSH公私钥详细过程
Git问题:解决“ssh:connect to host github.com port 22: Connection timed out”
坑:ssh: connect to host github.com port 22: Connection refused
Windows设置本地DNS域名解析hosts文件配置
ssh远程登录报错:kex_exchange_identification: Connection closed by remote host

  • 9
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
问题描述中提到了在使用Ubuntu系统时出现了"ssh: connect to host github.com port 22: Connection timed out"的错误。这个错误表示连接到github.comSSH端口22超时了。为了解决这个问题,可以尝试以下方法: 1. 修改SSH配置文件: - 打开终端并输入以下命令:`sudo vim /etc/ssh/ssh_config` - 找到`#Port 22`这一行(可能是注释状态),将其修改为`Port 443`(如果没有这一行,可以直接添加) - 保存并退出配置文件 - 重新连接到github.com,看是否问题已经解决 2. 使用不同的SSH命令: - 打开终端并输入以下命令:`ssh -T git@ssh.github.com` 或 `ssh -T -p 443 git@github.com` - 如果连接成功并显示"You've successfully authenticated",则说明问题已解决;如果仍然显示"ssh: connect to host github.com port 22: Connection timed out",则继续尝试以下命令 - 输入命令:`ssh -T -p 443 git@ssh.github.com` - 检查是否有异常信息显示 3. 配置新的端口: - 打开终端并输入以下命令:`cd ~/.ssh && vim config` - 在编辑器中添加以下内容并保存退出: ``` Host github.com User git Hostname ssh.github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa Port 443 ``` - 重新连接到github.com,看是否问题已经解决 这些方法应该能帮助您解决Ubuntu系统中的"ssh: connect to host github.com port 22: Connection timed out"错误。记得在尝试这些方法之前先备份您的SSH配置文件,以防出现意外情况。希望对您有所帮助!
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值