问题描述
- 现象:
git
连接 GitHub 时,报错Connection refused on port 22
。 - 触发条件:
- 使用移动热点或免费加速器时必现。
- 使用正常 Wi-Fi 时不出现。
解决步骤
-
临时切换为 HTTPS 协议(绕过 22 端口):
git remote set-url origin https://github.com/username/repo.git
-
强制 SSH 走 443 端口(GitHub 支持):
- 修改
~/.ssh/config
:Host github.comHostname ssh.github.comPort 443User git
- 测试连接:
ssh -T git@github.com
- 修改
-
检查代理冲突:
- 若使用加速器,关闭或重置 Git 代理配置:
git config --global --unset http.proxy git config --global --unset https.proxy
- 若使用加速器,关闭或重置 Git 代理配置:
验证结果
- ✅ HTTPS 协议可正常
git push/pull
。 - ✅ SSH over 443 端口后,连接成功。
补充说明
- 关联场景:移动网络或公共代理可能屏蔽 22 端口。
- 快速修复优先级:
- 优先使用 HTTPS。
- 长期方案:配置 SSH over 443。