git config 配置了 http.proxy 代理使用 ssh 仍然超时

471 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路。

最近不知道发生了什么,通过 ssh 协议对 GitHub 仓库进行任何操作都会超时,无法推拉代码。但使用 https 协议从 GitHub 拉代码正常。 因为我的账号配置了 2FA,还是需要搞清楚为什么 ssh 协议无法正常操作 GitHub 的 remote。

之前都没问题的,但现在操作总是超时,没有成功过:

% git clone --depth=1 git@github.com:TeslaCN/shardingsphere.git
Cloning into 'shardingsphere'...
ssh: connect to host github.com port 22: Connection timed out
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

检查我的 Git 配置,socks 代理经过验证仍然科学,感觉不是代理的问题。 .gitconfig

[user]
	signingkey = 55C1A242B0F517C4
	email = wuweijie@apache.org
	name = 吴伟杰
[commit]
	gpgsign = true
[http]
	proxy = socks://127.0.0.1:1089

既然用的是 ssh,能不能在 OpenSSH 工具上做点手脚?

Google 了一下找到了 stack overflow 的一个帖子: Connect with SSH through a proxy

可以对 github.com 这个 Host 使用 nc (netcat) 勾搭上我的 socks 代理。nc 应该也支持 https 协议的代理(本人环境是 Ubuntu 20.04,其他环境可以参考 stack overflow 的回答)。 在 .ssh/config 增加以下内容:

Host github.com
ProxyCommand nc -x 127.0.0.1:1089 %h %p

配置完后再试试:

% git clone --depth=1 git@github.com:TeslaCN/shardingsphere.git
Cloning into 'shardingsphere'...
remote: Enumerating objects: 15571, done.
remote: Counting objects: 100% (15571/15571), done.
remote: Compressing objects: 100% (8963/8963), done.
remote: Total 15571 (delta 6689), reused 9998 (delta 3544), pack-reused 0
Receiving objects: 100% (15571/15571), 43.18 MiB | 1.21 MiB/s, done.
Resolving deltas: 100% (6689/6689), done.

问题解决!