Skip to content

Instantly share code, notes, and snippets.

@vagetablechicken
Created August 12, 2024 02:28
Show Gist options
  • Save vagetablechicken/1f30f57c178536aa6135ea06b33d66ec to your computer and use it in GitHub Desktop.
Save vagetablechicken/1f30f57c178536aa6135ea06b33d66ec to your computer and use it in GitHub Desktop.
WSL2 网络配置

How to make LAN access wsl2 ports

wsl2 mirrored mode is not experimental now, but some tutorial still use the old style.

WSL 版本: 2.2.4.0
内核版本: 5.15.153.1-2
WSLg 版本: 1.0.61
MSRDC 版本: 1.2.5326
Direct3D 版本: 1.611.1-81528511
DXCore 版本: 10.0.26091.1-240325-1447.ge-release
Windows 版本: 10.0.22631.3880

First, the kernel should be 2.2.4. Be careful about the upgrade, plz make backup. I failed in one machine, and reinstalled the kernel. Then, set the ~/.wslconfig in win11:

[wsl2]
networkingMode=mirrored # 开启镜像网络
dnsTunneling=true # 开启 DNS Tunneling
firewall=true # 开启 Windows 防火墙
autoProxy=true # 开启自动同步代理
[experimental]
hostAddressLoopback=true

wsl2 shutdown and start again.

How to check

To make wsl2 accessiable in LAN, binding 0.0.0.0 is not working for me. So bind 192.168.x.x. In wsl2:

python3 -m http.server --bind 192.168.x.x

In win11 powershell or web browser, you can access it.

To make LAN access it, let it pass the firewall:

sudo New-NetFirewallRule -DisplayName "允许 HTTP8000" -Direction Inbound -LocalPort 8000 -Protocol TCP -Action Allow
Get-NetFirewallRule -DisplayName "允许 HTTP8000"
# delete
sudo Remove-NetFirewallRule -DisplayName "允许 HTTP8000"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment