18

There are tons of identical solutions over the internet for defining proxy tunnel for git's downloads like this one, which all is by setting git's https.proxy & http.proxy config. but those answers are not working when you try to clone/push/pull etc. over the ssh protocol!

For example, by setting git config --global https.proxy socks5://127.0.0.1:9999 when you try to clone git clone [email protected]:user/repo.git it does not go through the defined sock5 tunnel!

I've tried various thing but none was working!

Question:

How to set git to use a local socks5 proxy (e.g. 127.0.0.1:9999) when it uses ssh connections?

5 Answers 5

38

There are 2 types to clone git: HTTP, and ssh. There are 2 common types of proxy: HTTP, and socks.

Here's the method dealing with 2 * 2 conditions:

# Method 1. git http + proxy http
git config --global http.proxy "http://127.0.0.1:1080"
git config --global https.proxy "http://127.0.0.1:1080"

# Method 2. git http + proxy shocks
git config --global http.proxy "socks5://127.0.0.1:1080"
git config --global https.proxy "socks5://127.0.0.1:1080"

# to unset
git config --global --unset http.proxy
git config --global --unset https.proxy

# Method 3. git ssh + proxy http
vim ~/.ssh/config
Host github.com
HostName github.com
User git
ProxyCommand socat - PROXY:127.0.0.1:%h:%p,proxyport=1087

# Method 4. git ssh + proxy socks
vim ~/.ssh/config
Host github.com
HostName github.com
User git
ProxyCommand nc -v -x 127.0.0.1:1080 %h %p

Sign up to request clarification or add additional context in comments.

5 Comments

I know this because it's necessary for Chinese guys, hh. notion.so/…
Use Method 4 succeeded!
For method 3 & method 4, do NOT change the 'Host github.com' part or you will lose 10 mins to debug as I did.
You can't do metod 4 if your proxy requires authentication, but you can use ncat as follow: ProxyCommand ncat --proxy <proxy>:<port> --proxy-type socks5 --proxy-auth <user>:<pass> %h %p
for all windows platform guys. Use <code>ProxyCommand connect -S localhost:1081 %h %p</code> instead.
15

After some visiting so many pages, I finally find the solution to my question:

# [step 1] create a ssh-proxy
  ssh -D 9999 -qCN [email protected]

# [step 2] make git connect through the ssh-proxy
  # [current script only]
  export GIT_SSH_COMMAND='ssh -o ProxyCommand="connect -S 127.0.0.1:9999 %h %p"'
  # OR [git global setting] 
  git config --global core.sshCommand 'ssh -o ProxyCommand="connect -S 127.0.0.1:9999 %h %p"'
  # OR [one-time only use]
  git clone -c=core.sshCommand 'ssh -o ProxyCommand="connect -S 127.0.0.1:9999 %h %p"' [email protected]:user/repo.git
  # OR [current repository use only]
  git config core.sshCommand 'ssh -o ProxyCommand="connect -S 127.0.0.1:9999 %h %p"'

To install connect on Ubuntu:

sudo apt install connect-proxy

3 Comments

It says kex_exchange_identification: Connection closed by remote host. Connection closed by UNKNOWN port 65535
Verify that you can actually connect yo the server through a normal ssh
This is the only solution that worked for me, had to install the connect package on arch linux and set this ProxyCommand directly in the .ssh/config for the domain I needed it for. For some reason, my nc does not work like nc on other distros...
9

The previous answers may work but I find them overly complex.

The most common case should be to access a corporate git server over a SOCKS5 proxy. In this example:

  • Git server: git.evilcorp.com
  • SOCKS5 proxy: localhost:11080

The easiest way to configure git in this case is to have a nice SSH config for the git server (~/.ssh/config):

Host git.evilcorp.com
  # Identity file specifies wich SSH key used to access the git server.
  Identityfile ~/.ssh/id_rsa
  # ProxyCommand does the magic to access the proxy server.
  ProxyCommand /bin/nc -X 5 -x 127.0.0.1:11080 %h %p

Cool detail: The DNS resolution is done by the proxy, so your machine doesn't need to know about the corp DNS servers.

Comments

5

git's own git --config 'http.proxy=socks5://127.0.0.1:4444' or ssh_config's proxycommand using socat and nc techniques successfully reroute git commands over the SOCKS proxy.

The problem could be, however, that the DNS lookup fails due to the lookup being done locally rather than remotely over over the SOCKS proxy. For example, in the case of an intranet, the servers may not have domain names on the internet (only known to intranet DNS).

There are two ways to solve this. The first one I've seen recommended several times, but it makes the ambitious assumption that you control the remote server.

  1. Reroute local 53 traffic (DNS) to port X, forward that to the server at port Y, forward port Y to 53 on the server. Usually X=Y for simplicity.

  2. Intercept local system calls that retrieve ip addresses for names.

I would argue that 2. is better, because it catches the problem at the source and only assumes that you control your local machine, which is a more likely case than controlling the remote server.

proxychains-ng intecepts the getaddrinfo system call before it accesses your local nss.

Steps

