WSL 开发环境完整配置笔记(Ubuntu 24.04 + D盘部署 + 独立代理适配)

本文最后更新于 2026年4月10日 上午

WSL 开发环境完整配置笔记(Ubuntu 24.04 + D盘部署 + 独立代理适配)

聚焦内网代理开发场景、工具独立代理、无全局污染、一键可执行


一、Windows 前置开启(管理员 PowerShell)

1
2
3
4
5
# 启用 WSL 功能
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

# 启用虚拟机平台(WSL2 必需)
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

✅ 执行后重启电脑


二、安装最新版 WSL(支持 –location 自定义盘符)

下载官方离线包双击安装:
https://github.com/microsoft/WSL/releases/download/2.7.1/wsl.2.7.1.0.x64.msi


三、安装 Ubuntu 24.04 到 D 盘(二选一)

方案1|在线安装

1
wsl --install -d Ubuntu 24.04 --location D:\

方案2|离线安装(推荐,内网/无网络限制)

  1. 下载清华镜像离线包:
    https://mirrors.tuna.tsinghua.edu.cn/ubuntu-releases/24.04.4/ubuntu-24.04.4-wsl-amd64.wsl

  2. 执行安装(修改为你的实际路径):

1
wsl --install --from-file E:\tools\ubuntu-24.04.4-wsl-amd64.wsl --location D:\Ubuntu-24.04

四、验证安装状态

1
2
wsl --version       # 必须 ≥ 2.7.1
wsl -l -v # 必须 VERSION=2

五、WSL 镜像网络(Win11 专属|IP 与 Windows 完全一致)

编辑 Windows 配置文件:C:\Users\jxh\.wslconfig

1
2
[wsl2]
networkingMode=mirrored

重启 WSL 生效:

1
2
wsl --shutdown
wsl

核心特性

  • WSL 与 Windows 共用同一 IP,局域网设备可直接访问
  • 端口共享:同端口不可同时占用
  • 访问方式:Windows_IP:端口 直接访问 WSL 服务

六、Ubuntu 系统基础配置

1. 更换清华软件源

1
2
sudo cp /etc/apt/sources.list.d/ubuntu.sources /etc/apt/sources.list.d/ubuntu.sources.bak
sudo vi /etc/apt/sources.list.d/ubuntu.sources

替换为清华源:

1
2
3
4
5
6
7
8
9
10
11
Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

Types: deb
URIs: https://mirrors.tuna.tsinghua.edu.cn/ubuntu
Suites: noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

更新源缓存:

1
sudo apt update

2. APT 独立代理(仅 apt 走代理,不污染系统)

1
sudo vi /etc/apt/apt.conf.d/99proxy

写入:

1
2
Acquire::http::Proxy "http://192.168.1.1:10808";
Acquire::https::Proxy "http://192.168.1.1:10808";

七、pip 配置(清华源 + 独立代理)

1
2
mkdir -p ~/.config/pip
vi ~/.config/pip/pip.conf

写入配置:

1
2
3
4
5
6
[global]
index-url=https://pypi.tuna.tsinghua.edu.cn/simple
proxy = http://192.168.1.1:10808

[install]
trusted-host=pypi.tuna.tsinghua.edu.cn

八、Node.js + npm 完整配置(含全局免 sudo)

1. 安装 Node.js LTS

1
2
curl -fsSL https://deb.nodesource.com/setup_lts.x | sudo -E bash -
sudo apt install nodejs -y

2. npm 国内镜像 + 独立代理

1
2
3
npm config set registry https://registry.npmmirror.com
npm config set proxy http://192.168.1.1:10808
npm config set https-proxy http://192.168.1.1:10808

3. npm 全局目录(免 sudo 安装全局包)

1
2
3
4
mkdir -p ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH="$HOME/.npm-global/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc

九、Claude Code 安装 + 独立代理(仅它生效,不污染系统)

1. 安装 Claude

1
2
3
export https_proxy=http://192.168.1.1:10808
npm install -g @anthropic-ai/claude-code
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc && source ~/.bashrc

2. 创建独立代理包装脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 重命名真实二进制
mv ~/.npm-global/bin/claude ~/.npm-global/bin/claude-real

# 创建代理脚本
cat > ~/.npm-global/bin/claude << 'EOF'
#!/bin/bash
export HTTPS_PROXY=http://192.168.1.1:10808
export https_proxy=http://192.168.1.1:10808
export NO_PROXY=localhost,127.0.0.1
export no_proxy=localhost,127.0.0.1
exec "$(dirname "$0")/claude-real" "$@"
EOF

# 添加执行权限
chmod +x ~/.npm-global/bin/claude

3. Claude 升级恢复脚本(升级后一键重建代理)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cat > ~/reinstall-claude-wrapper.sh << 'EOF'
#!/bin/bash
mv ~/.npm-global/bin/claude ~/.npm-global/bin/claude-real
cat > ~/.npm-global/bin/claude << 'WRAPPER'
#!/bin/bash
export HTTPS_PROXY=http://192.168.1.1:10808
export https_proxy=http://192.168.1.1:10808
export NO_PROXY=localhost,127.0.0.1
export no_proxy=localhost,127.0.0.1
exec "$(dirname "$0")/claude-real" "$@"
WRAPPER
chmod +x ~/.npm-global/bin/claude
echo "success"
EOF

