$ 发布 2026-07-25 | ~4分钟阅读
语境: 2026年最新Linux终端代理与脚本配置完全指南,涵盖环境变量配置、代理工具、自动化脚本、快捷切换等,让你的Linux终端翻墙更高效。

Linux 终端代理与脚本配置完全指南 2026


输出
延迟估计 ~216 ms 置信度 ~0.89

前言

Linux 用户大多通过终端操作,如何在终端中高效配置代理?环境变量怎么设置?如何让所有命令都走代理?本文详细介绍Linux终端代理的各种配置方法,包括环境变量、代理工具、自动化脚本、快捷切换等,让你的Linux终端翻墙体验更加顺畅。


一、环境变量配置

1.1 基础环境变量

设置代理:

# HTTP 代理
export http_proxy=http://127.0.0.1:7890
export HTTP_PROXY=http://127.0.0.1:7890

# HTTPS 代理
export https_proxy=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890

# FTP 代理
export ftp_proxy=http://127.0.0.1:7890
export FTP_PROXY=http://127.0.0.1:7890

# SOCKS5 代理
export all_proxy=socks5://127.0.0.1:7891
export ALL_PROXY=socks5://127.0.0.1:7891

# 测试
curl https://www.google.com
wget https://www.google.com

取消代理:

unset http_proxy https_proxy ftp_proxy all_proxy
unset HTTP_PROXY HTTPS_PROXY FTP_PROXY ALL_PROXY

1.2 永久配置

方法一:修改 shell 配置文件

# ~/.bashrc 或 ~/.zshrc

# 添加代理函数
proxy_on() {
    export http_proxy=http://127.0.0.1:7890
    export https_proxy=http://127.0.0.1:7890
    export all_proxy=socks5://127.0.0.1:7891
    echo "✅ Proxy enabled"
}

proxy_off() {
    unset http_proxy https_proxy all_proxy
    echo "❌ Proxy disabled"
}

proxy_status() {
    if [ -n "$http_proxy" ]; then
        echo "🔵 Proxy: $http_proxy"
    else
        echo "⚪ Proxy: disabled"
    fi
}

使用:

source ~/.zshrc

proxy_on
proxy_off
proxy_status

方法二:创建独立配置文件

# ~/.proxyrc

http_proxy=http://127.0.0.1:7890
https_proxy=http://127.0.0.1:7890
all_proxy=socks5://127.0.0.1:7891
# 在 ~/.zshrc 中添加
source ~/.proxyrc

二、代理工具

2.1 proxychains-ng

安装:

# Debian/Ubuntu
apt install proxychains4

# CentOS/RHEL
yum install proxychains-ng

# macOS
brew install proxychains-ng

# 源码安装
git clone https://github.com/rofl0r/proxychains-ng.git
cd proxychains-ng
./configure --prefix=/usr --sysconfdir=/etc
make && make install

配置:

# /etc/proxychains.conf 或 ~/.proxychains/proxychains.conf

[ProxyList]
socks5 127.0.0.1 7891
# http 127.0.0.1 7890

使用:

# 单个命令
proxychains curl https://www.google.com
proxychains wget https://example.com/file.zip
proxychains git clone https://github.com/user/repo.git
proxychains docker pull ubuntu

# 进入代理 shell
proxychains bash

2.2 tsocks

安装:

# Debian/Ubuntu
apt install tsocks

# CentOS/RHEL
yum install tsocks

配置:

# /etc/tsocks.conf

server = 127.0.0.1
server_port = 7891
server_type = 5

使用:

tsocks curl https://www.google.com

2.3 goproxy

安装:

go install github.com/snail007/goproxy@latest

使用:

# 启动代理服务器
goproxy http -p ":8080"

# 作为客户端
export http_proxy=http://127.0.0.1:8080

三、应用级代理配置

3.1 Git 代理

配置:

# 全局配置
git config --global http.proxy http://127.0.0.1:7890
git config --global https.proxy http://127.0.0.1:7890

# 使用 SOCKS5
git config --global http.proxy socks5://127.0.0.1:7891
git config --global https.proxy socks5://127.0.0.1:7891

# 取消配置
git config --global --unset http.proxy
git config --global --unset https.proxy

# 仅对特定仓库
git config http.proxy http://127.0.0.1:7890

3.2 Docker 代理

配置:

# 创建配置文件
mkdir -p /etc/systemd/system/docker.service.d

cat > /etc/systemd/system/docker.service.d/http-proxy.conf << EOF
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:7890"
Environment="HTTPS_PROXY=http://127.0.0.1:7890"
Environment="NO_PROXY=localhost,127.0.0.1"
EOF

# 重启 Docker
systemctl daemon-reload
systemctl restart docker

# 验证
docker info | grep Proxy

3.3 npm/yarn/pnpm 代理

npm:

# 设置代理
npm config set proxy http://127.0.0.1:7890
npm config set https-proxy http://127.0.0.1:7890

# 使用镜像
npm config set registry https://registry.npmmirror.com

# 取消代理
npm config delete proxy
npm config delete https-proxy

yarn:

# 设置代理
yarn config set proxy http://127.0.0.1:7890
yarn config set https-proxy http://127.0.0.1:7890

# 使用镜像
yarn config set registry https://registry.npmmirror.com

pnpm:

# 设置代理
pnpm config set proxy http://127.0.0.1:7890
pnpm config set https-proxy http://127.0.0.1:7890

# 使用镜像
pnpm config set registry https://registry.npmmirror.com

3.4 pip 代理

配置:

# 临时设置
pip install requests --proxy http://127.0.0.1:7890

# 永久设置
cat > ~/.pip/pip.conf << EOF
[global]
proxy = http://127.0.0.1:7890
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
EOF

3.5 cargo 代理

配置:

cat > ~/.cargo/config.toml << EOF
[http]
proxy = "http://127.0.0.1:7890"

[registry]
index = "https://mirrors.tuna.tsinghua.edu.cn/crates.io-index"
EOF

四、自动化脚本

4.1 代理切换脚本

创建脚本:

#!/bin/bash
# ~/bin/proxy

PROXY_HOST="127.0.0.1"
HTTP_PORT="7890"
SOCKS_PORT="7891"

case "$1" in
    on)
        export http_proxy="http://$PROXY_HOST:$HTTP_PORT"
        export https_proxy="http://$PROXY_HOST:$HTTP_PORT"
        export all_proxy="socks5://$PROXY_HOST:$SOCKS_PORT"
        export HTTP_PROXY="$http_proxy"
        export HTTPS_PROXY="$https_proxy"
        export ALL_PROXY="$all_proxy"
        echo "✅ Proxy enabled"
        ;;
    off)
        unset http_proxy https_proxy all_proxy
        unset HTTP_PROXY HTTPS_PROXY ALL_PROXY
        echo "❌ Proxy disabled"
        ;;
    status)
        if [ -n "$http_proxy" ]; then
            echo "🔵 HTTP: $http_proxy"
            echo "🔵 SOCKS: $all_proxy"
        else
            echo "⚪ Proxy is disabled"
        fi
        ;;
    *)
        echo "Usage: proxy {on|off|status}"
        exit 1
        ;;
esac

使用:

chmod +x ~/bin/proxy

proxy on
proxy off
proxy status

4.2 自动检测脚本

脚本示例:

#!/bin/bash

# 检测网络环境
function detect_network() {
    # 测试访问 Google
    if curl -s --connect-timeout 3 https://www.google.com > /dev/null; then
        return 0  # 国外网络
    else
        return 1  # 国内网络
    fi
}

# 根据网络环境设置代理
if detect_network; then
    echo "🌍 Foreign network detected, no proxy needed"
    proxy off
else
    echo "🇨🇳 China network detected, enabling proxy"
    proxy on
fi

4.3 定时检查脚本

使用 cron:

# 编辑 cron
crontab -e

# 添加定时任务(每5分钟检查一次)
*/5 * * * * /path/to/detect-proxy.sh

# 查看 cron 日志
grep CRON /var/log/syslog

五、快捷切换方案

5.1 Alias 别名

在 ~/.zshrc 中添加:

alias px='proxy'
alias pxon='proxy on'
alias pxoff='proxy off'
alias pxst='proxy status'

使用:

pxon
pxoff
pxst

5.2 fzf 快速切换

安装 fzf:

brew install fzf  # macOS
apt install fzf   # Debian/Ubuntu

创建切换函数:

# ~/.zshrc

function px() {
    local options=("on" "off" "status")
    local selected=$(printf "%s\n" "${options[@]}" | fzf --prompt="Proxy: ")

    case $selected in
        on) proxy on ;;
        off) proxy off ;;
        status) proxy status ;;
    esac
}

5.3 终端标题显示代理状态

在 ~/.zshrc 中添加:

# 自定义终端标题
function precmd() {
    local proxy_icon=""
    if [ -n "$http_proxy" ]; then
        proxy_icon="🔒 "
    fi
    echo -ne "\e]0;${proxy_icon}$(hostname)\a"
}

六、系统级代理

6.1 iptables 转发

场景: 让所有流量走代理(需要 root)

# 开启 IP 转发
echo 1 > /proc/sys/net/ipv4/ip_forward

# 添加转发规则
iptables -t nat -A PREROUTING -p tcp --dport 80 -j DNAT --to-destination 127.0.0.1:7890
iptables -t nat -A PREROUTING -p tcp --dport 443 -j DNAT --to-destination 127.0.0.1:7890

# 保存规则
iptables-save > /etc/iptables/rules.v4

6.2 systemd 服务

创建 systemd 服务:

cat > /etc/systemd/system/proxy-enable.service << EOF
[Unit]
Description=Enable proxy on boot

[Service]
Type=oneshot
ExecStart=/usr/local/bin/proxy on
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF

# 启用服务
systemctl daemon-reload
systemctl enable proxy-enable

七、常见问题

7.1 环境变量不生效

解决方案:

# 检查是否设置成功
echo $http_proxy

# 重新加载配置
source ~/.zshrc

# 检查是否在正确的 shell 中
echo $SHELL

# 新终端窗口会自动加载

7.2 某些命令不走代理

原因: 部分命令不使用环境变量

解决方案:

# 使用 proxychains
proxychains command

# 使用 tsocks
tsocks command

# 使用 goproxy
goproxy command

7.3 代理服务器没启动

解决方案:

# 检查进程
ps aux | grep clash

# 启动代理服务器
clash -d ~/.config/clash &

# 设置开机自启
echo "clash -d ~/.config/clash &" >> ~/.zshrc

7.4 权限问题

解决方案:

# 使用 sudo 时继承环境变量
sudo -E command

# 或者在 sudoers 中配置
echo "Defaults env_keep += \"http_proxy https_proxy\"" | sudo tee -a /etc/sudoers

八、高级配置

8.1 多代理切换

配置多个代理:

# ~/.zshrc

proxy_china() {
    export http_proxy=http://127.0.0.1:7890
    export all_proxy=socks5://127.0.0.1:7891
    echo "✅ China proxy enabled"
}

proxy_us() {
    export http_proxy=http://127.0.0.1:7892
    export all_proxy=socks5://127.0.0.1:7893
    echo "✅ US proxy enabled"
}

proxy_eu() {
    export http_proxy=http://127.0.0.1:7894
    export all_proxy=socks5://127.0.0.1:7895
    echo "✅ EU proxy enabled"
}

8.2 代理链

配置代理链:

# 使用多个代理服务器
export http_proxy=http://proxy1.example.com:8080
export https_proxy=http://proxy2.example.com:8080

# 使用认证代理
export http_proxy=http://user:password@proxy.example.com:8080

8.3 动态代理

根据目标自动选择:

# 创建脚本
#!/bin/bash

if echo "$1" | grep -q "\.cn$"; then
    # 国内网站直连
    curl "$1"
else
    # 国外网站走代理
    proxychains curl "$1"
fi

九、总结

9.1 配置方案对比

方案优点缺点推荐度
环境变量简单,大部分命令支持部分命令不支持⭐⭐⭐⭐
proxychains所有命令都支持需要额外安装⭐⭐⭐⭐⭐
应用级配置精确控制每个应用单独配置⭐⭐⭐
系统级代理全局生效需要 root 权限⭐⭐⭐⭐

9.2 最佳实践

开发环境:

1. 设置环境变量(http_proxy, https_proxy, all_proxy)
2. 配置 git 代理
3. 配置 npm/yarn/pnpm 代理
4. 配置 pip 代理
5. 使用 proxychains 处理不支持代理的命令
6. 创建代理开关脚本

服务器环境:

1. 使用环境变量或 proxychains
2. 配置 Docker 代理
3. 设置定时任务检测网络环境
4. 配置防火墙规则
5. 考虑使用 TUN 模式

日常使用:

1. 创建代理切换脚本
2. 设置 alias 别名
3. 在终端标题显示状态
4. 使用 fzf 快速切换
5. 配置开机自启

通过合理配置Linux终端代理,你可以实现高效、便捷的翻墙体验。选择适合自己的方案,让工作和生活更加顺畅。


关注 润土分享 获取更多Linux技巧!

691 词 · 898 令牌