< Back to blog list

WSL 副环境搭建完整流程

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 /norestart
powershell
1dism /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
powershell
1dism /online /enable-feature /featurename:HypervisorPlatform /all /norestart

2.3 重启

执行完成上述,重启电脑


三、设置 WSL2 默认版本

powershell
1wsl --set-default-version 2
2
3# 验证 WSL 版本
4wsl --version

四、安装 Ubuntu

4.1 查看可用发行版

powershell
1wsl --list --online

4.2 安装 Ubuntu

powershell
1wsl --install -d Ubuntu --no-launch

如果下载超时(错误码 0x80072f78),用手动下载方式:

powershell
1Invoke-WebRequest -Uri "https://aka.ms/wslubuntu" -OutFile "E:\WSL\ubuntu.appx" -UseBasicParsing
powershell
1Add-AppxPackage -Path "E:\WSL\ubuntu.appx"
powershell
1Remove-Item "E:\WSL\ubuntu.appx" -Force

4.3 验证安装

powershell
1wsl --list --verbose

五、首次进入 Ubuntu(设置用户)

powershell
1wsl -d Ubuntu

按提示设置用户名和密码,然后退出:

bash
1exit
2
3# 或
4
5Ctrl+D 快捷键

六、迁移到 E 盘

6.1 关闭 WSL

powershell
1wsl --shutdown

6.2 创建目标目录

powershell
1New-Item -Path "E:\WSL\Ubuntu" -ItemType Directory -Force

6.3 导出 Ubuntu

powershell
1wsl --export Ubuntu "E:\WSL\ubuntu-export.tar"

6.4 注销原有 Ubuntu

powershell
1wsl --unregister Ubuntu

6.5 导入到 E 盘

powershell
1wsl --import Ubuntu "E:\WSL\Ubuntu" "E:\WSL\ubuntu-export.tar" --version 2

6.6 清理导出文件

powershell
1Remove-Item "E:\WSL\ubuntu-export.tar" -Force

6.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 --shutdown
powershell
1wsl --unregister Ubuntu
powershell
1Remove-Item "E:\WSL\Ubuntu" -Recurse -Force

< Back to blog list

<BLOG>

</BLOG>