
我们在终端使用Homebrew、git、npm等命令时,总会因为网络问题而安装失败。
尤其是安装Homebrew,据我了解很多朋友是花了很长时间来解决,心里不知道吐槽该死的网络多少遍了。
虽然设置镜像确实有用,但是没有普适性,今天就介绍下让终端也走代理的方法,这样可以通杀很多情况。
文本提到的代理是指搭配Shadowsocks使用的情况,其他软件也有一定共同之处。
目录
macOS & Linux
通过设置http_proxy、https_proxy,可以让终端走指定的代理。 配置脚本如下,在终端直接执行,只会临时生效:
export http_proxy=:1080 export https_proxy=$http_proxy1080是http代理对应的端口,请不要照抄作业,根据你的实际情况修改。
你可以在Shadowsocks的设置界面中查找代理端口信息。
便捷脚本
这里提供一个便捷脚本,里面包含打开、关闭功能:
function proxy_on() { export http_proxy=:1080 export https_proxy=$http_proxy echo -e "终端代理已开启。" } function proxy_off(){ unset http_proxy https_proxy echo -e "终端代理已关闭。" }通过proxy_on启动代理,proxy_off关闭代理。
接下来需要把脚本写入.bash_profile或.zprofile,这样就可以永久生效。
你可能会问,怎么写入脚本,耐心点,下文提供了安装脚本的方法。至于你应该写入哪个文件,请根据命令echo $SHELL返回结果判断:
/bin/bash => .bash_profile/bin/zsh=> .zprofile然后执行安装脚本(追加内容+生效),注意一定根据要上面结果修改.zprofile名称:
cat > ~/.zprofile << EOF function proxy_on() { export http_proxy=:1080 export https_proxy=$http_proxy echo -e "终端代理已开启。" } function proxy_off(){ unset http_proxy https_proxy echo -e "终端代理已关闭。" } EOF source ~/.zprofile可以执行curl cip.cc验证:
IP : xxx 地址 : 中国台湾台北市 运营商 : cht.com.tw 数据二 : 台湾省 | 中华电信(HiNet)数据中心 数据三 : 中国台湾 | 中华电信 URL :看到网上说通过curl -I 可能会遇到403问题,使用Google域名验证时需要注意下这个情况。
Windows
根据网上的文章,在Windows下使用全局代理方式也会对cmd生效(未经验证)。
cmd
set http_proxy=:1080 set https_proxy=:1080还原命令:
set http_proxy= set https_proxy=Git Bash
设置方法同"macOS & Linux"
PowerShell
$env:http_proxy=":1080" $env:https_proxy=":1080"还原命令(未验证):
$env:http_proxy="" $env:https_proxy=""其他代理设置
git代理
# 设置 git config --global http.proxy socks5://127.0.0.1:1080 git config --global https.proxy socks5://127.0.0.1:1080 # 恢复 git config --global --unset http.proxy git config --global --unset https.proxynpm
# 设置 npm config set proxy :port npm config set https-proxy :port # 恢复 npm config delete proxy npm config delete https-proxygit clone ssh 如何走代理
macOS
打开~/.ssh/config,如果没有这个文件,自己手动创建。
# 全局 # ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %p # 只为特定域名设定 Host github.com ProxyCommand nc -X 5 -x 127.0.0.1:1080 %h %pWindows
打开C:\Users\UserName\.ssh\config文件,没有看到的话,同样手动创建。
# 全局 # ProxyCommand connect -S 127.0.0.1:1080 %h %p # 只为特定域名设定 Host github.com ProxyCommand connect -S 127.0.0.1:6600 %h %p