git clone ssh repo 时使用 http 代理

方法一

$ sudo vi /etc/ssh/ssh_config

Host gerrit.example.com
    # ProxyCommand nc --proxy 192.168.5.2:3334 %h %p
    proxycommand socat - PROXY:192.168.5.2:%h:%p,proxyport=3334

方法二

$ vi git-ssh.sh
# ssh -o "proxycommand socat - PROXY:192.168.5.2:%h:%p,proxyport=3334" $*
# ssh -o StrictHostKeyChecking=no -o "proxycommand socat - PROXY:192.168.5.2:%h:%p,proxyport=3334" $* 
ssh -o StrictHostKeyChecking=no -F /dev/null -i /path/to/private_key -o "proxycommand socat - PROXY:192.168.5.2:%h:%p,proxyport=3334" $*

git-ssh.sh 脚本中的三种方法都是正确的,但通常选用第三种,选项的具体区别可以参考 ssh 的 man 手册页。

$ chmod +x git-ssh.sh
$ GIT_TRACE=1 GIT_SSH=./git-ssh.sh git clone ssh://runsisi@git.example.com:2222/ceph/ceph

注意:GIT_TRACE=1 环境变量只是用于打开 git 的调试输出而已。

这里使用导出 GIT_SSH 环境变量的方式执行 git clone,也可以使用导出 GIT_SSH_COMMAND 环境变量的方式,这两种方式的不同与 git 版本有关,请参考 使用指定 ssh 私钥访问 git 仓库,这里给出 git 版本为 2.10+ 的一种方式:

$ git config --global core.sshCommand 'ssh -o StrictHostKeyChecking=no -F /dev/null -i ~/.ssh/id_rsa -o "proxycommand socat - PROXY:192.168.5.2:%h:%p,proxyport=3334"'

注意这里不是以脚本方式运行,因此不需要像前面一样加上 $*

参考资料

使用指定 ssh 私钥访问 git 仓库

https://runsisi.com/2019/04/23/git-ssh-clone

Connect with SSH through a proxy

https://stackoverflow.com/questions/19161960/connect-with-ssh-through-a-proxy

Passing ssh options to git clone

https://stackoverflow.com/questions/7772190/passing-ssh-options-to-git-clone


最后修改于 2019-07-10