windows10 WSL setup

WSL 安装系统

推荐 Ubuntu-22.04 Ubuntu 22.04 LTS

wsl.exe --install --d Ubuntu-22.04

WSL 卸载系统

wsl -l -v
wsl --unregister Debian

WSL kernel 客制化

首先运行 wsl 通过如下命令确认当前内核版本:

uname -a
Linux x 6.6.87.2-microsoft-standard-WSL2+ #1 SMP PREEMPT_DYNAMIC Sun Jun 15 21:13:48 CST 2025 x86_64 x86_64 x86_64 GNU/Linux

克隆源代码

git clone https://github.com/microsoft/WSL2-Linux-Kernel.git --depth=1 -b linux-msft-wsl-6.6.y
cd WSL2-Linux-Kernel
git tag -l | grep "6.6.87"
git checkout -b my-6.6.87.2-kernel linux-msft-wsl-6.6.87.2
  • --depth=1 clones only the latest commit to save time and space.
  • -b linux-msft-wsl-6.6.y specifies the branch to clone.

目前 github https://github.com/microsoft/WSL2-Linux-Kernel 主要版本:

  • linux-msft-wsl-6.6.y
  • linux-msft-wsl-4.19.y
  • linux-msft-wsl-5.4.y
  • linux-msft-wsl-5.10.y
  • linux-msft-wsl-5.15.y
  • linux-msft-wsl-6.1.y
  • master

安装编译环境

sudo apt update && sudo apt install build-essential flex bison libssl-dev libelf-dev bc python3 pahole cpio

配置内核

cp /proc/config.gz config.gz
gunzip config.gz
cp config .config
make menuconfig
make -j
sudo make modules_install -j
sudo make install -j
cp vmlinux /mnt/c/Users/luyang/

配置 WSL

注意用户名根据实际进行修改

window 用户目录下(例如:C:\Users\luyang)创建文件: .wslconfig

文件内容如下:

[wsl2]
kernel=C:\\Users\\luyang\\vmlinux
# Limits VM memory to use no more than 16GB.
# Set this based on your host's total RAM. For a 32GB host, 16GB is a good start.
memory=16GB

# How much swap space to add to the WSL 2 VM.
# 0 for no swap file. A good starting point is equal to or double your memory setting.
# For a build like Cuttlefish, you might need a lot, e.g., 20GB or more.
swap=20GB

# Absolute Windows path to the swap virtual hard disk.
# This file will be created/managed by WSL. Ensure the path exists.
# Use double backslashes for Windows paths.
swapFile=C:\\wsl_swap\\wsl-swap.vhdx

# How many processors to assign to the WSL 2 VM.
# Set this to a reasonable number of your CPU cores, e.g., half or two-thirds.
# For heavy builds, too many cores with insufficient RAM can also lead to OOM.
processors=8

wsl 挂载 ext4 移动硬盘

windows powershell 管理员权限运行

PS C:\Users\luyang> GET-CimInstance -query "SELECT * from Win32_DiskDrive"

DeviceID           Caption              Partitions Size          Model
--------           -------              ---------- ----          -----
\\.\PHYSICALDRIVE2 WD Green SN350 1TB   1          1000202273280 WD Green SN350 1TB
\\.\PHYSICALDRIVE1 WDC WD40EJRX-89AKWY0 1          4000784417280 WDC WD40EJRX-89AKWY0
\\.\PHYSICALDRIVE0 WDC WD10EZEX-08WN4A0 3          1000202273280 WDC WD10EZEX-08WN4A0

windows 挂载

PS C:\Windows\system32> wsl --mount \\.\PHYSICALDRIVE3 --bare
操作成功完成。

wsl 终端挂载

sudo fdisk -l

Device     Boot Start        End    Sectors   Size Id Type
/dev/sdc1        2048 1000148991 1000146944 476.9G 83 Linux


sudo mount -t ext4 /dev/sdc1 /mnt

wsl 终端卸载

sudo umount -t ext4 /dev/sdc1 /mnt

windows 卸载

wsl --unmount \\.\PHYSICALDRIVE3 --bare