VSCode SSH 连接远程服务器后,Codex 插件登录失败(完整实战)
本文为作者实际实践过程,最后借助chatgpt整理完成
一、问题背景
我的开发环境如下:
text
Win10 本地电脑
↓(VSCode Remote SSH)
远程 Linux 服务器 / 容器
实际情况:
- 本地 Win10 可以正常访问 ChatGPT
- 远程服务器无法访问 ChatGPT
- 在 VSCode 中安装了 Codex 插件,使用账户登录失败
目标:
让远程服务器(或容器)里的 VSCode 插件请求,借助本地 Win10 访问 OpenAI。
二、问题本质
很多人以为:
text
VSCode Remote SSH 连接成功
= 插件网络走本地电脑
实际上不是。
在 Remote SSH 模式下,大多数插件运行在远程端:
text
Win10 本地 VSCode(界面)
↓
远程 Linux 服务器(插件实际运行)
所以:
- 本地能访问 ChatGPT 没用
- 插件请求仍然从远程服务器发出
- 远程服务器访问不了 OpenAI,就会失败
三、解决思路
既然远程服务器能访问本地 Win10,而 Win10 又可以访问chatgpt。
那就让远程服务器的请求走:
text
远程服务器
→ SSH 隧道
→ Win10
→ OpenAI
最稳定方案:
使用 SSH RemoteForward,把本地端口映射到远程服务器。
四、确认端口
在 Win10 CMD 执行:
bash
netstat -ano | findstr 7897
输出类似:
text
TCP 0.0.0.0:7897 LISTENING
说明 HTTP 端口是:
text
7897
(有的人是 7890,按自己实际情况)
五、建立 SSH 反向端口转发(核心步骤)
编辑本地电脑的 SSH 配置文件:
text
C:\Users\你的用户名\.ssh\config
加入 RemoteForward(重中之重的参数):
ssh-config
Host myserver
HostName 10.11.x.x
User your_user
RemoteForward 17897 127.0.0.1:7897
说明:
text
17897 = 远程服务器开放的端口
127.0.0.1:7897 = 本地 Win10 端口
含义:
text
远程服务器访问 127.0.0.1:17897
=
自动转发到 Win10 的 127.0.0.1:7897
六、使用 VSCode Remote SSH 重连服务器
在 VSCode 中:
text
Remote-SSH: Connect to Host...
连接:
text
myserver
此时 SSH 隧道自动建立。
七、远程服务器测试是否成功
登录远程服务器后执行:
bash
curl -x http://127.0.0.1:17897 https://api.openai.com -I
如果看到:
text
HTTP/1.1 200 Connection established
说明链路成功。
如果进一步执行:
bash
curl https://api.openai.com
返回:
json
{
"message": "Welcome to the OpenAI API!"
}
说明 OpenAI 已可访问。
八、让 VSCode / Codex 插件走proxy
在远程 VSCode 设置中打开:
text
Preferences: Open Remote Settings (JSON)
加入:
json
{
"http.proxy": "http://127.0.0.1:17897",
"http.proxySupport": "override",
"http.systemCertificates": true
}
然后执行:
text
Developer: Reload Window
九、如果使用容器开发(重要)
如果 VSCode 实际连接的是容器,例如终端显示:
text
root@xxxxxx:/workspace#
那么插件运行位置在容器内。
此时同样配置:
json
"http.proxy": "http://127.0.0.1:17897"
因为容器内已能访问该端口即可。
十、常见报错排查
1. Reconnecting... 一直重试
说明插件网络请求失败。
优先检查:
bash
curl -x http://127.0.0.1:17897 https://api.openai.com
2. 401 Unauthorized
例如:
text
Incorrect API key provided
说明已经通了,但 API Key 错误。
重新创建 Key:
text
https://platform.openai.com/api-keys
3. Quota exceeded
说明 Key 正确,但额度不足:
text
Check your plan and billing details
需要充值 API 余额。
注意:
text
ChatGPT Plus ≠ OpenAI API 额度
这是两套计费体系。
十一、为什么不建议直接在服务器全局配置proxy?
很多人会在 Linux 写:
bash
export http_proxy=http://127.0.0.1:17897
export https_proxy=http://127.0.0.1:17897
这样虽然可用,但会影响:
- curl
- pip
- git
- conda
- apt
所有命令行工具。
更推荐:
只在 VSCode 中配置 http.proxy
这样影响范围最小。
十二、最终架构图
text
VSCode 界面(Win10)
↓
Remote SSH
↓
远程服务器 / 容器中的 Codex 插件
↓
127.0.0.1:17897(SSH RemoteForward)
↓
Win10 7897
↓
OpenAI API
十三、最终结论
如果你本地电脑能访问CHATGPT,但远程服务器不能,而又想在 VSCode Remote SSH 中使用 Codex / OpenAI 插件。
最优解就是:
text
SSH RemoteForward + VSCode http.proxy
无需折腾服务器proxy,也无需服务器单独装proxy。
十四、附录:完整配置汇总
SSH Config
ssh-config
Host myserver
HostName 10.11.x.x
User root
RemoteForward 17897 127.0.0.1:7897
VSCode Remote Settings.json
json
{
"http.proxy": "http://127.0.0.1:17897",
"http.proxySupport": "override",
"http.systemCertificates": true
}
十五、写在最后
这个方案特别适合:
- 公司内网服务器开发
- GPU 远程训练机
- Docker 容器开发
- Remote SSH 使用 AI 编程插件
如果本地能用proxy,远程不能,这套方案非常稳。