【解决】Mac 终端无法使用代理

7,526 阅读1分钟

描述

  • 现象:已经使用 ClashX 开启系统代理,在终端中运行 sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)" 下载 oh-my-zsh 时,发现报错:Failed to connect to raw.githubusercontent.com,使用 git 安装时,也报错:unable to connect to github.com。
  • 定位:Mac 的终端默认没有开启代理,即使使用了 Clash 客户端开启系统代理,也无法在终端使用代理,导致出现网络问题无法下载。

解决

1、在 Clash 中点击 copy shell command,或者其他代理软件中的类似功能。

export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890

2.1、对于 zsh 终端,打开 .zshrc 文件,对于 bash 同理打开 .bash_profile,粘贴上述命令后保存退出,不要忘了 source .zshrc 使配置生效。

2.2、如果希望能够方便地控制终端是否使用代理,那么区别于 2.1,可以做如下配置设置 alias:

alias proxyOn='export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890' alias proxyOff='unset https_proxy;unset http_proxy;unset all_proxy;'

这样一来,我可以随时在终端中输入 proxyOn 开启代理,输入 proxyOff 关闭代理。

2.3、也可以将 proxyOn, proxyOff 作为函数执行,添加打印提示到终端的功能。

function proxyOn() { 
    export https_proxy=http://127.0.0.1:7890 
    export http_proxy=http://127.0.0.1:7890 
    export all_proxy=socks5://127.0.0.1:7890 
    # \n 换行是为了避免输出末尾出现 $ 符号 
    printf "Proxy is on.\n" 
} 
function proxyOff () {
    unset https_proxy 
    unset http_proxy 
    unset all_proxy 
    printf "Proxy is off.\n" 
}

和 alias 的使用类似,在终端中输入 proxyOn 开启代理,输入 proxyOff 关闭代理,还会回显信息。

3、使用 curl ipinfo.io 命令查看代理的开启情况。