Enable Home PC (WSL2) to Act as a Server
I wanted to set up my Windows PC running WSL2 so it could act as a server accessible from my local network (LAN). WSL2 uses a virtual network, which means its IP address is usually not directly reachable from the LAN. To solve this, I used Windows’ port forwarding with netsh
.
Reference: Port Forwarding WSL 2 to Your LAN
Here’s a quick note on how I did it.
1. Check WSL2 Network Interface
In WSL2, find your IP address:
ifconfig
2. Check Existing Port Proxy Rules
In PowerShell, list any existing port proxy rules:
netsh interface portproxy show v4tov4
3. Add Port Forwarding Rule
In PowerShell, set up a port forwarding rule to forward a port (e.g., 25565
) from the Windows host to the WSL2 guest:
netsh interface portproxy add v4tov4 listenport=25565 listenaddress=0.0.0.0 connectport=25565 connectaddress=<WSL2_IP>
Replace <WSL2_IP>
with the WSL2 address you found from ifconfig
.
For example, if your WSL2 IP is 172.18.141.83
:
netsh interface portproxy add v4tov4 listenport=25565 listenaddress=0.0.0.0 connectport=25565 connectaddress=172.18.141.83
4. Remove Port Forwarding Rule
To remove the rule if needed:
netsh interface portproxy delete v4tov4 listenport=25565 listenaddress=0.0.0.0
5️. Start a Simple Server in WSL2
For testing, you can quickly launch an HTTP server on port 25565:
python3 -m http.server 25565
6. Allow Inbound Connections in Windows Firewall
You may need to open up the ports in Windows Defender Firewall:
New-NetFirewallRule -DisplayName "WSL2 Port Bridge" -Direction Inbound -Action Allow -Protocol TCP -LocalPort 80,443,10000,3000,5000
Or you can add rules manually in Windows Defender Firewall to allow inbound traffic on the required ports.
Done!
Now your WSL2 server should be accessible from other devices on your local network via the forwarded port.