← 返回博客列表技术栈

Claude Code 本机代理配置指南

国内本机运行 claude 时,需让终端流量走 VPS + Novproxy 代理,才能连上 api.anthropic.com
与浏览器 ZeroOmega 共用同一条链路,但配置方式不同。

问题现象

安装 Claude Code 成功后,直接运行:

claude

可能出现:

Unable to connect to Anthropic services
Failed to connect to api.anthropic.com: ECONNREFUSED

不是安装失败,而是本机无法直连 Anthropic API(国内网络限制)。

浏览器 vs 终端:为什么浏览器能开、claude 不行?

浏览器(claude.ai)Claude Code(终端)
代理ZeroOmega 自动走 127.0.0.1:7890默认不走代理
目标claude.ai / console.anthropic.comapi.anthropic.com
你要做的切换 proxy 情景模式设置 HTTP_PROXY / HTTPS_PROXY

两者共用同一条物理链路:

本机 127.0.0.1:7890
  → SSH 隧道(本机 → VPS)
  → VPS gost(127.0.0.1:7890)
  → Novproxy 住宅 IP
  → 目标网站 / API

前置条件(代理链必须已就绪)

VPS 上 gost 自启

ssh root@45.77.124.231 systemctl status gost-claude

应显示 active (running)

若未配置,参见 Claude 代理开机自启配置指南

本机 SSH 隧道自启

systemctl --user status claude-ssh-tunnel

应显示 active (running)

若未配置,参见 Claude 代理开机自启配置指南 中的 PC SSH 隧道 systemd 服务部分。

快速验证代理

curl -x http://127.0.0.1:7890 ipinfo.io

应返回美国住宅/宽带 IP。若失败,先修代理链,再开 claude

让 Claude Code 走代理

一次性测试

# 确认 API 可达
curl -x http://127.0.0.1:7890 https://api.anthropic.com -I

# 带代理启动
HTTP_PROXY=http://127.0.0.1:7890 HTTPS_PROXY=http://127.0.0.1:7890 claude

推荐:别名 claude-proxy(不影响其他命令)

echo 'alias claude-proxy="HTTP_PROXY=http://127.0.0.1:7890 HTTPS_PROXY=http://127.0.0.1:7890 claude"' >> ~/.bashrc
source ~/.bashrc

日常使用:

cd ~/codes/你的项目
claude-proxy

可选:每次手动 export

export HTTP_PROXY=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890
claude

不建议把 HTTP_PROXY 写进 ~/.bashrc 全局生效,会影响其他不需要代理的程序。

API Key 仍需配置

代理只解决网络连通,还需:

export ANTHROPIC_API_KEY="sk-ant-你的key"
# 或已写入 ~/.bashrc
echo $ANTHROPIC_API_KEY | head -c 20

安装步骤见 Claude Code 本机安装指南

完整日常使用流程

1. 开机(VPS gost + 本机 SSH 隧道已 systemd 自启)
2. 验证:curl -x http://127.0.0.1:7890 ipinfo.io
3. 进入项目:cd ~/codes/你的项目
4. 启动:claude-proxy
5. 浏览器 Claude(可选):ZeroOmega 选 proxy

在 Cursor 里使用

Cursor 内置终端同样适用:

# 在 Cursor 终端中
claude-proxy

或先 export 再 claude。Cursor 的 AI 功能与 Claude Code CLI 是独立的;CLI 不会自动继承 ZeroOmega 设置。

常见问题

现象原因解决
ECONNREFUSED api.anthropic.com未设代理claude-proxy 或设置 HTTP_PROXY
curl 127.0.0.1:7890 失败SSH 隧道未运行systemctl --user start claude-ssh-tunnel
VPS 上 curl 7890 失败gost 未运行systemctl restart gost-claude
代理通但 claude 仍报错无 API Key配置 ANTHROPIC_API_KEY
401 UnauthorizedKey 无效/过期console.anthropic.com 重新生成
浏览器能开、终端不行正常,终端未走 ZeroOmega按本文配置 HTTP_PROXY
Novproxy 用户名过期sid 失效VPS 上更新 gost-claude.service 并 restart

检查清单

□ systemctl status gost-claude(VPS)        → active
□ systemctl --user status claude-ssh-tunnel → active
□ curl -x http://127.0.0.1:7890 ipinfo.io   → 美国 IP
□ curl -x http://127.0.0.1:7890 https://api.anthropic.com -I → 有响应
□ echo $ANTHROPIC_API_KEY                   → sk-ant-...
□ claude-proxy                              → 进入交互界面

命令速查

# 验证代理
curl -x http://127.0.0.1:7890 ipinfo.io
curl -x http://127.0.0.1:7890 https://api.anthropic.com -I

# 配置别名(一次性)
echo 'alias claude-proxy="HTTP_PROXY=http://127.0.0.1:7890 HTTPS_PROXY=http://127.0.0.1:7890 claude"' >> ~/.bashrc
source ~/.bashrc

# 启动
cd ~/codes/你的项目
claude-proxy

# 重启代理链
systemctl --user restart claude-ssh-tunnel
ssh root@45.77.124.231 systemctl restart gost-claude

架构图

┌─────────────┐     ZeroOmega      ┌──────────────┐
│   浏览器     │ ──→ 127.0.0.1:7890 │              │
└─────────────┘                    │   本机 PC     │
┌─────────────┐  HTTP_PROXY        │              │
│ Claude Code │ ──→ 127.0.0.1:7890 │              │
└─────────────┘                    └──────┬───────┘
                                          │ SSH -L 7890
                                          ▼
                                   ┌──────────────┐
                                   │  VPS gost    │
                                   │  :7890       │
                                   └──────┬───────┘
                                          │ Novproxy :1000
                                          ▼
                              api.anthropic.com / claude.ai

相关文档

文章说明
Claude Code 本机安装指南Node、npm、安装、API Key
Claude 代理开机自启配置指南gost 与 SSH 隧道 systemd 自启
Claude 浏览器住宅代理配置指南浏览器 ZeroOmega 手动配置
Agent 工具栈指南整体工具栈