
The user is asking how to set up a proxy on Mac in the .zshrc file. This is a common technical question about terminal proxy configuration. This is general knowledge that I can answer directly without needing to search - it’s stable technical knowledge that doesn’t change frequently.
Let me write a comprehensive answer about setting up proxy in .zshrc on macOS.
Key points to cover:
- Basic proxy environment variables (http_proxy, https_proxy, all_proxy)
- Creating proxy on/off functions
- Common proxy ports (Clash 7890, V2Ray 1080, Shadowsocks 1087/1086, etc.)
- no_proxy settings
- Verification methods
- Common tools and their configurations (git, npm, etc.)
The standard approach:
# ~/.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 https_proxy all_proxy
echo "代理已关闭"
}
Common ports:
- Clash / Clash Verge / ClashX: HTTP 7890 (mixed port), SOCKS 7891
- V2RayN/V2Ray: HTTP 1087/10808, SOCKS 1080/10808
- Shadowsocks: HTTP 1087, SOCKS 1080
- Surge: HTTP 6152, SOCKS 6153
Verification:
- curl cip.cc or curl ip.sb, curl ifconfig.me
- echo $http_proxy
- curl -v google.com
Important notes:
- HTTP proxy env var doesn’t apply to all apps (browsers use system proxy)
- Terminal apps like curl, wget respect env vars
- git may need separate config
- The http_proxy scheme for socks: socks5://
- For socks proxy with http_proxy variable, need socks5:// scheme
Also mention:
- Always-on vs on-demand
- no_proxy for local addresses
- macOS system proxy (networksettings) is separate from shell env
Let me also mention useful aliases and checking with env | grep -i proxy.
For git specifically: git uses http_proxy env vars, but for SSH protocol it doesn’t.
I should write this in Chinese since the user is asking in Chinese. The format should be a clear technical guide with code blocks. Previous responses have been detailed with headers and tables, which fits the technical how-to nature of the question. I’ll keep a similar style - structured markdown with code examples.
This doesn’t need a search - it’s stable technical knowledge.
在 Mac 的 .zshrc 中配置代理
一、基础写法(直接生效)
编辑 ~/.zshrc:
vim ~/.zshrc
# 或
open -e ~/.zshrc
加入以下内容(端口换成你自己的,下面以 Clash 默认 7890 为例):
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,192.168.*.*,10.*.*.*"
保存后让配置生效:
source ~/.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"
export HTTP_PROXY=$http_proxy
export HTTPS_PROXY=$https_proxy
export ALL_PROXY=$all_proxy
echo "✅ 代理已开启 → $http_proxy"
}
proxy_off() {
unset http_proxy https_proxy all_proxy \
HTTP_PROXY HTTPS_PROXY ALL_PROXY
echo "❌ 代理已关闭"
}
# 一条命令查看当前出口 IP
proxy_ip() {
curl -s --connect-timeout 5 cip.cc || echo "查询失败"
}
使用方式:
proxy_on # 开启代理
proxy_off # 关闭代理
proxy_ip # 查看当前出口 IP,验证是否生效
保存后 source ~/.zshrc 生效。
三、端口怎么填?(按代理软件对号入座)
| 代理软件 | HTTP 端口 | SOCKS5 端口 |
|---|---|---|
| Clash / ClashX / Clash Verge | 7890(Mixed 混合端口) | 7891(或 7890) |
| Surge | 6152 | 6153 |
| V2Ray / V2RayN | 1087(部分为 10809) | 1080(部分为 10808) |
| Shadowsocks (macOS 客户端) | 1087 | 1080 |
| Quantumult X / 其他 | 看软件设置里的“本地监听端口” | — |
不确定就在代理软件设置里找 “HTTP 端口 / Mixed Port / 本地监听”。
另外,代理软件需开启 “允许局域网连接” 一般不需要,但确保它在监听 127.0.0.1 或 0.0.0.0。
四、验证代理是否生效
# 1. 查看环境变量
env | grep -i proxy
# 2. 看出口 IP(应显示代理节点的 IP)
curl cip.cc
# 3. 测试 Google(能返回内容即成功)
curl -I https://www.google.com
五、常见坑
| 问题 | 说明 |
|---|---|
| curl 生效但 git 不生效 | git 走 HTTPS 时会用环境变量,一般没问题;但 SSH 方式(git@github.com)不读环境变量,需改 SSH 的 ProxyCommand |
| npm / yarn 不走代理 | npm 有自己的配置:npm config set proxy http://127.0.0.1:7890 |
| 终端生效但浏览器/APP 不生效 | 环境变量只对终端程序(curl、wget、git 等)有效;浏览器请开系统代理或用代理软件的“系统代理”开关 |
| SOCKS 代理写法 | 环境变量里要用 socks5:// 前缀:export all_proxy=socks5://127.0.0.1:1080 |
| 开了函数但 curl 走的还是直连 | 检查是否小写/大写变量都设了,某些程序只认大写 HTTP_PROXY |
| Clash 没开但 proxy_on 了 | 所有请求超时,proxy_off 关掉即可 |
git 走 SSH 协议时代理(进阶,可选)
# ~/.ssh/config
Host github.com
ProxyCommand nc -X 5 -x 127.0.0.1:7890 %h %p
六、完整示例(可直接抄走)
# ========== Proxy ==========
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 ON → $http_proxy"
}
proxy_off() {
unset http_proxy https_proxy all_proxy
echo "Proxy OFF"
}
alias pip='proxy_on && pip' # 谨慎使用,按需添加
一句话总结:在 ~/.zshrc 里定义 proxy_on / proxy_off 两个函数,填对代理软件的监听端口,source ~/.zshrc 后随开随关,用 curl cip.cc 验证。
