> ## Content Index
> Fetch the complete content index at: https://djdog.cc/llms.txt
> Use this file to discover other available public pages before exploring further.

# 在 Windows、Linux、Macos 的终端中使用代理
- URL: https://djdog.cc/blog/2024/terminal-proxy/
- Published: 2024-07-23T14:53:11.000Z
- Updated: 2024-10-22T04:00:00.000Z
- Description: 如何在常见的三种桌面系统的终端里使用代理
- Author: DJDOG
- Tags: Windows, Linux, #toc

这三种常见的桌面系统的终端都不会自动继承图形界面的系统代理，这里介绍一下在终端里使用代理的方法。

## Windows 系统

### 设置代理

```powershell
# 设置 HTTP 代理
set http_proxy=http://127.0.0.1:port

# 设置 HTTPS 代理
set https_proxy=http://127.0.0.1:port

```

### 验证代理

用 ping 命令是无法验证这里设置的终端代理的，因为 HTTP 代理只能代理基于 TCP 协议的 HTTP 或者 HTTPS 协议的流量。而 ping 命令用的是 icmp 协议。

所以这里使用 curl 命令来验证：

```powershell
curl -vv https://www.google.com

```

### 取消代理

```powershell
set http_proxy=
set https_proxy=

```

### 设置别名

可以通过设置别名来简化每次使用代理时的操作。

首先使用 `echo $PROFILE` 命令来确定 Powershell 的配置文件的位置。

打开输出的文件，按照如下格式编辑内容：

```powershell
function 别名 { 需要替代的命令 }

```

这里给出我的例子：

```powershell
function setproxy {
set http_proxy=http://127.0.0.1:port
set https_proxy=http://127.0.0.1:port 
}

function unsetproxy {
set http_proxy=
set https_proxy=
}

```

## Linux 和 Macos

Macos 终端默认使用 zsh，设置方式与 Linux 默认的 bash 类似，所以就放在一起来讲。

### 设置代理

```bash
# 设置 HTTP 代理
export http_proxy=http://127.0.0.1:port

# 设置 HTTPS 代理
export https_proxy=http://127.0.0.1:port

```

### 验证代理

使用 curl 命令来验证代理是否生效：

```bash
curl -vv https://www.google.com

```

### 取消代理

```bash
unset http_proxy
unset https_proxy

```

### 设置别名

可以通过设置别名来简化每次使用代理时的操作。

打开终端的配置文件（例如：`~/.bashrc` 或 `~/.zshrc`），添加如下内容：

```bash
alias setproxy='setproxy(){export http_proxy=http://127.0.0.1:port; export https_proxy=http://127.0.0.1:port;};setproxy'

alias unsetproxy='unsetproxy(){unset http_proxy; unset https_proxy;};unsetproxy'

```

记得替换 `port` 为实际的代理端口号。

[ Github:mkrl/misbrands](https://github.com/mkrl/misbrands?tab=CC0-1.0-1-ov-file&ref=djdog.cc)