chmod +x ~/reinstall-claude-wrapper.sh

4.VSCode Claude Code插件独立代理设置

vscode claude code插件的claude入口是和claude code cli独立的,也不继承vscode的代理设置,但两者共享用户目录下的claude设置

1
2
3
cd ~/.vscode-server/extensions/anthropic.claude-code-<版本号>/
cp package.json package.json.bak
vi set-proxy-and-run.js

set-proxy-and-run.js

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/usr/bin/env node

// 设置代理(请替换为你的实际代理地址和端口)
process.env.HTTP_PROXY = 'http://192.168.1.1:10808';
process.env.HTTPS_PROXY = 'http://192.168.1.1:10808';
process.env.http_proxy = 'http://192.168.1.1:10808';
process.env.https_proxy = 'http://192.168.1.1:10808';

// 加载原扩展并导出其所有方法(重点是 activate / deactivate)
const originalExtension = require('./extension.js');

// 将原扩展的所有导出转发给 VSCode
module.exports = originalExtension;
1
2
3
4
chmod +x set-proxy-and-run.js
sed -i 's/"main": "[^"]*"/"main": ".\/set-proxy-and-run.js"/' package.json
pkill -f -9 vscode-server
// 重启vscode

十、Claude Code 第三方 API 接入配置(智谱/第三方兼容)

1. 创建 Claude 配置目录

1
mkdir -p ~/.claude

2. 配置环境变量与模型映射

1
vi ~/.claude/settings.json

写入:

1
2
3
4
5
6
7
8
9
10
11
{
"env": {
"ANTHROPIC_AUTH_TOKEN": "your_api_key",
"ANTHROPIC_BASE_URL": "https://open.bigmodel.cn/api/anthropic",
"API_TIMEOUT_MS": "3000000",
"CLAUDE_CODE_DISABLE_NONESSENTIAL_TRAFFIC": 1,
"ANTHROPIC_DEFAULT_HAIKU_MODEL": "glm-4.7",
"ANTHROPIC_DEFAULT_SONNET_MODEL": "glm-5-turbo",
"ANTHROPIC_DEFAULT_OPUS_MODEL": "glm-5.1"
}
}

3. 配置 MCP 服务(联网能力/插件)

1
vi ~/.claude.json

写入:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
{
"hasCompletedOnboarding": true,
"mcpServers": {
"zai-mcp-server": {
"type": "stdio",
"command": "npx",
"args": [
"-y",
"@z_ai/mcp-server"
],
"env": {
"Z_AI_API_KEY": "your_api_key",
"Z_AI_MODE": "ZHIPU"
}
},
"web-search-prime": {
"type": "http",
"url": "https://open.bigmodel.cn/api/mcp/web_search_prime/mcp",
"headers": {
"Authorization": "your_api_key"
}
},
"web-reader": {
"type": "http",
"url": "https://open.bigmodel.cn/api/mcp/web_reader/mcp",
"headers": {
"Authorization": "your_api_key"
}
},
"zread": {
"type": "http",
"url": "https://open.bigmodel.cn/api/mcp/zread/mcp",
"headers": {
"Authorization": "your_api_key"
}
}
}
}

十一、共享 Windows SSH 密钥(Git 免密互通)

直接复用 Windows SSH 凭证,WSL/Windows 共用一套 Git 身份

1
2
3
4
5
6
7
8
9
10
# 创建目录
mkdir -p ~/.ssh

# 复制 Windows 密钥到 WSL
cp -r /mnt/c/Users/jxh/.ssh/* ~/.ssh/

# 修复 Linux 必需权限(必须执行)
chmod 700 ~/.ssh
chmod 600 ~/.ssh/id_rsa
chmod 644 ~/.ssh/id_rsa.pub

可选 Git 全局配置:

1
2
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"

十二、解决中文乱码

1
2
sudo locale-gen en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8

十三、常用验证 & 维护命令

1
2
3
4
# WSL 管理
wsl --shutdown
wsl
wsl -l -v
1
2
3
4
5
6
7
8
# 工具验证
claude --version
node -v
npm -v
git --version

# 查看全局代理(必须为空,无系统污染)
echo $HTTPS_PROXY

整套配置核心优势

  • 适配内网代理开发场景,所有工具均配置独立代理
  • WSL 完整安装到 D 盘,不占用 C 盘
  • 镜像网络:局域网直接访问 WSL 服务
  • APT / pip / npm / Claude 全部独立代理,互不干扰
  • 无全局环境变量污染,开发环境纯净
  • 与 Windows 共享 SSH,Git 直接免密操作
  • Claude Code 支持第三方 API(智谱)接入,内网可用
  • 工具升级后可一键恢复代理,维护成本极低

WSL 开发环境完整配置笔记(Ubuntu 24.04 + D盘部署 + 独立代理适配)
https://xinhaojin.github.io/2026/04/07/WSL 开发环境完整配置笔记(Ubuntu 24.04 + D盘部署 + 独立代理适配)/
作者
xinhaojin
发布于
2026年4月7日
许可协议