Published on

使用 socks5 代理 git ssh 协议

Authors

由于众所周知的原因,GitHub 在国内并不能很流畅的访问,为了提高访问的稳定性,一般通过代理的方式来访问。

GitHub 一般通过 HTTPS 或者 SSH 协议来访问。

如果是通过 HTTPS 协议访问,代理配置比较简单,直接在用户根目录下的.gitconfig配置文件中加入:

[http]
        proxy = socks5://127.0.0.1:<Your Port>

如果通过 SSH 协议,稍微有点麻烦,如果是 Windows 系统,需要使用 connect 这个工具,在 Windows 版的 git 安装包里有提供这个工具,如果是类 UNIX 系统,可以使用 nc 这个工具。

Windows 系统在 .ssh\config 下写入:

Host github.com
 ProxyCommand "<Your Connect Path>\connect" -S 127.0.0.1:<Your Port> %h %p
 IdentityFile "<Your Private Key Path>\id_ed25519"
 TCPKeepAlive yes
 IdentitiesOnly yes
 User git
 Port 22
 Hostname github.com

-S代表走 SOCKS5 协议,如果是走 HTTP 协议,需要改成 -H

UNIX Like 系统,打开 ~/.ssh/config,写入如下配置:

Host github.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:<Your Port> %h %p