Skip to content

Instantly share code, notes, and snippets.

@matinrco
Last active April 27, 2024 16:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matinrco/1c5214dd70156439ad03201daf4934c0 to your computer and use it in GitHub Desktop.
Save matinrco/1c5214dd70156439ad03201daf4934c0 to your computer and use it in GitHub Desktop.
Configure git/ssh to use socks/http proxy in windows, linux & wsl2

Windows

Add this to your ssh config file ( which is located in %USERPROFILE%\.ssh\config ) or create one if it's missing:

Http proxy & repo access via ssh:

Host *
    ProxyCommand "C:/Program Files/Git/mingw64/bin/connect.exe" -H {proxyserver}:{port} %h %p

or (for specific host name)

Host gitlab.com
    ProxyCommand "C:/Program Files/Git/mingw64/bin/connect.exe" -H {proxyserver}:{port} %h %p

Socks proxy & repo access via ssh:

Host *
    ProxyCommand "C:/Program Files/Git/mingw64/bin/connect.exe" -S {proxyserver}:{port} %h %p

or (for specific host name)

Host gitlab.com
    ProxyCommand "C:/Program Files/Git/mingw64/bin/connect.exe" -S {proxyserver}:{port} %h %p

GNU/Linux

Method 1:

install connect :

sudo apt install connect-proxy

then update ssh config file ( which is located in ~/.ssh/config ) or create one if it's missing :

Http proxy & repo access via ssh:

Host *
    ProxyCommand connect -H {proxyserver}:{port} %h %p

or (for specific host name)

Host gitlab.com
    ProxyCommand connect -H {proxyserver}:{port} %h %p

Socks proxy & repo access via ssh:

Host *
    ProxyCommand connect -S {proxyserver}:{port} %h %p

or (for specific host name)

Host gitlab.com
    ProxyCommand connect -S {proxyserver}:{port} %h %p

Method 2:

install proxychains :

sudo apt install proxychains-ng

create proxychains config in :

~/.proxychains/proxychains.conf

config file content (for http proxy & repo access via http/ssh):

strict_chain
proxy_dns
tcp_read_time_out 150000
tcp_connect_time_out 80000

[ProxyList]
http {proxyserver} {port}

config file content (for socks proxy & repo access via http/ssh):

strict_chain
proxy_dns
tcp_read_time_out 150000
tcp_connect_time_out 80000

[ProxyList]
socks5 {proxyserver} {port}

use proxychains to encapsulate git:

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

or

proxychains git clone path/to/repo.git

Method 3:

install socat :

sudo apt install socat

then update ssh config file ( which is located in ~/.ssh/config ) or create one if it's missing :

Http proxy & repo access via ssh:

Host *
    ProxyCommand socat - PROXY:{proxyserver}:%h:%p,proxyport={port}

or (for specific host name)

Host gitlab.com
    ProxyCommand socat - PROXY:{proxyserver}:%h:%p,proxyport={port}

WSL2 (Debian/Ubuntu)

If you want to use git inside wsl2 through proxy, GNU/Linux (Debian/Ubuntu) method works here too without any issue.

But, in my case, proxy server is running on my windows (out of wsl distro) and the problem is that localhost address inside wsl is not pointing to windows host. plus windows host ip address inside wsl can change dynamically with each startup. To fix this you need to read windows ip address from resolv.conf and here is the complete command with connect method ✨:

Http proxy & repo access via ssh:

Host *
    ProxyCommand connect -H `grep -m 1 nameserver /etc/resolv.conf | awk '{print $2}'`:{port} %h %p

Socks proxy & repo access via ssh:

Host *
    ProxyCommand connect -S `grep -m 1 nameserver /etc/resolv.conf | awk '{print $2}'`:{port} %h %p

Windows/WSL2/Linux

Use git cli:

Http proxy & repo access via http:

git config --global http.proxy http://{proxyserver}:{port}

or (for specific host name)

git config --global http.https://gitlab.com.proxy http://{proxyserver}:{port}

Note

Don't forget to replace {proxyserver} & {port} with your proxy address. e.g {proxyserver}:{port} => 127.0.0.1:1080

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment