Running a public server from WSL 2
-
In WSL, make sure your server is using IPv4.
Java server default to ipv6, -Djava.net.preferIPv4Stack=true
-
Allow the port through your WSL firewall
ufw allow $PORTin WSL -
Forward your port from the public IP port to the WSL port using
netsh interface portproxy add v4tov4 listenport=$PORT listenaddress=0.0.0.0 connectport=$PORT connectaddress=127.0.0.1in a Powershell with admin rights
-
Allow the port through the Windows firewall explicitly by adding a new
Inbound Ruleusing the Windows Defender Firewall with Advanced Security administrative tool. This is accessible asWF.mscincmdand Powershell. SelectInbound Rule, and clickNew rule...in the action menu to the right, and work your way through the menu to allow the port explicitly. -
In your router, setup port forwarding for the port.
Since WSL2 uses virtual network adapters, its IP address resets after every startup.
Allow Server Running Inside WSL To Be Accessible Outside Windows 10 Host
WSL-2: Which ports are automatically forwarded?
My confusion came from firewall issues with WSL-2.
What I have found is:
- Services listening on ports in WSL-2 are accesible from the host machine as
localhost:<port>- WSL-2 ports are not accessible from outside of the host machine
- WSL-2 ports can be made available through
netstat interface portproxyor other portforward tools using the ip address of the WSL instance.The firewall did not allow acces to the WSL ports by just listing the ports. I had to specifcally select the
iphlpsvservice (IP Helper service) in my firewall rules to allow traffic through to the WSL instance.
Rebooting Ubuntu on Windows without rebooting Windows?
At an administrative PowerShell prompt:
Restart-Service LxssManager
Can you access a WSL2 server from the internet and how?