MacOS代理分为桌面应用代理设置&Terminal代理设置,使用代理软件默认只会开启桌面应用代理,Terminal代理需要单独配置
桌面应用代理设置
Terminal查看桌面应用代理设置情况
scutil --proxy
返回内容(0表示未开启,1表示已开启,ExceptionsList 表示无需代理的地址列表)
<dictionary> {
HTTPEnable : 0
HTTPSEnable : 0
ProxyAutoConfigEnable : 0
SOCKSEnable : 0
}
<dictionary> {
ExceptionsList : <array> {
0 : 127.0.0.1
}
SOCKSEnable : 1
SOCKSPort : 1086
SOCKSProxy : 127.0.0.1
}
图形界面设置
Terminal代理设置(端口地址请根据实际情况修改)
临时使用代理(仅针对当前打开的Terminal窗口)
export http_proxy="http://127.0.0.1:1080"
export https_proxy="http://127.0.0.1:1080"
export all_proxy="socks5://127.0.0.1:1086"
永久使用代理(针对所有打开的Terminal窗口)
bash(配置~/.bash_profile)
在~/.bash_profile追加代理参数配置
echo export http_proxy="http://127.0.0.1:1080" >> ~/.bash_profile
echo export https_proxy="http://127.0.0.1:1080" >> ~/.bash_profile
echo export all_proxy="socks5://127.0.0.1:1086" >> ~/.bash_profile
使代理参数配置立即生效
source ~/.bash_profile
zsh(配置~/.zshrc)
在~/.zshrc追加代理参数配置
echo export http_proxy="http://127.0.0.1:1080" >> ~/.zshrc
echo export https_proxy="http://127.0.0.1:1080" >> ~/.zshrc
echo export all_proxy="socks5://127.0.0.1:1086" >> ~/.zshrc
使代理参数配置立即生效
source ~/.zshrc
测试代理是否成功(不能通过ping检测,ping不会走代理)
curl -I https://twitter.com
代理成功输出内容包含
status: 200 OK
代理失败输出内容
curl: (7) Failed to connect to 127.0.0.1 port 1080: Connection refused
查看当前代理参数配置
echo $http_proxy;
echo $https_proxy;
echo $all_proxy;
全局代理下自动设置Terminal脚本(注意:一定要先打开代理模式,而且必须是全局代理模式,再打开Terminal)
在~/.zshrc添加下列内容
export http_proxy=`scutil --proxy | awk ‘\ /HTTPEnable/ { enabled = $3; } \ /HTTPProxy/ { server = $3; } \ /HTTPPort/ { port = $3; } \ END { if (enabled == "1") { print "http://" server ":" port; } }‘` export http_proxy="${http_proxy}" export all_proxy=`scutil --proxy | awk ‘\ /SOCKSEnable/ { enabled = $3; } \ /SOCKSProxy/ { server = $3; } \ /SOCKSPort/ { port = $3; } \ END { if (enabled == "1") { print "socks5://" server ":" port; } }‘` export all_proxy="${all_proxy}" |
使配置立即生效
source ~/.zshrc
参考链接
https://www.serverlab.ca/tutorials/osx/administration-osx/configuring-a-network-proxy-for-osx/(How to Configure Network Proxy Settings for OSX)
https://www.cnblogs.com/DeviLeo/p/5798255.html(MacOS X Terminal中设置代理)
https://blog.kelu.org/tech/2017/06/19/setting-socks5-proxy.html(设置 socks5/http 代理,可用于git和shell终端)
https://www.hi-linux.com/posts/48321.html(通过 ProxyChains-NG 实现终端下任意应用代理)
http://www.voidcn.com/article/p-xtdshbdf-mc.html(Mac 为 Homebrew 设置代理)
原文地址:https://www.cnblogs.com/luoxiaolei/p/10851882.html