Git 使用代理
最近给 Git 配置代理,发现给 SSH 配置有认证的代理并不容易,竟然没找到直接的配置方法,结合一些工具和文档零零散散的也算是解决了,以下对 git 的各类代理配置总结。
Git 支持 http 和 ssh 两种传输协议,前者就是 git clone https://github.com/xxx/xxx
这种形式的 URL,后者是 git clone git@xxx.xxx.com/xxx.git
这种形式。由于是两种不同的协议,因此要分协议配置。
1. Git SSH 协议配置代理
Git SSH 的代理配置需要写在 ~/.ssh/config
中。需要结合 nc
命令(Windows 下 connect 命令)才能完成。由于 nc
命令不能静默完成认证操作,所以有认证的代理还需要用 ncat 命令。
1.1 有认证的代理
首先安装 ncat 工具。Debian、Ubuntu 直接 apt install nmap
即可,windows 也有对应版本。
Host github.com
ProxyCommand ncat --proxy proxy.server.com:port --proxy-type http --proxy-auth proxyUsername:proxyPassword %h %p
记得将代理的密码和用户名修改,配置完就可以愉快的使用代理了~
1.2 无认证的代理
修改 ~/.ssh/config
,添加如下内容。
Linux – HTTP Proxy
Host github.com
ProxyCommand nc -X connect -x proxy.server.com:port %h %p
Linux – Socks5 Proxy
Host github.com
ProxyCommand nc -X 5 -x proxy.server.com:port %h %p
Windows 可以使用 Git for Windows安装目录下的 connect 命令调用代理。
Windows – HTTP Proxy
Host github.com
ProxyCommand D:/Git/mingw64/bin/connect.exe -H proxy.server.com:port %h %p
Windows – Socks5 Proxy
Host github.com
ProxyCommand D:/Git/mingw64/bin/connect.exe -S proxy.server.com:port %h %p
2. Git HTTP/HTTPS 协议配置代理
使用 git 命令让全局使用代理:
git config --global http.proxy http://proxyUsername:proxyPassword@proxy.server.com:port
git config --global https.proxy http://proxyUsername:proxyPassword@proxy.server.com:port
对于有认证的代理,要设置认证方法为 basic
。
git config --global http.proxyAuthMethod 'basic'
否则会报 fatal: unable to access 'https://xxx': Proxy CONNECT aborted
的错误。
共有 0 条评论