Skip to content

WSL

WSL Github

常用操作

bash
# 启用
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
#
wsl --update
#
wsl --set-default-version 2
# 查看有效发行版列表
wsl --list --online
# 列出所有已安装发行版
wsl -l -v
# 停止
wsl --shutdown
# ================================================================================================================
# AlmaLinux
# 安装
wsl --install -d AlmaLinux9
# 卸载
wsl --unregister AlmaLinux9
# 运行
wsl -d AlmaLinux9
# 终止
wsl --terminate AlmaLinux9
# ================================================================================================================
# Ubuntu
# 安装
wsl --install -d Ubuntu
# 卸载
wsl --unregister Ubuntu
# 运行
wsl -d Ubuntu
# 终止
wsl --terminate Ubuntu
# 启用
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
#
wsl --update
#
wsl --set-default-version 2
# 查看有效发行版列表
wsl --list --online
# 列出所有已安装发行版
wsl -l -v
# 停止
wsl --shutdown
# ================================================================================================================
# AlmaLinux
# 安装
wsl --install -d AlmaLinux9
# 卸载
wsl --unregister AlmaLinux9
# 运行
wsl -d AlmaLinux9
# 终止
wsl --terminate AlmaLinux9
# ================================================================================================================
# Ubuntu
# 安装
wsl --install -d Ubuntu
# 卸载
wsl --unregister Ubuntu
# 运行
wsl -d Ubuntu
# 终止
wsl --terminate Ubuntu

常见问题

Error code: Wsl/Service/0x8007273d

执行下面命令,然后重启系统

bash
netsh winsock reset
netsh winsock reset

参考的对象类型不支持尝试的操作

保存后双击保存进注册表,解决问题

bash
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WinSock2\Parameters\AppId_Catalog\0408F7A3]
"AppFullPath"="C:\\Windows\\System32\\wsl.exe"
"PermittedLspCategories"=dword:80000000
Windows Registry Editor Version 5.00
[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\WinSock2\Parameters\AppId_Catalog\0408F7A3]
"AppFullPath"="C:\\Windows\\System32\\wsl.exe"
"PermittedLspCategories"=dword:80000000

systemmd

https://devblogs.microsoft.com/commandline/systemd-support-is-now-available-in-wsl/

https://www.jianshu.com/p/b7a978ed1e77

新建并打开 wsl.conf 文件

bash
#
sudo touch /etc/wsl.conf
#
sudo vi /etc/wsl.conf
#
sudo touch /etc/wsl.conf
#
sudo vi /etc/wsl.conf

增加以下内容,用于启动 systemmd

bash
[boot]
systemd=true
[boot]
systemd=true

重启 WSL

bash
wsl.exe --shutdown
wsl.exe --shutdown

固定 IP

https://github.com/microsoft/WSL/issues/4210#issuecomment-648570493

https://www.zhihu.com/question/387747506

https://zhuanlan.zhihu.com/p/380779630

WSL 在每次启动的时候,都会自动分配地址,地址是随机的。所以在启动后,删除已分配地址,手工添加固定的地址,处理好端口转发就可以 实现固定地址。

可以将一下内容保存成 WSL.bat 文件,已管理员运行即可启动固定地址的 WSL。

bash
# 删除已分配地址并手工指定固定地址
wsl -d AlmaLinux9 -u root ip addr del $(ip addr show eth0 ^| grep 'inet\b' ^| awk '{print $2}' ^| head -n 1) dev eth0
wsl -d AlmaLinux9 -u root ip addr add 192.168.10.10/24 broadcast 192.168.10.255 dev eth0
wsl -d AlmaLinux9 -u root ip route add 0.0.0.0/0 via 192.168.10.1 dev eth0
wsl -d AlmaLinux9 -u root echo nameserver 192.168.10.1 ^> /etc/resolv.conf
#
powershell -c "Get-NetAdapter 'vEthernet (WSL)' | Get-NetIPAddress | Remove-NetIPAddress -Confirm:$False; New-NetIPAddress -IPAddress 192.168.10.1 -PrefixLength 24 -InterfaceAlias 'vEthernet (WSL)'; Get-NetNat | ? Name -Eq WSLNat | Remove-NetNat -Confirm:$False; New-NetNat -Name WSLNat -InternalIPInterfaceAddressPrefix 192.168.10.0/24;"
# 端口监听分发
netsh interface portproxy add v4tov4 listenport=22 listenaddress=0.0.0.0 connectport=22 connectaddress=192.168.10.10
netsh interface portproxy add v4tov4 listenport=3306 listenaddress=0.0.0.0 connectport=3306 connectaddress=192.168.10.10
netsh interface portproxy add v4tov4 listenport=5672 listenaddress=0.0.0.0 connectport=5672 connectaddress=192.168.10.10
netsh interface portproxy add v4tov4 listenport=15672 listenaddress=0.0.0.0 connectport=15672 connectaddress=192.168.10.10
netsh interface portproxy add v4tov4 listenport=6379 listenaddress=0.0.0.0 connectport=6379 connectaddress=192.168.10.10
#
wt -p AlmaLinux9
# 删除已分配地址并手工指定固定地址
wsl -d AlmaLinux9 -u root ip addr del $(ip addr show eth0 ^| grep 'inet\b' ^| awk '{print $2}' ^| head -n 1) dev eth0
wsl -d AlmaLinux9 -u root ip addr add 192.168.10.10/24 broadcast 192.168.10.255 dev eth0
wsl -d AlmaLinux9 -u root ip route add 0.0.0.0/0 via 192.168.10.1 dev eth0
wsl -d AlmaLinux9 -u root echo nameserver 192.168.10.1 ^> /etc/resolv.conf
#
powershell -c "Get-NetAdapter 'vEthernet (WSL)' | Get-NetIPAddress | Remove-NetIPAddress -Confirm:$False; New-NetIPAddress -IPAddress 192.168.10.1 -PrefixLength 24 -InterfaceAlias 'vEthernet (WSL)'; Get-NetNat | ? Name -Eq WSLNat | Remove-NetNat -Confirm:$False; New-NetNat -Name WSLNat -InternalIPInterfaceAddressPrefix 192.168.10.0/24;"
# 端口监听分发
netsh interface portproxy add v4tov4 listenport=22 listenaddress=0.0.0.0 connectport=22 connectaddress=192.168.10.10
netsh interface portproxy add v4tov4 listenport=3306 listenaddress=0.0.0.0 connectport=3306 connectaddress=192.168.10.10
netsh interface portproxy add v4tov4 listenport=5672 listenaddress=0.0.0.0 connectport=5672 connectaddress=192.168.10.10
netsh interface portproxy add v4tov4 listenport=15672 listenaddress=0.0.0.0 connectport=15672 connectaddress=192.168.10.10
netsh interface portproxy add v4tov4 listenport=6379 listenaddress=0.0.0.0 connectport=6379 connectaddress=192.168.10.10
#
wt -p AlmaLinux9

如果是内网访问,需要在防火墙开启对应端口。

bash
# 防火墙
netsh advfirewall firewall add rule name=WSL dir=in action=allow protocol=TCP localport=22
netsh advfirewall firewall add rule name=MySQL dir=in action=allow protocol=TCP localport=3306
netsh advfirewall firewall add rule name=RabbitMQ dir=in action=allow protocol=TCP localport=5672
netsh advfirewall firewall add rule name=RabbitMQ dir=in action=allow protocol=TCP localport=15672
netsh advfirewall firewall add rule name=Redis dir=in action=allow protocol=TCP localport=6379
# 防火墙
netsh advfirewall firewall add rule name=WSL dir=in action=allow protocol=TCP localport=22
netsh advfirewall firewall add rule name=MySQL dir=in action=allow protocol=TCP localport=3306
netsh advfirewall firewall add rule name=RabbitMQ dir=in action=allow protocol=TCP localport=5672
netsh advfirewall firewall add rule name=RabbitMQ dir=in action=allow protocol=TCP localport=15672
netsh advfirewall firewall add rule name=Redis dir=in action=allow protocol=TCP localport=6379

开发环境

bash
# 在D盘新建数据目录
mkdir -p /data
# 创建软连接指向Windows系统的目录
sudo ln -s /mnt/d/WSL/data /data
# 在D盘新建数据目录
mkdir -p /data
# 创建软连接指向Windows系统的目录
sudo ln -s /mnt/d/WSL/data /data