Open a SOCKS5 port

ssh -v -NT -D 127.0.0.1:4444 intranethost

Setup proxychains to use that port.

~/.proxychains/proxychains.conf

strict_chain
proxy_dns
tcp_read_time_out 150000
tcp_connect_time_out 80000

[ProxyList]
socks5 127.0.0.1 4444

Use proxychains to encapsulate git.

alias gitproxy='proxychains git'
gitproxy clone intranethost:path/to/repo.git

The beauty of taking this route is that this is all transparent to git and its remotes.

3 Comments

On the proxy the DNS Server 4.2.2.2 is used by default to work around this, use the following alias: alias gitproxy='PROXYRESOLV_DNS=$IP_OF_YOUR_DNS_SERVER proxychains git'
If DNS is the problem, just use socks5h to use remote dns resolution: git --config 'http.proxy=socks5h://127.0.0.1:4444'
@PhillyTheThrilly Nice one. I did not know about socks5h. Your comment seems like it should be an answer.
2

You need to define the GIT_SSH_COMMAND environment variable first

In it, you can redefine the ssh command, in order to use your socks proxy setting

ssh -D $port_number $hostname
# or
ssh -D $port_number $username@$hostname

Or using a proxycommand nc (or ncat on Windows)

The point is: once ssh is working with your socks5 proxy, you can define the same syntax in GIT_SSH_COMMAND, and Git will use the right ssh command.

You can also test it with a local configuration:

git -c core.sshCommand='ssh -D 9998 [email protected]' git pull
git -c core.sshCommand='ssh -D 9999 127.0.0.1' git pull

Git 2.46 (Q3 2024), rc0 batch 2 adds on the topic:

See commit 70405ac, commit 804ecbc, commit c98f78b, commit 2101341 (09 Jul 2024) by brian m. carlson (bk2204).
(Merged by Junio C Hamano -- gitster -- in commit d6c8636, 16 Jul 2024)

gitfaq: add documentation on proxies

Signed-off-by: brian m. carlson

Many corporate environments and local systems have proxies in use.
Note the situations in which proxies can be used and how to configure them.
At the same time, note what standards a proxy must follow to work with Git.
Explicitly call out certain classes that are known to routinely have problems reported various places online, including in the Git for Windows issue tracker and on Stack Overflow, and recommend against the use of such software, noting that they are associated with myriad security problems (including, for example, breaking sandboxing and image integrity (chromium issue 40285192), and, for TLS middleboxes, the use of insecure protocols and ciphers and lack of certificate verification (PDF)).
Don't mention the specific nature of these security problems in the FAQ entry because they are extremely numerous and varied and we wish to keep the FAQ entry relatively brief.

gitfaq now includes in its man page:

Can I use a proxy with Git?

Yes, Git supports the use of proxies. Git honors the standard http_proxy, https_proxy, and no_proxy environment variables commonly used on Unix, and it also can be configured with http.proxy and similar options for HTTPS (see git config).
The http.proxy and related options can be customized on a per-URL pattern basis.
In addition, Git can in theory function normally with transparent proxies that exist on the network.

For SSH, Git can support a proxy using OpenSSH's ProxyCommand. Commonly used tools include netcat and socat. However, they must be configured not to exit when seeing EOF on standard input, which usually means that netcat will require -q and socat will require a timeout with something like -t 10.
This is required because the way the Git SSH server knows that no more requests will be made is an EOF on standard input, but when that happens, the server may not have yet processed the final request, so dropping the connection at that point would interrupt that request.

An example configuration entry in ~/.ssh/config with an HTTP proxy might look like this:

Host git.example.org
User git
ProxyCommand socat -t 10 - PROXY:proxy.example.org:%h:%p,proxyport=8080

Note that in all cases, for Git to work properly, the proxy must be completely transparent.
The proxy cannot modify, tamper with, or buffer the connection in any way, or Git will almost certainly fail to work.

Note that many proxies, including many TLS middleboxes, Windows antivirus and firewall programs other than Windows Defender and Windows Firewall, and filtering proxies fail to meet this standard, and as a result end up breaking Git.
Because of the many reports of problems and their poor security history, we recommend against the use of these classes of software and devices.

8 Comments

Thanks for the answer, I couldn't come up with you solution, Let's say I have host and user to set up my socks5 proxy with sh -D 9999 -C -N [email protected]; with your instructions I have tried the followings: GIT_SSH_COMMAND='ssh -D 9998 [email protected]' git pull (with no proxy tunnel running in the system) and GIT_SSH_COMMAND='ssh -D 9999 127.0.0.1' git pull (when a proxy is running at port: 9999 at localhost) none was success. Maybe I'm doing it wrong, can you be kind enough to write a copy & past solution? [The connection with none of the above approaches was got through the proxy!]
What is you OS, OS version, shell, shell version, Git version?
Os: Ubuntu 18.04 shell: GNU bash, version 4.4.20 git: 2.23.0
@dariush Does your ssh work through the proxy, without using Git?
just updated my comment, mistakenly press Enter for new-line and it submitted the comment
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.