Skip to content

Instantly share code, notes, and snippets.

@tizee
Last active May 20, 2020 02:36
Show Gist options
  • Select an option

  • Save tizee/c52dc95d4025a76cc0606a012d84a446 to your computer and use it in GitHub Desktop.

Select an option

Save tizee/c52dc95d4025a76cc0606a012d84a446 to your computer and use it in GitHub Desktop.
git clone over ssh tunneling

TOC

问题

A(本地计算机) <-> B(跳板服务器) <-> C(git repo仓库所在服务器)

我想要通过B来代理A发送给C的git clone请求而不是用ssh-agent转发。为什么A不直接向C发起请求呢?通常是出于安全或下载速度上的考虑,对于中文用户而言,一般都是GFW(笑,梯子还是没有ssh tunneling裸奔快。

解决办法

1. 沿用正常的ssh url

  1. 设置ssh tunnel
ssh -ND locahost:9999 [proxy_user]@[proxy_hostname] -p [port]
  1. 在.ssh/config设置需要通过ssh tunnel转发的服务器
# .ssh/config
Host github.com
  User git
  IdentityFile ~/.ssh/github_id_rsa
  ProxyCommand nc -x localhost:9999 %h %p
  1. git clone
git clone git@github.com:[username]/[reponame].git

2. 使用本地localhost作为 ssh url

  1. 在设置ssh tunnel时就设置好对应的git仓库服务器
ssh -NL 9999:github.com:22 [proxy_user]@[proxy_hostname]
  1. git clone
git clone ssh://git@localhost:9999/[username]/[reponame].git

这个更方便些(只需要两条命令,懒人福音)。

Question

A(local machine) <-> B(proxy server) <-> C(git repo server)

I would like to git clone the repo on local machine A instead of ssh-agent forwarding that clones the repo on proxy server B. Why not clone the repo directly on A? Well, for the sake of security or the network latency(download speed). For Chinese users, this is mainly due to GFW.

Solution

1. clone with normal ssh url

  1. setup a tunnel between localhost port and proxy server
ssh -ND locahost:9999 [name]@[hostname] -p [port]
  1. setup .ssh/config
# .ssh/config
Host github.com
  User git
  IdentityFile ~/.ssh/github_id_rsa
  ProxyCommand nc -x localhost:9999 %h %p
  1. git clone
git clone git@github.com:[username]/[reponame].git

Here we only proxy commands to github.com.

2. clone with localhost ssh url

  1. setup a tunnel between localhost port and proxy server that only forward conections to github.com:22
ssh -NL 9999:github.com:22 [proxy_user]@[proxy_hostname]
  1. git clone
git clone ssh://git@localhost:9999/[username]/[reponame].git
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment