咱们在终端应用Homebrewgitnpm等命令时,总会因为网络问题而装置失败。

尤其是装置Homebrew,据我理解很多敌人是花了很长时间来解决,心里不晓得吐槽该死的网络多少遍了。

尽管设置镜像的确有用,然而没有普适性,明天就介绍下让终端也走代理的办法,这样能够通杀很多状况。

文本提到的代理是指搭配Shadowsocks应用的状况,其他软件也有肯定共同之处。

macOS & Linux

通过设置http_proxyhttps_proxy,能够让终端走指定的代理。
配置脚本如下,在终端间接执行,只会长期失效:

export http_proxy=http://127.0.0.1:1080export https_proxy=$http_proxy

1080http代理对应的端口,请不要照抄作业,依据你的理论状况批改。

你能够在Shadowsocks的设置界面中查找代理端口信息。

便捷脚本

这里提供一个便捷脚本,外面蕴含关上、敞开性能:

function proxy_on() {    export http_proxy=http://127.0.0.1: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 << EOFfunction proxy_on() {    export http_proxy=http://127.0.0.1:1080    export https_proxy=$http_proxy    echo -e "终端代理已开启。"}function proxy_off(){    unset http_proxy https_proxy    echo -e "终端代理已敞开。"}EOFsource ~/.zprofile

能够执行curl cip.cc验证:

IP    : xxx地址    : 中国  台湾  台北市运营商    : cht.com.tw数据二    : 台湾省 | 中华电信(HiNet)数据中心数据三    : 中国台湾 | 中华电信URL    : http://www.cip.cc/xxx

看到网上说通过curl -I http://www.google.com可能会遇到403问题,应用Google域名验证时须要留神下这个状况。

Windows

依据网上的文章,在Windows下应用全局代理形式也会对cmd失效(未经验证)。

cmd

set http_proxy=http://127.0.0.1:1080set https_proxy=http://127.0.0.1:1080

还原命令:

set http_proxy=set https_proxy=

Git Bash

设置办法同"macOS & Linux"

PowerShell

$env:http_proxy="http://127.0.0.1:1080"$env:https_proxy="http://127.0.0.1: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.proxygit config --global --unset https.proxy

npm

# 设置npm config set proxy http://server:portnpm config set https-proxy http://server:port# 复原npm config delete proxynpm config delete https-proxy

git 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 %p

Windows

关上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

关注公众号:湖中剑,找到更多对于我的内容。