服务器端git clone解决“ssh:connect to host github.com port 22: Connection timed out”

问题描述:

本博客主要解决的问题:使用vscode远程连接服务器时,使用git clone如下报错
在这里插入图片描述

ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.

解决方案:

1.拿到SSH Key

SSH key的位置在 .ssh/id_rsa.pub,复制下来备用。

2.github上添加该ssh key

如下图所示,点击new ssh key
服务器端git clone解决“ssh:connect to host github.com port 22: Connection timed out”_第1张图片

服务器端git clone解决“ssh:connect to host github.com port 22: Connection timed out”_第2张图片

3.验证是否成功

ssh -T git@github.com
//成功会运行结果在这里插入代码片出现类似如下
Hi xiangshuo1992! You've successfully authenticated, but GitHub does not provide shell access.
//失败报错如下
ssh:connect to host github.com port 22: Connection timed out

4.解决方法

如果你以上运行成功的话,下面就不用看了。如果出现上述的error,请按照下面的操作来。
在.ssh目录下,新建config文件,写入如下信息:

Host github.com
User git
Hostname ssh.github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa
Port 443

Host gitlab.com
Hostname altssh.gitlab.com
User git
Port 443
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa

服务器端git clone解决“ssh:connect to host github.com port 22: Connection timed out”_第3张图片

保存退出即可。

最后的关键步骤来了,修改config文件的权限为600!
步骤如下:

cd ~/.ssh
chmod -R 600 config

最后一步是很多博主忽略的问题,如果不进行最后一步会有如下报错:

Bad owner or permissions on /lustre/home/pjwang/.ssh/config

你可能感兴趣的:(git,ssh,github)