31

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?

1
  • The accepted answer is ubiquitous on the internet and probably works in most cases. However, it did not for me. I found a workaround for my own problem of accessing a server on the windows host from WSL2: I setup the server running on WSL2 instead, mounting the relevant host directories into the WSL, and became happy bidirectionally. Note, that it is no problem to access the WSL2 from the Windows host. Only WSL2 to host is difficult. This is no general answer but a workaround for some of those whose problem is not solved by the accepted answer. Aug 11, 2022 at 11:57

3 Answers 3

38

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? answer by NotTheDr01ds.

8
  • 12
    That looks suspiciously like my Stack Overflow answer of a few days ago :-). Please remember to add a reference/attribution when copying/pasting from another answer. Oct 4, 2021 at 18:20
  • @NotTheDr01ds: Sorry, I forgot to add the reference. Feel free to add here your answer; let me know and I'll delete mine.
    – harrymc
    Oct 4, 2021 at 18:26
  • No worries - I just try to link people back to the original source so that they can upvote there as well. I don't ask for upvotes for the original source, but I hope folks will do so if they find it useful. Oct 4, 2021 at 18:27
  • Also, funny thing - You accidentally linked back to this answer (yours, here) instead of the SO one. I'll edit and fix. Oct 4, 2021 at 18:28
  • 1
    But yeah, as an aside, I don't usually ask for upvotes, but if you found my SO answer useful enough to copy/paste from, you could have at least thrown an upvote on it while you were at it! :-) Oct 4, 2021 at 18:29
3

"Localhost" always refers to system itself. When you're running a host OS and a virtualized OS, you're running 2 systems, both of which refer to themselves as "localhost". So you can never access "localhost" of another system, you need to use its external IP.

In your case if you want to access from the Ubuntu system a service that's running on your host Windows listening to port 8888, you have to access 172.29.192.1:8888. Only the system that's actually offering the service must listen to the port and allow new incoming connections through that port.

Because the Ubuntu is a separate system, you won't have anything there listening to port 8888 unless you install a service that listens to that port.

1
  • 2
    But won't that 172.29.192.1 address change every time you restart the computer? So if you bookmark a page under that 172.29.192.1:8888 address the bookmark will be broken each time the IP changes. How do you get around that?
    – luisdev
    Feb 16, 2022 at 6:03
3

Further elaborating on the answer from @harrymc and @NotTheDr01ds:

Generally, you can contact Windows services from wsl2 command line using "$(hostname).local" as the host name. If such connections seem to be blocked, you can try the following:

  • Check whether Windows Firewall blocks the traffic. One easy check would be to turn it off for a few seconds and repeat the test. If that works, add appropriate Firewall rules.

  • Issue nslookup "$(hostname).local". This will give you a bunch of IPs. By default, the first one will be used, but that might not be the one which works. Try the other ones as well. If you find one that works, you can tailor a short script for your own system which will always return the correct IP.

2
  • "$(hostname).local" cannot be resolved. nslookup etc. does not give me any IP for this. (WSL2 on Win11)
    – Tino
    Jun 25 at 18:28
  • @Tino hostname is a standard Linux command, and $(...) is a standard way to include the result of an external command into a command line. You either use a shell which needs a different syntax, or the hostname command is not installed. Both should be easy to fix.
    – malamut
    Jul 2 at 8:50

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .