终端如何走代理

阅读须知

  • 对于不同操作系统的终端,命令不同。因此,本文接下来分别对于Linux (主要)、MacOS、Windows下的配置方式做出整理。

    • 由于Windows下,可以依靠git bash执行代理相关的Linux终端操作,故对于cmdpowerShell等命令执行器使用较少,故建议使用Linux/MacOS的方式即可,并且本文涉及介绍Linux中相关的配置可放心观看(因为基于macos的我没有验证)。
    • 说个题外话,Windows下的开源项目Windows终端使用体验不错,配合上述git bash工具一起使用,体验更加,可参考文章windows终端的初步使用。因此,最终的整理结果就是,win下使用windows终端+git bash, 使用Linux的代理配置即可。
  • 不论是Linux发行版中的apt、yum等, python的pip, node的npm……这些在终端中使用的软件我们在没有代理的情况下, 一般都会给其配置国内的镜像源, 毫无疑问,这是指标不治本的做法。 所以还是更推荐配置代理的方式来使用它们, 既然看到了这篇文章, 就默认你已经有了可用的代理服务, 只是不知如何在终端中配置, 这是本文解决的问题。

  • 一般的代理协议:

    • http -> 配置格式如下
      • http://user:password@proxy.server:port/
        • user:password:如果需要密码,这里就填用户名和密码,否则为空
        • proxy.server:代理地址
        • port:代理端口号
    • socks -> 配置格式如下
      • socks5://user:password@proxy.server:port/
        • user:password:如果需要密码,这里就填用户名和密码,否则为空
        • proxy.server:代理地址
        • port:代理端口号

          注意: 这里关于代理的格式是通用的(虽然文中不同可能出现不同的英文表达方式),但本质相同。并且文中大多地方为了方便书写,省略了user:password@(一般很少有人给自己代理设置这些),但有的时候参考这里知道格式如何写就行。

  • 检测当前是否处于代理状态

    1
    2
    curl 是常用的命令行工具,用来请求 Web 服务器。它的名字就是客户端(client)的 URL 工具的意思。
    它的功能非常强大,命令行参数多达几十种。如果熟练的话,完全可以取代 Postman 这一类的图形界面工具。
    • 利用curl,对提供相关服务的链接发送GET请求, 如 cip.cc 、 ip.gs等网站,来获得当前终端的原iphttp/https的代理ip

      1
      2
      3
      4
      $ curl cip.cc               ->      原ip

      $ curl ip.gs -> http 的代理ip
      $ curl https://ip.gs -> https 的代理ip
    • 利用curl,直接请求www.google.com,看是否正常返回

      1
      2
      3
      $ curl www.google.com

      $ curl https://www.google.com

Linux终端代理设置

配置终端中http/https代理

MacOS下的配置方法与当前所介绍的LinuxLinux一致, windows下可通过git bash使用。也就是此此操作说win,linux,MacOS通用

操作命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 打开
## http
export http_proxy="http://proxyAddress:port"
export https_proxy="http://proxyAddress:port"
## socket
export http_proxy="socks5://proxyAddress:port"
export https_proxy="socks5://proxyAddress:port"
## 设置全部设置代理 -> 这里全部指http/https
export ALL_PROXY="http://proxyAddress:port"
export ALL_PROXY="socks5://proxyAddress:port"

# 关闭
## http
unset http_proxy
## https
unset https_proxy
## http/https
unset ALL_PROXY
  • apt命令或yum命令等之类的下载命令,不会通过上述环境变量来代理,需要配置他们专有的自身配置文件(看到这里大家应该也不难理解,毕竟我们只是配置了终端对于http/https通信协议的代理)

  • 以上输入命令的方式配置的http/https代理,只作用于当前终端中,不会影响整体环境

  • 若是直接写入系统配置文件.bashrc或者.zshrc能够一直保留

  • 通过在.bashrc或者.zshrc中设置alias可以简化操作:

    1
    2
    3
    4
    5
    # 打开
    alias setproxy="export ALL_PROXY=http://proxyAddress:port"
    alias setproxy="export ALL_PROXY=socks5://proxyAddress:port"
    # 关闭
    alias unsetproxy="unset ALL_PROXY"

TIPS:

  • 需要注意的是, linux中<包括一些为多平台适配linux的终端(如gitbash)中>, 通常不会默认适配socks5的代理模式, 因此这些场景下优先使用http代理(此时socks代理不可用, 配置了也无效)。
  • 对于直接为终端配置的代理, 最直接的用途就是 curlwinget等工具使用时需要代理的场景。(如在配置neovim时, 某些lsp的依赖(如blink、mason等)需要通过curl来自动下载, 此时如果curl无法通过代理访问一些域外网站, 就会造成下载失败的报错, 进而影响编码体验。)

配置apt的代理

若安装时配置了代理信息,则不用手动配置; 否则,需手动更改配置文件。

1、通过创建配置文件,配置apt代理

1
2
3
4
5
6
7
8
9
10
# 1.创建并编辑配置文件
sudo vim /etc/apt/apt.conf.d/proxy.conf
# 2.文件中添加以下格式内容,配置代理
Acquire::http::Proxy "http://user:password@proxy.server:port/";
Acquire::https::Proxy "http://user:password@proxy.server:port/";
# 2.或添加以下格式内容,配置代理
Acquire {
HTTP::proxy "http://user:password@proxy.server:port/";
HTTPS::proxy "http://user:password@proxy.server:port/";
}

2、通过直接编辑/etc/apt/apt.conf文件, 配置apt代理

1
2
3
4
5
# 编辑配置文件
sudo vim /etc/apt/apt.conf
# 末尾加上
Acquire::http::Proxy "http://user:password@proxy.server:port/"
Acquire::https::Proxy "http://user:password@proxy.server:port/";

配置git的代理

1
2
3
4
5
6
7
8
9
# 当前
git config http.proxy 'socks5://127.0.0.1:1080'
git config https.proxy 'socks5://127.0.0.1:1080'
# 全局
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
8
9
10
11
12
13
14
# 查看
npm config get proxy
npm config get https-proxy

# 设置代理
npm config set proxy http://server:port
npm config set https-proxy http://server:port

# 删除代理
npm config set proxy null
npm config set https-proxy null

npm config delete proxy
npm config delete https-proxy

配置pip代理

1
2
3
## --proxy="ip:port"的位置可以很随意,但要在`pip install`之后
pip install --proxy="proxyAddress:port" yourpackage
pip install yourpackage --proxy="proxyAddress:port"

curl命令单独走代理

这里只举socks5代理的使用例子: curl -x socks5h://localhost:7448 http://www.google.com/

这种方式不像为终端或git配置时那样需要单独直到http或https, 这个参数在执行时是通用的。

ProxyChains的使用 (未验证)

ProxyChains强制使给定程序发起的TCP连接通过事先配置的代理。可以涵盖所有需要代理的情景。

对于任何一个需要代理的命令行程序,直接在运行的指令前面加proxychains即可实现代理。

安装

1
sudo apt install proxychains4
  • 默认安装proxychains是第三版的

配置

proxychains 的配置文件顺序是当前目录下的 ./proxychains.conf 然后是 $HOME/.proxychains/proxychains.conf 最后是系统目录下的 /etc/proxychains.conf

1
vim /etc/proxychains4.conf
  • 添加对应的代理配置,其格式如下:
1
2
3
4
##代理列表开始
[ProxyList]
##[]为可选内容
type host port [user pass]
  • 注释远程DNS解析,防止DNS污染风险
1
2
# 注释下面这行
proxy_dns

windows终端代理设置

cmd:

1
2
set http_proxy=http://proxyAddress:port
set https_proxy=http://proxyAddress:port

还原命令:

1
2
set http_proxy=
set https_proxy=

PowerShell:

1
2
$env:http_proxy="http://proxyAddress:port"
$env:https_proxy="http://proxyAddress:port"

还原命令(未验证):

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