经常在命令行终端下工作的码农们,SS无法正常工作。因为在终端下不支持socks5代理,只支持http代理,这就很尴尬了。
wget、curl、git、brew等命令行工具都会变得很慢。苹果在新系统中加入了SIP安全机制会阻止第三方程序向系统目录内(/System,/bin,/sbin,/usr(除了/usr/local))进行写操作,sudo也不行。办法是先把SIP关了,等装好软件配置好后再打开SIP。或者改用其他软件。因为懒得去把SIP关了开开了关了,找了另外一个软件privoxy。它刚好就是安装在/usr/local内,不需要关闭SIP也可以正常使用。
安装很简单,用 brew 安装:
brew install privoxy
或
brew install --build-from-source privoxy
打开配置文件 /usr/local/etc/privoxy/config
:
加入下面两项配置:
listen-address 0.0.0.0:8118
forward-socks5 / localhost:1086 .
- 第一行设置 privoxy 监听任意IP地址的8118端口。
- 第二行设置本地socks5代理客户端端口。
- 注意不要忘了最后有一个空格和点号。
因为没有安装在系统目录内,所以启动时要打全路径。
sudo /usr/local/sbin/privoxy /usr/local/etc/privoxy/config
netstat -na | grep 8118
在命令行终端输入如下命令,该终端即可翻墙:
export http_proxy='http://localhost:8118'
export https_proxy='http://localhost:8118'
原理是将 socks5 代理转化成 http 代理给命令行终端使用。
如果不想使用了取消即可。
unset http_proxy
unset https_proxy
如果关闭了终端,功能就会失效,如果需要代理一直生效,则可以把上述两行代码添加到 ~/.bash_profile 文件最后。
iterm2 的话编辑 ~/.zshrc 文件即可。
vim ~/.bash_profile
export http_proxy='http://localhost:8118'
export https_proxy='http://localhost:8118'
还可以在 ~/.bash_profile 加入开关函数,使用起来更方便。
function proxy_off(){
unset http_proxy
unset https_proxy
echo -e "已关闭代理"
}
function proxy_on() {
export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
export http_proxy="http://127.0.0.1:8118"
export https_proxy=$http_proxy
echo -e "已开启代理"
}
“The first 90% of the code accounts for the first 90% of the development time. The remaining 10% of the code accounts for the other 90% of the development time.” – Tom Cargill
标 题:Mac命令行终端下使用shadowsocks翻墙