wsl 主页
目录
- 安装卸载列出 WSL 发行版
- WSL kernel 客制化
- wsl 挂载 ext4 移动硬盘
安装卸载列出 WSL 发行版
# 列出当前已安装的所有WSL发行版(包括正在运行和已停止的)
wsl --list
# 列出微软官方提供的、可在线安装的所有WSL发行版
wsl --list --online
# 安装指定的WSL发行版,这里是Ubuntu 24.04版本
wsl --install -d Ubuntu-24.04
# 彻底卸载并删除名为Ubuntu-24.04的WSL发行版(数据会全部清除)
wsl --unregister Ubuntu-24.04
C:\Users\max_h>wsl --list
适用于 Linux 的 Windows 子系统分发:
Ubuntu-24.04 (默认)
Ubuntu-20.04
C:\Users\max_h>wsl --list --online
以下是可安装的有效分发的列表。
使用 'wsl.exe --install <Distro>' 安装。
NAME FRIENDLY NAME
Ubuntu Ubuntu
Debian Debian GNU/Linux
kali-linux Kali Linux Rolling
Ubuntu-18.04 Ubuntu 18.04 LTS
Ubuntu-20.04 Ubuntu 20.04 LTS
Ubuntu-22.04 Ubuntu 22.04 LTS
Ubuntu-24.04 Ubuntu 24.04 LTS
OracleLinux_7_9 Oracle Linux 7.9
OracleLinux_8_7 Oracle Linux 8.7
OracleLinux_9_1 Oracle Linux 9.1
openSUSE-Leap-15.6 openSUSE Leap 15.6
SUSE-Linux-Enterprise-15-SP5 SUSE Linux Enterprise 15 SP5
SUSE-Linux-Enterprise-15-SP6 SUSE Linux Enterprise 15 SP6
openSUSE-Tumbleweed openSUSE Tumbleweed
C:\Users\max_h>wsl --install -d Ubuntu-24.04
Ubuntu 24.04 LTS 已安装。
正在启动 Ubuntu 24.04 LTS...
Installing, this may take a few minutes...
Please create a default UNIX user account. The username does not need to match your Windows username.
For more information visit: https://aka.ms/wslusers
Enter new UNIX username: luyang
New password:
Retype new password:
passwd: password updated successfully
Installation successful!
To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.
Welcome to Ubuntu 24.04.1 LTS (GNU/Linux 5.15.167.4-microsoft-standard-WSL2 x86_64)
* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/pro
System information as of Wed Mar 19 14:19:04 CST 2025
System load: 0.21 Processes: 31
Usage of /: 0.1% of 1006.85GB Users logged in: 0
Memory usage: 3% IPv4 address for eth0: 172.20.24.196
Swap usage: 0%
This message is shown once a day. To disable it please create the
/home/luyang/.hushlogin file.
C:\Users\max_h>wsl --unregister Ubuntu-24.04
正在注销。
操作成功完成。
C:\Users\max_h>wsl --list
适用于 Linux 的 Windows 子系统没有安装的分发版。
使用 'wsl.exe --list --online' 列出可用的分发版
和 'wsl.exe --install <Distro>' 进行安装。
也可以通过访问 Microsoft Store 来安装分发版:
https://aka.ms/wslstore
错误代码: Wsl/WSL_E_DEFAULT_DISTRO_NOT_FOUND
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
WSL 端口映射
查询当前已经转发的端口
如何查看当前 windows -> WSL 配置了哪些端口转发? 以管理员身份运行 PowerShell:
PS C:\WINDOWS\system32> netsh interface portproxy show all
侦听 ipv4: 连接到 ipv4:
地址 端口 地址 端口
--------------- ---------- --------------- ----------
0.0.0.0 8080 172.18.150.174 80
0.0.0.0 9999 172.25.198.88 9999
127.0.0.1 562 172.25.198.88 562
0.0.0.0 562 172.25.198.88 562
0.0.0.0 4865 172.25.198.88 4865
0.0.0.0 6666 172.25.198.88 6666
确认 WSL IP 地址
WSL 终端运行:
$ ifconfig
docker0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.17.0.1 netmask 255.255.0.0 broadcast 172.17.255.255
inet6 fe80::42:d3ff:fe45:67a4 prefixlen 64 scopeid 0x20<link>
ether 02:42:d3:45:67:a4 txqueuelen 0 (Ethernet)
RX packets 12553 bytes 108393702 (108.3 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 15258 bytes 1786658 (1.7 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.25.198.88 netmask 255.255.240.0 broadcast 172.25.207.255
inet6 fe80::215:5dff:feec:a420 prefixlen 64 scopeid 0x20<link>
ether 00:15:5d:ec:a4:20 txqueuelen 1000 (Ethernet)
RX packets 201105667 bytes 290122284896 (290.1 GB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 55298591 bytes 3209600213 (3.2 GB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 217290 bytes 624612200 (624.6 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 217290 bytes 624612200 (624.6 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
veth11e7cb7: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet6 fe80::fcfb:17ff:fe7d:ad3 prefixlen 64 scopeid 0x20<link>
ether fe:fb:17:7d:0a:d3 txqueuelen 0 (Ethernet)
RX packets 12553 bytes 108569444 (108.5 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 15279 bytes 1788224 (1.7 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
如上: eth0
172.25.198.88
增加端口4864
映射
netsh interface portproxy add v4tov4 listenaddress=0.0.0.0 listenport=4864 connectaddress=172.25.198.88 4864
PS C:\WINDOWS\system32> netsh interface portproxy show all
侦听 ipv4: 连接到 ipv4:
地址 端口 地址 端口
--------------- ---------- --------------- ----------
0.0.0.0 8080 172.18.150.174 80
0.0.0.0 9999 172.25.198.88 9999
127.0.0.1 562 172.25.198.88 562
0.0.0.0 562 172.25.198.88 562
0.0.0.0 4865 172.25.198.88 4865
0.0.0.0 6666 172.25.198.88 6666
0.0.0.0 4864 172.25.198.88 4864
删除端口4864
映射
netsh interface portproxy delete v4tov4 listenport=4864 listenaddress=0.0.0.0
PS C:\WINDOWS\system32> netsh interface portproxy show all
侦听 ipv4: 连接到 ipv4:
地址 端口 地址 端口
--------------- ---------- --------------- ----------
0.0.0.0 8080 172.18.150.174 80
0.0.0.0 9999 172.25.198.88 9999
127.0.0.1 562 172.25.198.88 562
0.0.0.0 562 172.25.198.88 562
0.0.0.0 4865 172.25.198.88 4865
0.0.0.0 6666 172.25.198.88 6666
From: https://learn.microsoft.com/en-us/windows/wsl/networking
Here’s an example of using the Netsh interface portproxy Windows command to add a port proxy that listens on your host port and connects that port proxy to the IP address for the WSL 2 VM.
netsh interface portproxy add v4tov4 listenport=<yourPortToForward> listenaddress=0.0.0.0 connectport=<yourPortToConnectToInWSL> connectaddress=(wsl hostname -I)
In this example, you will need to update <yourPortToForward>
to a port number, for example listenport=4000
. listenaddress=0.0.0.0
means that incoming requests will be accepted from ANY IP address. The Listen Address specifies the IPv4 address for which to listen and can be changed to values that include: IP address, computer NetBIOS name, or computer DNS name. If an address isn’t specified, the default is the local computer. You need to update the <yourPortToConnectToInWSL>
value to a port number where you want WSL to connect, for example connectport=4000
. Lastly, the connectaddress
value needs to be the IP address of your Linux distribution installed via WSL 2 (the WSL 2 VM address), which can be found by entering the command: wsl.exe hostname -I
.
So this command may look something like:
netsh interface portproxy add v4tov4 listenport=4000 listenaddress=0.0.0.0 connectport=4000 connectaddress=192.168.101.100
To obtain the IP address, use:
wsl hostname -I
for the IP address of your Linux distribution installed via WSL 2 (the WSL 2 VM address)cat /etc/resolv.conf
for the IP address of the Windows machine as seen from WSL 2 (the WSL 2 VM)
Using listenaddress=0.0.0.0
will listen on all IPv4 ports.
Using a lowercase "i" with the hostname command will generate a different result than using an uppercase "I". wsl hostname -i is your local machine (127.0.1.1 is a placeholder diagnostic address), whereas wsl hostname -I will return your local machine's IP address as seen by other machines and should be used to identify the connectaddress of your Linux distribution running via WSL 2.