Linux – Network problems using WSL2

networkingwindows 10windows-subsystem-for-linuxwsl2

Currently I'm using Windows10 and WSL2 with Ubuntu 20.04 for developing. For the most case this works excellent, apart from one problem. There seems to be something broken in the networking layer between Windows/WSL2. When serving angular using 'ng serve', I can't access it from Windows.

Only when using the terminal in VS Code, and opening the browser from the link in the terminal output, will Windows open it – however, it does not open 'localhost:4200', it opens 127.0.0.1:'random port' which is not allowed by our proxy, which means the angular application will show up but not be usable. After doing this, the application is available from localhost:4201 – but never before opening it from VS Code. This means I cannot use other terminals (like the new Windows Terminal or ConEmu) to start the applications.

As far as I can tell this is a Windows-problem rather than a problem with the Linux-installation itself, but I have not found a way to 'reinstall' the Windows-part of WSL2 without losing all data on the Linux-installation. Any pointers would be appreciated.

Best Answer

My answer is based on the article Running a public server from WSL 2, where the theory is that Windows creates a virtual adapter that isn’t properly bridged with the internet network adapter.

The article raises the following points:

  1. In WSL ensure that your server is using IPv4

  2. If you are using a firewall in WSL, allow the relevant port (3390)

  3. In Windows, forward your port from the public IP port to the WSL port in Command Prompt or Powershell with admin rights:

     netsh interface portproxy add v4tov4 listenport=$PORT listenaddress=0.0.0.0 connectport=$PORT connectaddress=127.0.0.1
    
  4. In Windows, allow the port through the Windows firewall using a new Inbound Rule.

  5. For access from the internet, setup port forwarding for the port in the router.

A YouTube video describing the process with some small differences is WSL 2 Networking.

Related Question