本文最后更新于:2024年1月17日 上午

有时,防火墙或者其他什么神器的原因导致 SSH 连接失败,git 无法链接服务器,这种情况下可能使用 443 端口可以解决问题。

问题复现

有时候突然 git 就不好使了,报错:

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

确认不是网络问题,不是 ssh key 的问题,那就可以试试 https 端口访问 git

问题修复

尝试用 443 端口连接 git

首先测试这个路数是否可以解决问题,执行命令:

1
ssh -T -p 443 git@ssh.github.com

如果输出:

1
2
> Hi USERNAME! You've successfully authenticated, but GitHub does not
> provide shell access.

说明该方案可以解决当前的问题。

可以直接将现有的 git ssh 地址 git@github.com:YOUR-USERNAME/YOUR-REPOSITORY.git 替换为 ssh://git@ssh.github.com:443/YOUR-USERNAME/YOUR-REPOSITORY.git

1
git clone ssh://git@ssh.github.com:443/YOUR-USERNAME/YOUR-REPOSITORY.git

强制 https 执行 git 链接

如果能够通过端口443将 SSH 设置为 git@SSH. GitHub.com,则可以覆盖 SSH 设置,强制任何到 GitHub.com 的连接通过该服务器和端口运行。

要在 SSH 配置文件中设置这个值,请编辑位于 ~/. SSH/config 的文件,并添加以下部分:

1
2
3
4
Host github.com
Hostname ssh.github.com
Port 443
User git

这样不用修改链接,之前的都会走 443 端口的 https 方式

参考资料



文章链接:
https://www.zywvvd.com/notes/tools/git/git-ssh-443/git-ssh-443/


“觉得不错的话,给点打赏吧 ୧(๑•̀⌄•́๑)૭”

微信二维码

微信支付

支付宝二维码

支付宝支付

Git 通过 Https 端口进行 SSH 链接
https://www.zywvvd.com/notes/tools/git/git-ssh-443/git-ssh-443/
作者
Yiwei Zhang
发布于
2024年1月16日
许可协议