how to access windows localhost from wsl2?
Become or hire the top 3% of the developers on Toptal https://topt.al/25cXVn
--------------------------------------------------
Music by Eric Matyas
https://www.soundimage.org
Track title: Lost Jungle Looping
--
Chapters
00:00 Question
01:18 Accepted answer (Score 44)
02:58 Answer 2 (Score 4)
03:54 Answer 3 (Score 4)
04:56 Answer 4 (Score 1)
05:37 Thank you
--
Full question
https://superuser.com/questions/1679757/...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#networking #windowssubsystemforlinux #tcp #wsl2 #gdb
#avk47
ACCEPTED ANSWER
Score 52
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.
ANSWER 2
Score 6
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.
ANSWER 3
Score 5
"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.
ANSWER 4
Score 3
Other answers have solved this by using "$(hostname).local"
.
However, for my case at least (Ubuntu 23.10 on WSL 2), running the command ping "$(hostname).local"
errored:
ping: <myhostname.local: Name or service not known
I managed to fix this by installing libnss-mdns
, letting the ping
command run normally. (Run sudo apt install libnss-mdns
to install on Ubuntu)
Source for the fix: https://github.com/microsoft/WSL/issues/10077#issuecomment-1736181588