how to access windows localhost from wsl2?

I have windows 11 with wsl2(ubuntu) installed on it.

I've setup a gdb listener server on my windows localhost and want to access it from wsl2. but it seems my windows machine and wsl vm are using different network adapters.

>ipconfig

Windows IP Configuration


Ethernet adapter vEthernet (Default Switch):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::1114:bb:d0ad:93f8%18
   IPv4 Address. . . . . . . . . . . : 172.29.192.1
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . :

Ethernet adapter vEthernet (WSL):

   Connection-specific DNS Suffix  . :
   Link-local IPv6 Address . . . . . : fe80::897:849a:5fed:1c6e%52
   IPv4 Address. . . . . . . . . . . : 172.21.128.1
   Subnet Mask . . . . . . . . . . . : 255.255.240.0
   Default Gateway . . . . . . . . . :

my server is actively listening on 8888 port and is accessible on the windows but not on ubuntu.

I've opened the 8888 port both from the windows firewall and ubuntu firewall

what is the solution?


localhost doesn't work because WSL2 is running with a virtual network (vNIC) created by the Windows Virtual Machine Platform (a subset of Hyper-V). Inside WSL2, localhost is the address of the vNIC.

WSL2 does set up a virtual router on the Windows host to allow connectivity to both the outside world as well as the Windows host. You can see this via:

ip route

The "default via" address is the address to use for the Windows host.

You could parse it from the above, or from /etc/resolv.conf, but WSL also sets up a convenience mDNS, the .local domain, using the Windows "computer name", which is also used as the hostname of the WSL instance.

Accessing it is by concatenating $(hostname) with .local.

Try:

ping "$(hostname).local"

Or, if ICMP is blocked (seems so on new Windows 11 installs), use netcat. It's available by default in the WSL Ubuntu installation, but may need to be installed in other distributions like openSUSE:

nc -zv "$(hostname).local" <portnumber>

Reference: Access a localhost running in Windows from inside WSL2?