TransWikia.com

Network problems using WSL2

Super User Asked by Lars Rødal on November 7, 2021

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.

3 Answers

I needed three things for this to work:

  1. Upgrade to the latest version of windows
  2. wsl --shutdown
  3. Settings > Network & Internet > Network Reset as described here

Answered by Michael Cole on November 7, 2021

I ran into this as well; this is my solution: WSL2-PortForward.cmd

It takes care of the port proxy and firewall rules for you and cleans up after itself.

@ECHO OFF
SET LXDISTRO=MyWSL2vm & SET WSL2PORT=3399 & SET HOSTPORT=3398
NETSH INTERFACE PORTPROXY RESET & NETSH AdvFirewall Firewall delete rule name="%LXDISTRO% Port Forward" > NUL
WSL -d %LXDISTRO% -- ip addr show eth0 ^| grep -oP '(?^<=inets)d+(.d+){3}' > IP.TMP
SET /p IP=<IP.TMP
NETSH INTERFACE PORTPROXY ADD v4tov4 listenport=%HOSTPORT% listenaddress=0.0.0.0 connectport=%WSL2PORT% connectaddress=%IP% 
NETSH AdvFirewall Firewall add rule name="%LXDISTRO% Port Forward" dir=in action=allow protocol=TCP localport=%HOSTPORT% > NUL
ECHO WSL2 Virtual Machine %IP%:%WSL2PORT%now accepting traffic on %COMPUTERNAME%:%HOSTPORT%

Run the command:

.WSL2-PortForward.cmd

Result:

WSL2 Virtual Machine 172.17.109.95:3399 now accepting traffic on MYCOMPUTER:3398

Answered by Daniel Milisic on November 7, 2021

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.

Answered by harrymc on November 7, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP