WSL 副环境搭建完整流程
一、前置验证
1.1 硬件虚拟化检查
powershell
1systeminfo | Select-String "Hyper-V"预期输出:
Hyper-V 要求: 已检测到虚拟机监控程序。将不显示 Hyper-V 所需的功能。
1.3 确认现有 WSL 状态
powershell
1wsl --list --verbose二、启用 Windows 功能(如已启用则跳过)
警告
以下命令需要管理员权限。如果现有 WSL 已正常运行,说明功能已启用,可直接跳到第三步。
2.1 检查功能是否已启用
powershell
1dism /online /get-features /format:table | Select-String "VirtualMachinePlatform|Microsoft-Windows-Subsystem-Linux|HypervisorPlatform"如果三项都是「已启用」,跳到第三步。
2.2 启用功能
逐条执行,每条会提示需要重启:
powershell
1dism /online /enable-feature /featurename:VirtualMachinePlatform /all /norestartpowershell
1dism /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestartpowershell
1dism /online /enable-feature /featurename:HypervisorPlatform /all /norestart2.3 重启
执行完成上述,重启电脑
三、设置 WSL2 默认版本
powershell
1wsl --set-default-version 2
2
3# 验证 WSL 版本
4wsl --version四、安装 Ubuntu
4.1 查看可用发行版
powershell
1wsl --list --online4.2 安装 Ubuntu
powershell
1wsl --install -d Ubuntu --no-launch如果下载超时(错误码 0x80072f78),用手动下载方式:
powershell
1Invoke-WebRequest -Uri "https://aka.ms/wslubuntu" -OutFile "E:\WSL\ubuntu.appx" -UseBasicParsingpowershell
1Add-AppxPackage -Path "E:\WSL\ubuntu.appx"powershell
1Remove-Item "E:\WSL\ubuntu.appx" -Force4.3 验证安装
powershell
1wsl --list --verbose五、首次进入 Ubuntu(设置用户)
powershell
1wsl -d Ubuntu按提示设置用户名和密码,然后退出:
bash
1exit
2
3# 或
4
5Ctrl+D 快捷键六、迁移到 E 盘
6.1 关闭 WSL
powershell
1wsl --shutdown6.2 创建目标目录
powershell
1New-Item -Path "E:\WSL\Ubuntu" -ItemType Directory -Force6.3 导出 Ubuntu
powershell
1wsl --export Ubuntu "E:\WSL\ubuntu-export.tar"6.4 注销原有 Ubuntu
powershell
1wsl --unregister Ubuntu6.5 导入到 E 盘
powershell
1wsl --import Ubuntu "E:\WSL\Ubuntu" "E:\WSL\ubuntu-export.tar" --version 26.6 清理导出文件
powershell
1Remove-Item "E:\WSL\ubuntu-export.tar" -Force6.7 设置默认用户
警告
导入后默认用户变为 root,需要重置。将 <用户名> 替换为你首次进入时设置的用户名:
powershell
1wsl -d Ubuntu-24.04 -u root -e bash -c "echo -e '[user]\ndefault=wave' >> /etc/wsl.conf"
2
3wsl --terminate Ubuntu-24.04
4
5wsl -d Ubuntu-24.04 -- whoami七、验证
powershell
1wsl --list --verbose预期输出:
NAME STATE VERSION
* Ubuntu Running 2
Debian Stopped 2
powershell
1Get-ChildItem "E:\WSL\Ubuntu"预期:看到 ext4.vhdx 文件。
powershell
1wsl -d Ubuntu -- cat /etc/os-release预期:显示 Ubuntu 版本信息。
八、使用与删除
日常使用
powershell
1wsl -d Ubuntu删除副环境(不影响 Debian)
powershell
1wsl --shutdownpowershell
1wsl --unregister Ubuntupowershell
1Remove-Item "E:\WSL\Ubuntu" -Recurse -Force< Back to blog list