WSL 2 - localhost problem

WSL 2 - localhost problem

"WSL 2 come with a new architecture, supporting faster linux file access and implementing all core features. However, the new architecture also create a few new problems, especially with networking."


WSL 2 now run on its own light weight virtual machine instead of being part of Windows System.

This create a problem where the VM now has its own network interface and its own IP Address.

From what I read, WSL 2 suppose to have a feature called Localhost Forwarding that will allow Windows to automatically forward localhost request to WSL 2 VM.

In many case, this forwarded request will have a different destination or host than localhost (most likely to be WSL 2's IP Address). As such, you will need to configure your server to bind to WSL 2's IP Address or to 0.0.0.0 (accept any IP) instead of localhost.

Unfortunately, localhost forwarding doesn't seem to work on my machine so I have to type out my WSL 2's IP address inside my browser instead of localhost but otherwise, this is an okay workaround for now.

You can find your WSL 2's IP Address with (Ubuntu-20.04 LTS):

ip addr list | grep inet

Apparently, ifconfig has been deprecated in favor of the ip (iproute2) command in newer distros.

A few examples

For gatsby, you need to pass -H 0.0.0.0 as an argument to your gatsby develop command.

yarn run dev -H 0.0.0.0

For your own node server, make sure the listen() method bind to 0.0.0.0 .

server.listen(3000, '0.0.0.0')

Accessing from other machines

It seems like accessing WSL 2 servers from other devices on the same network also will not work without configuring Windows to forward such request to WSL 2 manually.

I think this is because the connection between WSL 2 and Windows is being done over a virtual network interface, and thus only exist and accessible within the Windows host itself. For external devices to connect, they need to send their requests to Windows' 'real' IP address on that network and have Windows forward those requests through the virtual interface to WSL 2.

If you run ipconfig in PowerShell, you will likely see a few virtual Ethernet adapter (vEthernet) will IP on different subnet.

I haven't had any needs to do this yet but will add how to configure this when I get to it.


Written by Romson Preechawit

Hi there! I'm a typical geek, designer, developer. Love coding, design, photography, reading, and a lot more. English is not my first language so please help correct my grammar! :)

Romson Preechawit