开启“镜像网络模式”(最推荐,Win11 首选)
这是微软为 Windows 11 专门开发的特性。开启后,WSL2 会直接共享 Windows 的网络堆栈。局域网用户访问 Windows 的 IP,就等同于直接访问 WSL2。
在 Windows 中按下 Win + R,输入 %UserProfile% 并回车。
创建或编辑 .wslconfig 文件,添加以下配置:
[wsl2]
networkingMode=mirrored
关键步骤:开启此模式后,你必须在 Windows 防火墙中手动放行对应的端口,或者直接运行以下管理员 PowerShell 命令放行所有 WSL 流量:
New-NetFirewallRule -DisplayName "WSL Mirrored Mode" -Direction Inbound -Action Allow -Protocol TCP
New-NetFirewallRule -DisplayName "WSL Mirrored Mode" -Direction Inbound -Action Allow -Protocol UDP
重启 WSL:在 PowerShell 运行 wsl --shutdown。
访问方式:局域网设备直接访问 [Windows的局域网IP]:[端口号]。