WSL2 在开启 Windows 代理情况下安装 Codex CLI 的完整步骤
·
本文档提供两种方法在 WSL2 中使用 Windows 代理,并完成 Codex CLI 安装与登录。
方法一:通过 Windows 主机 IP 使用代理(适用于默认 NAT 网络)
步骤一:确认 Windows 代理端口
在 Windows PowerShell 或 CMD 执行
reg query "HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer
输出示例
ProxyServer REG_SZ 127.0.0.1:9674
说明 Windows 系统代理地址为
127.0.0.1:9674
步骤二:获取 Windows 在 WSL 网络中的 IP
进入 WSL 终端执行
grep nameserver /etc/resolv.conf
输出示例
nameserver 172.20.128.1
其中
172.20.128.1
为 Windows 主机在 WSL 网络中的地址。
步骤三:在 WSL 设置代理
在 WSL 终端执行
export http_proxy=http://172.20.128.1:9674
export https_proxy=http://172.20.128.1:9674
export HTTP_PROXY=http://172.20.128.1:9674
export HTTPS_PROXY=http://172.20.128.1:9674
步骤四:测试代理
执行
curl https://api.openai.com
如果返回 HTTP 状态码(例如 401),说明代理已经生效。
步骤五:安装 Node.js
执行
sudo apt update
sudo apt install -y nodejs npm
检查版本
node -v
npm -v
步骤六:安装 Codex CLI
执行
npm install -g @openai/codex
检查是否安装成功
codex --version
步骤七:登录 ChatGPT 账号
执行
codex login --device-auth
终端会给出一个登录地址和验证码。
在 Windows 浏览器中打开该地址,输入验证码并完成登录授权。
步骤八:设置 WSL 永久代理
编辑 bashrc
nano ~/.bashrc
在文件末尾添加
WIN_IP=$(grep -m1 nameserver /etc/resolv.conf | awk '{print $2}')
export http_proxy=http://$WIN_IP:9674
export https_proxy=http://$WIN_IP:9674
export HTTP_PROXY=$http_proxy
export HTTPS_PROXY=$https_proxy
保存后执行
source ~/.bashrc
方法二:使用 WSL Mirrored Networking(推荐)
该方法可以让 WSL 直接使用 Windows 的 127.0.0.1 代理。
步骤一:创建 WSL 配置文件
在 Windows 用户目录创建文件
C:\Users\你的用户名\.wslconfig
写入
[wsl2]
networkingMode=mirrored
dnsTunneling=true
步骤二:重启 WSL
在 Windows PowerShell 执行
wsl --shutdown
然后重新打开 WSL。
步骤三:在 WSL 设置代理
执行
export http_proxy=http://127.0.0.1:9674
export https_proxy=http://127.0.0.1:9674
export HTTP_PROXY=http://127.0.0.1:9674
export HTTPS_PROXY=http://127.0.0.1:9674
步骤四:测试代理
执行
curl https://api.openai.com
如果返回 HTTP 响应说明代理正常。
步骤五:安装 Node.js
执行
sudo apt update
sudo apt install -y nodejs npm
步骤六:安装 Codex CLI
执行
npm install -g @openai/codex
步骤七:登录 ChatGPT
执行
codex login --device-auth
在浏览器完成授权登录。
步骤八:设置永久代理
编辑
nano ~/.bashrc
添加
export http_proxy=http://127.0.0.1:9674
export https_proxy=http://127.0.0.1:9674
export HTTP_PROXY=$http_proxy
export HTTPS_PROXY=$https_proxy
执行
source ~/.bashrc
完成
配置完成后,WSL 中以下工具都可以正常通过 Windows 代理访问网络
- apt
- curl
- git
- npm
- Codex CLI
更多推荐



所有评论(0)