WSL2 命令大全
整理日期:2026-05-25 | Windows Subsystem for Linux 2
适用版本:WSL2(Windows 10 版本 1903+ / Windows 11)
目录
基础命令
安装与初始化
| 命令 | 说明 | 示例 |
|---|---|---|
wsl --install |
安装 WSL2 及默认 Ubuntu(需管理员 PowerShell) | wsl --install |
wsl --install -d <Distro> |
安装指定发行版 | wsl --install -d Debian |
wsl --install -d <Distro> --name <Name> |
安装并自定义发行版名称 | wsl --install -d Ubuntu-24.04 --name MyUbuntu |
wsl --install -d <Distro> --location <Path> |
安装到指定目录(需提前创建目录) | wsl --install -d Ubuntu-24.04 --location D:\WSL\Ubuntu |
wsl --install -d <Distro> --name <Name> --location <Path> |
安装指定发行版,自定义名称与安装位置 | wsl --install -d Ubuntu-24.04 --name <Name> --location <InstallPath> |
wsl --install --no-distribution |
仅安装 WSL 内核,不安装发行版 | wsl --install --no-distribution |
wsl --update |
更新 WSL2 内核到最新版 | wsl --update |
wsl --update --rollback |
回滚到上一版 WSL 内核 | wsl --update --rollback |
日常操作
| 命令 | 说明 | 示例 |
|---|---|---|
wsl --list / wsl -l |
列出已安装的发行版 | wsl -l |
wsl --list -v / wsl -l -v |
列出发行版及详细状态 | wsl -l -v |
wsl --list --online / wsl -l -o |
列出可安装的发行版 | wsl -l -o |
wsl --status |
查看 WSL 全局状态 | wsl --status |
wsl --version |
查看 WSL 版本信息 | wsl --version |
wsl --shutdown |
关闭所有 WSL 实例 | wsl --shutdown |
wsl --terminate <Distro> |
终止指定发行版 | wsl -t Ubuntu |
启动发行版
| 命令 | 说明 | 示例 |
|---|---|---|
wsl |
启动默认发行版 | wsl |
wsl ~ |
启动默认发行版并进入用户主目录 | wsl ~ |
wsl -d <Distro> |
启动指定发行版 | wsl -d Ubuntu-22.04 |
wsl -d <Distro> -u <User> |
以指定用户启动 | wsl -d Ubuntu -u root |
wsl -e <Command> |
执行命令后退出(不进入交互 shell) | wsl -e cat /etc/os-release |
快速查询表
| 场景 | 命令 |
|---|---|
| 首次安装 WSL2 | wsl --install |
| 安装并指定名称与位置 | wsl --install -d Ubuntu-24.04 --name <Name> --location <InstallPath> |
| 查看已安装的 Linux | wsl -l -v |
| 查看可安装的发行版 | wsl -l -o |
| 启动默认 Linux | wsl |
| 启动指定 Linux | wsl -d Ubuntu-24.04 |
| 关闭所有 WSL | wsl --shutdown |
| 强制停止某个发行版 | wsl -t Ubuntu |
| 升级 WSL 内核 | wsl --update |
分发版管理
安装与卸载
| 命令 | 说明 | 示例 |
|---|---|---|
wsl --install -d <Distro> |
安装指定发行版 | wsl --install -d Ubuntu-22.04 |
wsl --import <Distro> <Dir> <File> |
从 tar 文件导入发行版 | wsl --import Ubuntu D:\WSL\Ubuntu D:\backup.tar |
wsl --export <Distro> <File> |
导出发行版为 tar 文件 | wsl --export Ubuntu D:\backup.tar |
wsl --unregister <Distro> |
卸载发行版(数据不可恢复) | wsl --unregister Ubuntu |
发行版设置
| 命令 | 说明 | 示例 |
|---|---|---|
wsl --set-default <Distro> |
设置默认发行版 | wsl --set-default Ubuntu-22.04 |
wsl --set-default-version <Version> |
设置新安装发行版的默认 WSL 版本 | wsl --set-default-version 2 |
wsl --set-version <Distro> <Version> |
转换发行版到 WSL1 或 WSL2 | wsl --set-version Ubuntu 2 |
wsl --manage <Distro> --set-sparce |
启用稀疏 VHD(节省磁盘空间) | wsl --manage Ubuntu --set-sparce |
常用发行版名称
Ubuntu (默认,最新 LTS)
Ubuntu-22.04 (Ubuntu 22.04 LTS)
Ubuntu-24.04 (Ubuntu 24.04 LTS)
Debian (Debian GNU/Linux)
kali-linux (Kali Linux 渗透测试)
openSUSE-Leap-15.5 (openSUSE Leap)
SUSE-Linux-Enterprise-15 (SLES 15)
FedoraLinux (Fedora)
Pengwin (Windows 集成优化版)
Arch Linux (需手动安装,非商店版)
分发版生命周期
安装/导入
│
▼
┌─────────────┐ ┌──────────┐ ┌─────────────┐
│ Installed │────►│ Running │────►│ Stopped │
└──────┬──────┘ └──────────┘ └──────┬──────┘
│ ▲ │ │
│ │ │ wsl -t │
│ │ ▼ │
│ └────┘ │
│ │
▼ ▼
┌──────────────┐ ┌──────────────┐
│ Unregister │◄── 数据删除 │ 可重启 │
│ (不可恢复) │ wsl --unregister└──────────────┘
└──────────────┘
系统操作
状态查看
| 命令 | 说明 |
|---|---|
wsl --status |
WSL 全局状态(默认版本、内核版本等) |
wsl --version |
WSL 组件版本信息 |
wsl -l -v |
所有发行版状态(名称/状态/版本) |
wsl -l --quiet |
仅列出发行版名称 |
wsl -l --running |
仅列出运行中的发行版 |
wsl -l -v 输出示例
NAME STATE VERSION
* Ubuntu Running 2
Ubuntu-22.04 Stopped 2
docker-desktop Stopped 2
docker-desktop-data Stopped 2
* 表示默认发行版
WSL 安装位置与核心文件
| 项目 | 默认位置 | 说明 |
|---|---|---|
wsl.exe |
C:\Windows\System32\wsl.exe |
WSL 主程序 |
| WSL 系统目录 | C:\Windows\System32\lxss\ |
WSL 核心组件、库文件、工具 |
| WSL2 内核 | C:\Windows\System32\lxss\tools\kernel |
Linux 内核镜像 |
| WSL 支持 DLL | C:\Windows\System32\lxss\wslsupport.dll |
WSL 支持库 |
| 全局配置 | C:\Users\<用户名>\.wslconfig |
全局 WSL2 配置 |
| 发行版注册表 | HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss |
记录每个发行版的 BasePath |
常用查询命令
# 查看 wsl.exe 实际路径
(Get-Command wsl).Source
# 查看 WSL 系统目录内容
Get-ChildItem C:\Windows\System32\lxss -Recurse
# 查看 WSL2 内核文件
Get-ChildItem C:\Windows\System32\lxss\tools\kernel
# 查看全局配置文件
Get-ChildItem "$env:USERPROFILE\.wslconfig"
# 查看所有发行版的安装位置
Get-ChildItem "HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss" |
Get-ItemProperty | Select-Object DistributionName, BasePath
典型目录结构示例
C:\Windows\System32\lxss\
├── lib\
│ ├── libcuda.so
│ ├── libcuda.so.1
│ └── ... # GPU / CUDA 相关库
├── tools\
│ ├── kernel # WSL2 Linux 内核
│ └── bsdtar # tar 解压工具(用于 wsl --import)
└── wslsupport.dll # WSL 支持库
发行版数据位置
每个发行版的实际数据存放在注册表 BasePath 指向的目录中,核心文件是 ext4.vhdx:
# 列出某个发行版的虚拟磁盘
Get-ChildItem "E:\WSL\Ubuntu\ext4.vhdx"
启动与停止
| 命令 | 说明 |
|---|---|
wsl |
启动默认发行版 |
wsl ~ |
启动并进入用户主目录 |
wsl -d <Distro> |
启动指定发行版 |
wsl -d <Distro> -u <User> |
以指定用户身份启动 |
wsl -e <Command> |
执行单条命令后退出 |
wsl --exec <Command> |
同 -e,执行命令 |
wsl --shell-type <Type> |
指定 shell 类型启动 |
wsl --shutdown |
关闭所有发行版 |
wsl -t <Distro> |
终止指定发行版 |
关闭指定发行版
WSL 没有类似 VirtualBox acpipowerbutton 的"优雅关机"命令,停止指定发行版的标准做法就是 wsl -t 强制终止。
核心命令
# 终止指定发行版(<Distro> 为发行版名称,不区分大小写)
wsl -t Ubuntu
wsl --terminate Ubuntu # 等价写法(完整参数)
完整使用流程
# 1. 查看所有发行版状态
wsl -l -v
# 2. 停止指定的发行版
wsl -t Ubuntu-22.04
# 3. 验证状态(STATE 应变为 Stopped)
wsl -l -v
# 4. 重新启动该发行版
wsl -d Ubuntu-22.04
三种"停止"方式对比
| 命令 | 作用范围 | 是否影响其他发行版 | 典型场景 |
|---|---|---|---|
wsl -t <Distro> |
单个发行版 | ❌ 不影响 | ✅ 推荐:只关掉一个,其它继续用 |
wsl --shutdown |
所有发行版 + VM 引擎 | ✅ 全部停止 | 释放内存 / 修改 .wslconfig 后重启 |
wsl --shutdown <Distro> |
❌ 不存在 | - | 不要用这个,会报错 |
常见误区
# ❌ 错误:WSL 不支持针对单个发行版的优雅关闭
wsl -d Ubuntu --shutdown
# 报错:Invalid command line option: --shutdown
# ❌ 错误:不能直接在 PowerShell 用 Windows 命令
Stop-Service -Name "Ubuntu"
# Ubuntu 不是 Windows 服务,不会生效
# ✅ 正确:用 wsl -t 终止指定发行版
wsl -t Ubuntu
强制终止后数据安全
- 未保存的文件会丢失(等同于直接拔电源)
- Linux 文件系统(
/home,/root等)数据不会损坏(ext4 journal 保证) - Windows 挂载点(
/mnt/c)的数据不受影响 - 如有关键操作未保存,先进入 WSL 正常退出(
exit或logout),再用wsl -t
重启单个发行版的便捷写法
# 等价于先 wsl -t 再 wsl -d
wsl -t Ubuntu; wsl -d Ubuntu
# PowerShell 函数封装(写入 $PROFILE)
function Restart-Wsl-Distro($name) {
wsl -t $name
Start-Sleep -Seconds 2
wsl -d $name
}
# 使用:Restart-Wsl-Distro Ubuntu
在 WSL 内执行系统命令
# 查看 WSL 内部 IP
wsl -e hostname -I
# 查看 Linux 内核版本
wsl -e uname -r
# 查看发行版信息
wsl -e cat /etc/os-release
# 执行管道命令(需加引号)
wsl -- sh -c "ps aux | grep nginx"
文件系统
Windows 访问 Linux 文件
| 方式 | 路径/命令 |
|---|---|
| 文件资源管理器 | \\wsl$\<Distro>\ |
| PowerShell | Get-ChildItem \\wsl$\Ubuntu\home\ |
| CMD | dir \\wsl$\Ubuntu\home\ |
| 常用路径 | 说明 |
|---|---|
\\wsl$\Ubuntu\home\<user>\ |
Ubuntu 用户主目录 |
\\wsl$\Ubuntu\root\ |
Ubuntu root 目录 |
\\wsl$\ |
列出所有运行中的发行版 |
Linux 访问 Windows 文件
| 路径 | 说明 |
|---|---|
/mnt/c/ |
Windows C 盘 |
/mnt/d/ |
Windows D 盘 |
/mnt/<drive>/ |
其他盘符(自动挂载) |
/mnt/wslg/ |
WSLg 图形界面共享目录 |
文件系统结构
Windows 系统
┌─────────────────────────────────────┐
│ C:\Users\<用户名> │
│ D:\Data │
│ \\wsl$\Ubuntu\home\<user> ◄── 访问 WSL │
└─────────────────────────────────────┘
│ ▲
│ /mnt/c/ │ \\wsl$\
▼ │
Linux WSL2 系统
┌─────────────────────────────────────┐
│ /mnt/c/Users/<用户名> ◄── C盘 │
│ /mnt/d/Data ◄── D盘 │
│ /home/<user> ◄── Linux家目录│
│ /root/ ◄── root目录 │
│ /mnt/wslg/ ◄── 图形共享 │
└─────────────────────────────────────┘
性能提示
- Linux 文件操作:在 Linux 文件系统内(
/home,/root,/tmp等)速度最快 - 跨系统访问:通过
/mnt/访问 Windows 文件会有性能损耗 - 大文件处理:建议在 Linux 本地文件系统操作,完成后复制到 Windows
- 项目开发:代码项目放在 Linux 文件系统(
~/projects),避免/mnt/下直接开发
文件互操作示例
# Linux 中查看 Windows 桌面
ls /mnt/c/Users/$USER/Desktop/
# Linux 中复制文件到 Windows
cp ~/report.pdf /mnt/d/Backup/
# Windows PowerShell 中访问 Linux 文件
cp \\wsl$\Ubuntu\home\ubuntu\file.txt D:\Backup\
# 在 WSL 中使用 Windows 程序
notepad.exe /mnt/c/Users/$USER/Desktop/file.txt
explorer.exe .
查看发行版实际存储路径
WSL 发行版的真实数据存储在 Windows 注册表 HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss 中记录的位置,可通过 PowerShell 读取:
# 查看所有 WSL 发行版的实际存储路径
Get-ChildItem "HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss" | Get-ItemProperty |
Select-Object DistributionName, BasePath
典型输出:
DistributionName BasePath
---------------- --------
Ubuntu C:\Users\<用户名>\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState
Ubuntu-22.04 D:\WSL\Ubuntu-22.04
说明:
- 从 Microsoft Store 安装的发行版(默认
Ubuntu)通常存放在C:\Users\<用户名>\AppData\Local\Packages\...下- 通过
wsl --import或wsl --install --location自定义安装的发行版,会存放在--location指定的目录BasePath是发行版的根目录,其中包含ext4.vhdx(实际虚拟磁盘文件)- 修改注册表前请先
wsl --shutdown,避免数据损坏
通过 Microsoft Store 安装的发行版
如果是从 Microsoft Store 安装的发行版,也可通过 Appx 包信息定位:
Get-AppxPackage -Name "*Ubuntu*" | Select Name, InstallLocation
虚拟硬盘通常位于该安装目录下的 LocalState\ext4.vhdx。
直接定位虚拟硬盘文件(ext4.vhdx)
结合注册表中的 BasePath,可以直接列出所有发行版的 .vhdx 文件:
Get-ChildItem "HKCU:\Software\Microsoft\Windows\CurrentVersion\Lxss" | Get-ItemProperty |
ForEach-Object {
Get-ChildItem $_.BasePath -Filter "ext4.vhdx" -ErrorAction SilentlyContinue |
Select-Object @{N="DistributionName";E={$_.DistributionName}}, FullName, Length
}
常见问题:Get-AppxPackage 没有输出
如果 Get-AppxPackage -Name "*Ubuntu*" 没有任何输出,说明该发行版不是通过 Microsoft Store 安装的,可能是通过 wsl --install(默认安装到 C:\Users\<用户名>\AppData\Local\Packages)或 wsl --import / wsl --install --location 自定义位置安装的。此时请使用上面的注册表方法查询 BasePath。
网络相关
WSL2 网络架构
Windows Host
┌─────────────────┐
│ Hyper-V │
│ Virtual Switch │
│ (WSL 虚拟网卡) │
└────────┬────────┘
│
┌──────────────┴──────────────┐
│ │
▼ ▼
┌────────────────────┐ ┌────────────────────┐
│ WSL2 Instance │◄──────►│ Windows Apps │
│ 172.x.x.x │ │ 192.168.x.x │
│ (Linux) │ │ (宿主机) │
└────────────────────┘ └────────────────────┘
网络特性
| 特性 | 说明 |
|---|---|
| localhost 互通 | WSL2 与 Windows 通过 127.0.0.1 / localhost 互相访问 |
| 动态 IP | WSL2 每次启动可能获得不同 IP(172.x.x.x) |
| DNS 转发 | WSL2 使用 Windows 主机的 DNS 解析 |
| 防火墙共享 | 受 Windows 防火墙规则影响 |
常用网络命令
# 查看 WSL2 IP 地址
hostname -I
ip addr show eth0
# 查看 Windows 主机 IP(从 WSL2 内)
cat /etc/resolv.conf | grep nameserver
# 测试网络连通性
ping google.com
curl -I https://github.com
# 查看监听端口
ss -tlnp
netstat -tlnp
# 测试端口访问
curl http://localhost:8080
curl http://127.0.0.1:3000
端口访问方式
| 访问方向 | 地址 | 示例 |
|---|---|---|
| WSL2 → Windows | 127.0.0.1 / localhost |
curl http://localhost:3000 |
| Windows → WSL2 | 127.0.0.1 / localhost |
浏览器访问 http://localhost:8080 |
| WSL2 → WSL2 | 127.0.0.1 |
同发行版内访问 |
| 局域网 → WSL2 | 需 Windows 端口转发 | 配置防火墙规则 |
性能与配置
.wslconfig 全局配置
文件位置:C:\Users\<用户名>\.wslconfig
[wsl2]
# 内存限制(建议留 2-4GB 给 Windows)
memory=8GB
# CPU 核心数
processors=4
# 交换空间大小
swap=2GB
# 交换文件路径(可选)
swapFile=D:\WSL\swap.vhdx
# 本地主机端口转发(默认 true)
localhostForwarding=true
# 嵌套虚拟化(运行 KVM/QEMU 需要)
nestedVirtualization=true
# VM 空闲关闭时间(毫秒,0 表示不自动关闭)
vmIdleTimeout=300000
# 是否开启 GUI 应用支持(WSLg)
guiApplications=true
# 调试控制台(调试用)
debugConsole=false
# 网络模式(Windows 11 22H2+)
# networkingMode=mirrored
# DNS 隧道(解决某些网络环境问题)
# dnsTunneling=true
# 防火墙集成(Windows 11 22H2+)
# firewall=true
发行版特定配置(wsl.conf)
文件位置:WSL 内 /etc/wsl.conf
[boot]
# 启动时执行的命令
systemd=true
command = service docker start
[user]
# 默认用户
default = ubuntu
[network]
# 是否生成 /etc/resolv.conf
generateResolvConf = true
# 主机名
hostname = my-wsl
[interop]
# 是否启用 Windows 互操作
enabled = true
appendWindowsPath = true
[automount]
# 是否挂载 Windows 盘符
enabled = true
# 挂载选项
mountFsTab = true
root = /mnt/
options = "metadata,umask=22,fmask=11"
# 驱动器特定配置
[automount]
crossDistro = true
配置生效
# 修改 .wslconfig 或 wsl.conf 后,必须重启 WSL
wsl --shutdown
# 然后重新启动
wsl
磁盘空间管理
# 查看 WSL 虚拟磁盘大小(PowerShell 管理员)
Get-ChildItem -Path "$env:LOCALAPPDATA\Packages" -Recurse -Filter "*.vhdx" | Select-Object FullName, @{N="SizeGB";E={[math]::Round($_.Length/1GB,2)}}
# 手动压缩 VHD(旧版 WSL)
# 注意:wsl --trim-vhd 命令不存在,使用以下方法:
# 方法 1:使用 diskpart(管理员 CMD/PowerShell)
diskpart
# select vdisk file="C:\Users\<用户名>\AppData\Local\Packages\...\LocalState\ext4.vhdx"
# attach vdisk readonly
# compact vdisk
# detach vdisk
# exit
# 方法 2:使用 PowerShell 的 Optimize-VHD(需 Hyper-V 模块)
Optimize-VHD -Path "<path-to-vhdx>" -Mode Full
WSL2 内存自动回收
# 在 WSL2 内手动释放缓存(临时)
sudo sh -c "echo 3 > /proc/sys/vm/drop_caches"
# 或者配置 .wslconfig 限制内存,超限时 WSL 会自动回收
Docker 集成
Docker Desktop WSL2 后端
| 步骤 | 操作 |
|---|---|
| 1. 启用 WSL2 后端 | Docker Desktop → Settings → General → Use the WSL 2 based engine |
| 2. 启用发行版集成 | Settings → Resources → WSL Integration → 勾选目标发行版 |
| 3. 应用重启 | Apply & Restart |
Docker 镜像加速配置
当遇到 TLS handshake timeout 或拉取缓慢时,配置国内镜像源:
Docker Desktop 配置路径:Settings → Docker Engine → 编辑 JSON
{
"builder": {
"gc": {
"defaultKeepStorage": "20GB",
"enabled": true
}
},
"experimental": false,
"registry-mirrors": [
"https://docker.m.daocloud.io",
"https://docker.xuanyuan.me",
"https://docker.1ms.run",
"https://docker-0.unsee.tech"
]
}
验证配置:
docker info | grep -A 10 "Registry Mirrors"
WSL2 内使用 Docker
# 验证 Docker 可用性
docker --version
docker compose version
# 查看 Docker 信息
docker info
# 基本操作
docker ps
docker images
docker compose up -d
docker compose down
Docker 架构
Windows 主机
┌─────────────────────────────────────────┐
│ Docker Desktop (GUI) │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────┐ │
│ │ Docker Daemon │ │
│ │ (运行在 WSL2 的 Linux VM 中) │ │
│ │ ┌─────────────────────────┐ │ │
│ │ │ Container Runtime │ │ │
│ │ │ (containerd + runc) │ │ │
│ │ └─────────────────────────┘ │ │
│ └─────────────────────────────────┘ │
└─────────────────────────────────────────┘
│
│ 通过命名管道 / UNIX socket
▼
WSL2 发行版
┌─────────────────────────────────────────┐
│ Ubuntu / Debian / ... │
│ │ │
│ ▼ │
│ ┌─────────────────────────────────┐ │
│ │ docker CLI (客户端) │ │
│ │ docker compose │ │
│ └─────────────────────────────────┘ │
└─────────────────────────────────────────┘
实用技巧
常用操作速查
| 场景 | 命令 |
|---|---|
| 以 root 身份进入 | wsl -d Ubuntu -u root |
| 备份发行版 | wsl --export Ubuntu D:\backup\ubuntu.tar |
| 恢复/迁移发行版 | wsl --import Ubuntu D:\WSL\Ubuntu D:\backup\ubuntu.tar |
| 设置默认用户(发行版内) | <DistroName> config --default-user <username> |
| 例如 Ubuntu | ubuntu config --default-user johndoe |
| 查看详细发行版信息 | wsl -l -v |
发行版迁移完整流程
# 1. 导出当前发行版
wsl --export Ubuntu D:\backup\ubuntu-backup.tar
# 2. 注销原发行版(会删除原数据,确保导出成功后再执行)
wsl --unregister Ubuntu
# 3. 在新位置导入
wsl --import Ubuntu D:\WSL\Ubuntu D:\backup\ubuntu-backup.tar
# 4. 设置默认用户(导入后默认是 root,需要重新设置)
Ubuntu config --default-user <你的用户名>
# 注意事项:
# - 导入目录不能是 \\wsl$\ 路径
# - 导入后需重新设置默认用户
# - 原发行版数据需提前备份
快速执行命令
# 不进入 WSL,直接执行单条命令
wsl -e echo "Hello from WSL"
wsl -e ls -la /etc
wsl -e pwd
# 执行复杂命令(使用 sh -c)
wsl -- sh -c "df -h && free -h"
# 管道和重定向(从 Windows 侧)
wsl -e ps aux | findstr "nginx"
Shell 别名配置
在 WSL 内的 ~/.bashrc 或 ~/.zshrc 中添加:
# WSL 快捷别名
alias wsl-update='wsl.exe --update'
alias wsl-shutdown='wsl.exe --shutdown'
# 常用目录快捷方式
alias winhome='cd /mnt/c/Users/$USER'
alias windesk='cd /mnt/c/Users/$USER/Desktop'
# 使用 Windows 程序的快捷方式
alias explorer='explorer.exe'
alias code='code.exe'
alias notepad='notepad.exe'
使配置生效:
source ~/.bashrc
Windows 侧 PowerShell 别名
在 PowerShell 的 $PROFILE 中添加:
# WSL 快捷函数
function wsl-start { wsl }
function wsl-stop { wsl --shutdown }
function wsl-list { wsl -l -v }
function wsl-ubuntu { wsl -d Ubuntu }
# 快速进入 WSL 目录
function wsl-cd { Set-Location \\wsl$\Ubuntu\home\$env:USERNAME }
时间同步
WSL2 在休眠/恢复后可能出现时间与 Windows 主机不同步的问题,导致证书验证失败、构建时间戳错误等。
问题表现
# 查看 WSL2 时间(可能落后于 Windows)
date
# 证书验证失败(时间不对导致)
curl https://example.com
# 报错:SSL certificate problem: certificate is not yet valid
# git 操作报错
git pull
# 报错:fatal: unable to access... server certificate verification failed
手动同步时间
# 方法 1:使用 ntpdate(需安装)
sudo apt update && sudo apt install -y ntpdate
sudo ntpdate time.windows.com
# 方法 2:使用 systemd-timesyncd(推荐,系统自带)
sudo systemctl restart systemd-timesyncd
# 方法 3:直接设置时间(需安装 dateutils 或手动计算)
sudo date -s "$(curl -s --head http://www.baidu.com | grep ^Date: | sed 's/Date: //g')"
# 方法 4:从 Windows 主机同步时间(无需外部网络)
sudo hwclock -s
# 或
sudo date -s "$(powershell.exe -Command "Get-Date -Format 'yyyy-MM-dd HH:mm:ss'")"
自动同步配置
在 WSL 内的 ~/.bashrc 或 ~/.zshrc 中添加:
# WSL2 时间自动同步(每次打开 shell 时检查)
# 如果时间差超过 60 秒,则从 Windows 同步
wsl_time_sync() {
local win_time=$(powershell.exe -Command "[DateTimeOffset]::UtcNow.ToUnixTimeSeconds()")
local wsl_time=$(date +%s)
local diff=$((win_time - wsl_time))
# 取绝对值
if [ $diff -lt 0 ]; then
diff=$((-diff))
fi
# 如果时间差超过 60 秒,执行同步
if [ $diff -gt 60 ]; then
echo "[WSL Time Sync] 检测到时间偏差 ${diff} 秒,正在同步..."
sudo hwclock -s 2>/dev/null || sudo date -s "$(powershell.exe -Command 'Get-Date -Format yyyy-MM-dd HH:mm:ss')" 2>/dev/null
echo "[WSL Time Sync] 时间已同步: $(date)"
fi
}
# 执行同步(仅在 WSL 环境中)
if grep -q "microsoft" /proc/version 2>/dev/null; then
wsl_time_sync
fi
使用 cron 定时同步
# 编辑 crontab
sudo crontab -e
# 添加每 10 分钟同步一次(使用 Windows 时间源,无需外网)
*/10 * * * * /sbin/hwclock -s >/dev/null 2>&1
# 或使用 ntpdate(需确保网络可达)
*/10 * * * * /usr/sbin/ntpdate time.windows.com >/dev/null 2>&1
使用 systemd 服务(推荐)
创建服务文件 /etc/systemd/system/wsl-time-sync.service:
[Unit]
Description=WSL2 Time Synchronization from Windows Host
After=network.target
[Service]
Type=oneshot
ExecStart=/bin/bash -c 'date -s "$(powershell.exe -Command \'Get-Date -Format yyyy-MM-dd HH:mm:ss\')"'
创建定时器 /etc/systemd/system/wsl-time-sync.timer:
[Unit]
Description=Run WSL2 time sync every 10 minutes
[Timer]
OnBootSec=1min
OnUnitActiveSec=10min
[Install]
WantedBy=timers.target
启用定时器:
sudo systemctl daemon-reload
sudo systemctl enable --now wsl-time-sync.timer
# 查看状态
sudo systemctl status wsl-time-sync.timer
在 wsl.conf 中配置(部分版本支持)
在 /etc/wsl.conf 中添加:
[boot]
systemd=true
command = /usr/sbin/hwclock -s
注意:
command在较新版本的 WSL 中支持,会在发行版启动时执行。
完整命令速查表
┌─────────────────────────────────────────────────────────────┐
│ WSL2 命令速查 │
├─────────────────────────────────────────────────────────────┤
│ │
│ 安装与升级 │
│ ├─ wsl --install 安装 WSL2 + 默认 Ubuntu │
│ ├─ wsl --install -d <Distro> 安装指定发行版 │
│ ├─ wsl --install -d <D> --name <N> 自定义发行版名称 │
│ ├─ wsl --install -d <D> --location <P> 安装到指定目录 │
│ ├─ wsl --install -d <D> --name <N> --location <P> │
│ │ 自定义名称 + 安装位置 │
│ ├─ wsl --install --no-distribution 仅安装 WSL 内核 │
│ ├─ wsl --update 更新 WSL 内核 │
│ └─ wsl --update --rollback 回滚内核 │
│ │
│ 分发版管理 │
│ ├─ wsl -l 列出发行版 │
│ ├─ wsl -l -v 详细列表(含版本) │
│ ├─ wsl -l -o 可安装的发行版列表 │
│ ├─ wsl --set-default <Distro> 设置默认发行版 │
│ ├─ wsl --set-default-version <N> 设置默认 WSL 版本 │
│ ├─ wsl --set-version <Distro> <N> 转换 WSL 版本 │
│ ├─ wsl --export <Distro> <File> 导出发行版 │
│ ├─ wsl --import <Distro> <Dir> <File> 导入发行版 │
│ ├─ wsl --unregister <Distro> 卸载发行版 │
│ └─ wsl -t <Distro> 终止发行版 │
│ │
│ 启动与连接 │
│ ├─ wsl 启动默认发行版 │
│ ├─ wsl ~ 启动并进入主目录 │
│ ├─ wsl -d <Distro> 启动指定发行版 │
│ ├─ wsl -d <Distro> -u <User> 以指定用户启动 │
│ ├─ wsl -e <Command> 执行命令后退出 │
│ ├─ wsl --shutdown 关闭所有 WSL │
│ └─ wsl -t <Distro> 终止指定发行版(停止指定的 WSL)│
│ │
│ 系统信息 │
│ ├─ wsl --status 查看全局状态 │
│ ├─ wsl --version 查看版本信息 │
│ └─ wsl --hostname 查看主机名 │
│ │
│ 发行版内命令(WSL2 中执行) │
│ ├─ <Distro> config --default-user <User> 设置默认用户 │
│ └─ <Distro> install 安装发行版 │
│ │
└─────────────────────────────────────────────────────────────┘
常见问题
| 问题 | 原因 | 解决方案 |
|---|---|---|
| WSL2 安装失败 | 未启用虚拟化或 Hyper-V | 以管理员运行 wsl --install,确保 BIOS 开启虚拟化 |
错误码 0x800701bc |
WSL 内核版本过旧 | 执行 wsl --update 更新内核 |
错误码 0x80370102 |
虚拟化未启用 | BIOS 中启用 Intel VT-x / AMD-V |
| 内存占用过高 | WSL2 默认无内存限制 | 创建 .wslconfig 限制 memory |
| Vmmem 进程占用大量内存 | WSL2 VM 内存未回收 | 执行 wsl --shutdown 或配置内存限制 |
| 端口无法访问 | Windows 防火墙拦截 | 检查防火墙规则,或关闭防火墙测试 |
| DNS 解析失败 | 公司网络/代理问题 | 配置 wsl.conf 关闭 generateResolvConf,手动设置 DNS |
/mnt 下文件访问慢 |
9P 协议性能限制 | 大文件操作在 Linux 本地文件系统执行 |
| 发行版无法卸载 | 发行版正在运行 | 先 wsl -t <Distro>,再 wsl --unregister |
| 导入后默认用户是 root | 导入不保留用户配置 | 执行 <Distro> config --default-user <用户名> |
| Docker 无法连接 | Docker Desktop 未集成 | 检查 Docker Desktop WSL Integration 设置 |
| 中文显示乱码 | 字符编码问题 | WSL 内安装 locales 并配置 LANG=zh_CN.UTF-8 |
| WSL2 时间不同步 | 休眠恢复后时间停滞 | 执行 sudo hwclock -s 或配置自动同步服务 |
| SSL/证书验证失败 | WSL2 时间落后于实际时间 | 先同步时间,再重试操作 |
提示:使用
wsl --help查看完整帮助信息,或使用wsl <command> --help查看子命令帮助。

浙公网安备 33010602011771号