ssh连接失败问题(ssh:Could not resolve hostname github.com: Temporary failure in name)
环境
环境:wsl2下的ubuntu系统
终端:vscode中的终端
系统:Windows 10 22H2 19045.3324
网络环境:fastgithub(github开源项目)下,梯子未使用但之前使用过。
遇到的问题
从这段可以找到信息
ssh:Could not resolve hostname github.com: Temporary failure in name。
解决第一步:找到信息
起初没有理解resolve hostname github.com是什么意思,于是就google和StackOverflow搜了一会,在StackOverflow上面找到了这段回答,我贴出下图。
第二步;尝试他的方法
所以我在Ubuntu的终端ping了github.com和baidu.com,结果是github无法ping通,而baidu.com可以ping通,接着继续网上找解决办法,这里问题就细化为了dns error。然后在一个回答中找到了,如下图所示。
在ubuntu所在终端中输入
echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf
再ping github.com就可以ping通了,此时我非常高兴的去运行了git clone,结果仍然得到了错误。提示我如下图。
这里的关键信息是 Connection closed by 20.205.243.166 port 22。
第三步:再接再厉
既然dns问题解决了,那么第一个answer无法继续帮助我了,所以我又找了找,找到了这篇文章。
关键的信息是22端口被屏蔽了(尽管我关闭了Windows的防火墙,且查看了Ubuntu防火墙的状态(输入sudo ufw status即可查看)是inactive,它仍然是被屏蔽的状态没错),我没再纠结22端口的事情,而是按照文章内容先测试一下这个443端口是否可以成功,这是非常重要的前提。
执行命令ssh -T -p 443 git@ssh.github.com
后不再提示connection refused
,所以要尝试这个方案的小伙伴先执行这条命令测试下。
22端口可能被防火墙屏蔽了,可以尝试连接GitHub的443端口。
ruby 复制代码 $ 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端口。
复制代码 Host github.com Hostname ssh.github.com Port 443
如果~/.ssh
目录下没有config文件,新建一个即可。
修改完~/.ssh/config
文件后,使用ssh -T git@github.com
来测试和GitHub的网络通信是否正常,如果提示Hi xxxxx! You've successfully authenticated, but GitHub does not provide shell access.
就表示一切正常了。
这里大段引用了下面一篇文章的文字,我在最后标注了来源
git clone成功
困扰了一个晚上和一个上午的问题终于解决,done!
这步我因为提前输入了ssh -T -p 443 git@ssh.github.com,已经确认了指纹,要输入yes,接着才git clone成功。
参考的文章
github wsl 一个issue:github.com/microsoft/W…
connecting-to-github-with-ssh github文档:docs.github.com/zh/authenti…
掘金文章(大幅引用版):juejin.cn/post/710273… 作者:coding进阶
掘金文章:juejin.cn/s/wsl2%20ss…
防火墙查看状态: