ByteNoteByteNote

字节笔记本

2026年3月13日

macOS 终端代理配置完全指南

API中转
¥120

本文是一篇关于 macOS 终端代理配置的完整指南。作者详细介绍了在 macOS 上为各种终端工具配置代理的方法,包括系统代理、环境变量、Git 代理、SSH 代理等,帮助开发者解决终端网络访问问题。

问题背景

在 macOS 上使用终端时,经常会遇到需要通过代理访问网络资源的情况,特别是:

  • 公司网络限制
  • 访问国外资源
  • GitHub 等平台访问受限

代理类型

1. HTTP/HTTPS 代理

bash
# 设置 HTTP 代理
export http_proxy=http://proxy.example.com:8080
export https_proxy=http://proxy.example.com:8080

2. SOCKS 代理

bash
# 使用 proxychains
proxychains4 curl https://example.com

3. SSH 代理

bash
# 通过 SSH 跳板机
ssh -J jump@example.com target@example.com

工具配置

Git 代理

bash
# 设置 Git 代理
git config --global http.proxy http://proxy.example.com:8080
git config --global https.proxy http://proxy.example.com:8080

# 取消代理
git config --global --unset http.proxy
git config --global --unset https.proxy

Homebrew 代理

bash
# 设置 Homebrew 代理
export HOMEBREW_BOTTLE_DOMAIN=proxy.example.com
export ALL_PROXY=socks5://proxy.example.com:1080

# 或在配置文件中
echo 'export ALL_PROXY=socks5://proxy.example.com:1080' >> ~/.zshrc

pip 代理

bash
# 临时使用代理
pip install --proxy http://proxy.example.com:8080 package-name

# 永久配置
pip config set global.proxy http://proxy.example.com:8080

npm 代理

bash
# 设置 npm 代理
npm config set proxy http://proxy.example.com:8080
npm config set https-proxy http://proxy.example.com:8080

Shell 配置

Zsh 配置

bash
# ~/.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:7890
    echo "代理已启用"
}

proxy_off() {
    unset http_proxy
    unset https_proxy
    unset all_proxy
    echo "代理已禁用"
}

# 默认启用
proxy_on

Bash 配置

bash
# ~/.bash_profile 或 ~/.bashrc

# 代理环境变量
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:7890
export no_proxy=localhost,127.0.0.1

自动切换方案

基于网络环境

bash
# 自动检测并切换
if networksetup -getwebproxy | grep -q "Enabled: Yes"; then
    proxy_on
else
    proxy_off
fi

基于 WiFi SSID

bash
# 根据连接的 WiFi 自动切换
ssid=$(networksetup -getairportnetwork en0 | awk -F': ' '{print $2}')

if [ "$ssid" = "Office-Network" ]; then
    proxy_on
else
    proxy_off
fi

常见问题

Q1: 代理不生效

bash
# 检查环境变量
echo $http_proxy
echo $https_proxy

# 测试连接
curl -I https://www.google.com

Q2: Git 仍然无法访问

bash
# 使用 SSH 代替 HTTPS
git config --global url."https://github.com/".insteadOf "git@github.com:"

Q3: 某些应用不走代理

bash
# 设置 no_proxy
export no_proxy=localhost,127.0.0.1,*.local,*.example.com

高级技巧

1. 代理自动切换脚本

bash
#!/bin/bash
# proxy-switcher.sh

while true; do
    if ping -c 1 -W 1000 8.8.8.8 > /dev/null 2>&1; then
        proxy_off
    else
        proxy_on
    fi
    sleep 5
done

2. 应用特定代理

bash
# 为特定应用设置代理
alias curl-proxy='curl -x http://127.0.0.1:7890'
alias git-proxy='git -c http.proxy=http://127.0.0.1:7890'

3. PAC 自动配置

bash
# 使用 pacparser
pip install pacparser

# 加载 PAC 脚本
python -c "from pacparser import pacparser; pacparser.parse_pac(pac_script); print(pacparser.find_proxy('https://example.com'))"

推荐工具

1. proxychains4

bash
brew install proxychains-ng

# 配置 /etc/proxychains.conf
# 使用
proxychains4 curl https://example.com

2. Clash

bash
brew install --cask clash

# 开启系统代理
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890

3. V2Ray

bash
brew install v2ray

# 配置 V2Ray
# 启动服务
brew services start v2ray

原文链接

https://tf2jaguar.github.io/mac-terminal-proxy.html

分享: