关于终端:终端使用代理加速的正确方式Shadowsocks

5次阅读

共计 2156 个字符,预计需要花费 6 分钟才能阅读完成。

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

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

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

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

macOS & Linux

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

export http_proxy=http://127.0.0.1:1080
export 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 << EOF
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 "终端代理已敞开。"
}
EOF

source ~/.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:1080
set 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.proxy
git config --global --unset https.proxy

npm

# 设置

npm config set proxy http://server:port
npm config set https-proxy http://server:port

# 复原
npm config delete proxy
npm 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

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

正文完
 0