Skip to content

WSL

WSL Documentation

WSL Github

WSL2 Fixes

安装

开启系统组件,包含子系统和虚拟机平台。

bash
# 启用WSL
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
# 启动虚拟机平台
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

在微软商店,安装最新版的子系统,然后安装内核补丁包。

WSL2 Linux kernel update package for x64 machines

设置默认版本

bash
wsl --set-default-version 2

常用操作

bash
# ================================================================================================================
# 基础操作
# ================================================================================================================
# 更新到最新版
wsl --update
# 查看当前版本
wsl --version
# 查看有效发行版列表
wsl --list --online
# 列出所有已安装发行版
wsl -l -v
# 停止
wsl --shutdown
# ================================================================================================================
# Alma Linux
# ================================================================================================================
# 安装
wsl --install -d AlmaLinuxOS-9
# 卸载
wsl --unregister AlmaLinuxOS-9
# 运行
wsl -d AlmaLinuxOS-9
# 终止
wsl --terminate AlmaLinuxOS-9
# ================================================================================================================
# Ubuntu
# ================================================================================================================
# 安装
wsl --install -d Ubuntu
# 卸载
wsl --unregister Ubuntu
# 运行
wsl -d Ubuntu
# 终止
wsl --terminate Ubuntu

常见问题

Error code: Wsl/Service/0x8007273d

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

bash
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

Systemd

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

增加以下内容

bash
[boot]
systemd=true

重启 WSL

bash
wsl.exe --shutdown

内存

在当前用户目录下新建.wslconfig文件,增加以下内容

[wsl2]
memory=2GB
swap=4GB

重启WSL

bash
wsl.exe --shutdown

检查是否成功

bash
free -m

网络

默认网络

有需要对局域网内暴露服务时,可以在启动后拿到实际IP后,配置好端口转发

shell
#
# 设置端口转发
#
netsh interface portproxy add v4tov4 listenport=80 listenaddress=0.0.0.0 connectport=80 connectaddress=172.19.74.66
netsh interface portproxy add v4tov4 listenport=22 listenaddress=0.0.0.0 connectport=22 connectaddress=172.19.74.66
netsh interface portproxy add v4tov4 listenport=5672 listenaddress=0.0.0.0 connectport=5672 connectaddress=172.19.74.66
netsh interface portproxy add v4tov4 listenport=15672 listenaddress=0.0.0.0 connectport=15672 connectaddress=172.19.74.66
netsh interface portproxy add v4tov4 listenport=6379 listenaddress=0.0.0.0 connectport=6379 connectaddress=172.19.74.66
netsh interface portproxy add v4tov4 listenport=9092 listenaddress=0.0.0.0 connectport=9092 connectaddress=172.19.74.66
#
# 重置端口转发
#
netsh interface portproxy reset

桥接网络

桥接网络必须要求系统是Win11 22H2以上版本

https://www.cnblogs.com/ywhb/p/17156461.html

bash
# 查看当前所有网卡
Get-NetAdapter
# 桥接主机无线网络和WSL虚拟网络
Set-VMSwitch WSL -NetAdapterName WLAN
Set-VMSwitch WSLBridge -NetAdapterName WLAN
# 取消桥接
Set-VMSwitch WSL -SwitchType Internal
Get-VMSwitch -SwitchType External

配置文件

[wsl2]
networkingMode=bridged
vmSwitch=WSL-External
ipv6=false
memory=2GB
swap=4GB

开发环境

bash
# 常用工具安装
sudo yum install net-tools  
sudo yum install iproute
# 创建软连接指向Windows系统的目录
sudo ln -s /mnt/d/Workspace/WSL /data

宿主机开启端口

如果需要内网访问,那么需要在宿主电脑上开启对应的端口

bash
# 防火墙
netsh advfirewall firewall add rule name=HTTP dir=in action=allow protocol=TCP localport=80
netsh advfirewall firewall add rule name=HTTPS dir=in action=allow protocol=TCP localport=443
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