Mac电脑的终端命令行设置代理

方法一:使用 proxychains(推荐)
关闭 SIP 10.11 后下由于开启了 SIP 会导致命令行下 proxychains 代理的模式失效,如果你要使用 proxychains 这种简单的方法,就需要先关闭 SIP。
在恢复模式下,终端里输入 csrutil enable --without debug 来部分关闭 SIP,完整教程点击这里查看。
恢复模式重启进入系统后,终端里输入 csrutil status,结果中如果有 Debugging Restrictions: disabled 则说明关闭成功。
安装 Proxychains 安装好 Homebrew 后,终端中输入 brew install proxychains-ng
/usr/local/etc/proxychains.conf 中的配置替换为

strict_chain quiet_mode proxy_dns remote_dns_subnet 224 tcp_read_time_out 15000 tcp_connect_time_out 8000 localnet 127.0.0.0/255.0.0.0 localnet 10.0.0.0/255.0.0.0 localnet 172.16.0.0/255.240.0.0 localnet 192.168.0.0/255.255.0.0[ProxyList] http 127.0.0.1 1235

【Mac电脑的终端命令行设置代理】然后在需要走代理的命令前加上 proxychains4 即可,如
proxychains4 brew update

方法二:使用环境变量
大部分命令行程序支持使用 HTTP_PROXY/HTTPS_PROXY 的模式来设置代理,而且此模式不受 SIP 影响。具体每一个命令行程序支持那种模式的代理,需要查看他们的文档。
使用示例:
https_proxy="http://127.0.0.1:1235" http_proxy="http://127.0.0.1:1235" brew install git https_proxy="http://127.0.0.1:1235" http_proxy="http://127.0.0.1:1235" vagrant box update

为 Git 配置代理 Git 比较特殊,使用环境变量的方法并没有用,只有 proxychains 有效,如果不想使用 proxychains,可以对照本教程进行配置。
对于 HTTP/HTTPS 协议,比如 git clone https://github.com/github/hub.git,使用下面的命令为 github.com 域名配置代理。
git config --global http.https://github.com.proxy http://127.0.0.1:1235

对于 SSH 协议,比如 git clone git@github.com:github/hub.git,需要在文件 ~/.ssh/config 中添加
host github.com ProxyCommand /usr/bin/nc -X connect -x 127.0.0.1:1235 %h %p

相应的配置完成后,git clone 就会使用代理了
方法三:使用 Surge for Mac 的增强模式
我们默认的 Surge 规则已经做好了支持,只需要在 Surge 菜单中勾选「Enhanced Mode」即可。

    推荐阅读