终端使用代理

实战

错误

1
Connection to api.telegram.org timed out. (connect timeout=5.0)

环境

macOS 13.0.1,使用 ClashX 1.96.1,ClashX 正常代理,使用自带终端。

配置

打开终端,输入一下代码即可开启代理。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cat > ~/.bash_profile << EOF
function proxy_on() {
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
echo -e "终端代理已开启。"
}
function proxy_off(){
unset http_proxy https_proxy all_proxy
echo -e "终端代理已关闭。"
}
EOF

source ~/.bash_profile

proxy_on

关闭终端代理,在终端输入 proxy_off

结果

1
2
3
4
5
6
7
8
9
10
11
curl -I http://www.google.com
HTTP/1.1 200 OK
Transfer-Encoding: chunked
Cache-Control: private
Connection: keep-alive
...


curl -I https://api.telegram.org
HTTP/1.1 200 Connection established
...

前言

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

尤其是安装 Homebrew,据我了解很多朋友是花了很长时间来解决,心里不知道吐槽该死的网络多少遍了。

虽然设置镜像确实有用,但是没有普适性,今天就介绍下让终端也走代理的方法,这样可以通杀很多情况。

文本提到的代理是指搭配 Clash 使用的情况,其他软件也有一定共同之处。

macOS & Linux

通过设置 http_proxyhttps_proxy,可以让终端走指定的代理。
配置脚本如下,在终端直接执行,只会临时生效:

1
2
3
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

7890http 代理对应的端口,请不要照抄作业,根据你的实际情况修改。

你可以在 Clash 的设置界面中查找代理端口信息。

便捷脚本

这里提供一个便捷脚本,里面包含打开、关闭功能:

1
2
3
4
5
6
7
8
9
10
function proxy_on() {
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
echo -e "终端代理已开启。"
}
function proxy_off(){
unset http_proxy https_proxy all_proxy
echo -e "终端代理已关闭。"
}

通过 proxy_on 启动代理,proxy_off 关闭代理。

接下来需要把脚本写入.bash_profile.zprofile,这样就可以永久生效。

你可能会问,怎么写入脚本,耐心点,下文提供了安装脚本的方法。

至于你应该写入哪个文件,请根据命令 echo $SHELL 返回结果判断:

  • /bin/bash => .bash_profile
  • /bin/zsh => .zprofile

然后执行安装脚本(追加内容 + 生效),注意一定根据要上面结果修改.bash_profile 名称:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
cat > ~/.bash_profile << EOF
function proxy_on() {
export http_proxy=http://127.0.0.1:7890
export https_proxy=\$http_proxy
echo -e "终端代理已开启。"
}

function proxy_off(){
unset http_proxy https_proxy
echo -e "终端代理已关闭。"
}
EOF

source ~/.bash_profile

打开代理

1
proxy_on

关闭代理

1
proxy_off

可以执行 curl cip.cc 验证:(clash 开全局)

1
2
3
4
5
6
7
8
9
10
curl cip.cc
IP : 85.203.21.9
地址 : 日本 东京都 东京
运营商 : falco-networks.com

数据二 : 日本 | 东京Choopa数据中心

数据三 : 日本东京都东京

URL : http://www.cip.cc/85.203.21.9

看到网上说通过 curl \-I http://www.google.com 可能会遇到 403 问题,使用 Google 域名验证时需要注意下这个情况。

Windows

根据网上的文章,在 Windows 下使用全局代理方式也会对 cmd 生效(未经验证)。

cmd

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

还原命令:

1
2
set http_proxy=
set https_proxy=

Git Bash

设置方法同 “macOS & Linux”

PowerShell

1
2
$env:http_proxy="http://127.0.0.1:1080"
$env:https_proxy="http://127.0.0.1:1080"

还原命令 (未验证):

1
2
$env:http_proxy=""
$env:https_proxy=""

其他代理设置

git 代理

1
2
3
4
5
6
7
# 设置
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

1
2
3
4
5
6
7
# 设置
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,如果没有这个文件,自己手动创建。

1
2
3
4
5
# 全局
# 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 文件,没有看到的话,同样手动创建。

1
2
3
4
5
# 全局
# ProxyCommand connect -S 127.0.0.1:1080 %h %p
# 只为特定域名设定
Host github.com
ProxyCommand connect -S 127.0.0.1:6600 %h %p

终端用了代理,还要给 git 单独设置代理吗?

git 有两种协议,一种 https,还有一种是 ssh 。

如果是用 https,设置终端代理即可。如果是 ssh,需要单独配置代理

参考文献

终端使用代理加速的正确方式(Shadowsocks) - SegmentFault 思否