Skip to content

Instantly share code, notes, and snippets.

@stormwild
Last active April 18, 2025 03:05
Show Gist options
  • Select an option

  • Save stormwild/f464fae904212d4334b3905655a2218c to your computer and use it in GitHub Desktop.

Select an option

Save stormwild/f464fae904212d4334b3905655a2218c to your computer and use it in GitHub Desktop.
Public Server from WSL2

WSL2

Public Server

Running a public server from WSL 2

  1. In WSL, make sure your server is using IPv4.

    Java server default to ipv6, -Djava.net.preferIPv4Stack=true

  2. Allow the port through your WSL firewall

    ufw allow $PORT in WSL

  3. 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.1

    in a Powershell with admin rights

  4. Allow the port through the Windows firewall explicitly by adding a new Inbound Rule using the Windows Defender Firewall with Advanced Security administrative tool. This is accessible as WF.msc in cmd and Powershell. Select Inbound Rule, and click New rule... in the action menu to the right, and work your way through the menu to allow the port explicitly.

  5. In your router, setup port forwarding for the port.

WSL Port Forwarding

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 portproxy or 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 iphlpsv service (IP Helper service) in my firewall rules to allow traffic through to the WSL instance.

Reboot

Rebooting Ubuntu on Windows without rebooting Windows?

At an administrative PowerShell prompt: Restart-Service LxssManager

@mydesravines
Copy link

Can you access a WSL2 server from the internet and how?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment