WSL2 use "localhost" to access Windows service
Asked Answered
O

4

15

I'm using WSL2 on Windows 10.

My dev stack is using a local webserver (localwp or wamp) on the host OS. I use WSL2 as the main terminal (SSH, Git, SASS, automation tools, ...).

What I need is a way to connect to my host services (MySql) from the WSL2 system using a server name instead of a random IP address.

It is already possible for the Windows host to connect to WSL2 services with "localhost". Is there a solution to do it the other way?

Otranto answered 8/1, 2021 at 8:39 Comment(0)
S
19

You should use hostname.local to access Windows from WSL2 because that will use the correct IP. Note that hostname should be replaced with the result of the hostname command run in WSL2. You can check the IP by running ping $(hostname).local from WSL2.

You also need to add a firewall rule to allow traffic from WSL2 to Windows. In an elevated PowerShell prompt run this:

New-NetFirewallRule -DisplayName "WSL" -Direction Inbound  -InterfaceAlias "vEthernet (WSL)"  -Action Allow

The command above should allow you to access anything exposed by Windows from WSL, no matter what port, however bear in mind that any apps you've launched get an automated rule created for them when you first launch them, blocking access from public networks (this is when you get a prompt from Windows Firewall, asking whether the app should be allowed to accept connections from public networks).

If you don't explicitly allow, they will be blocked by default, which also blocks connections from WSL. So you might need to find that inbound rule, and change it from block to allow (or just delete it).

See info here:

https://github.com/microsoft/WSL/issues/4585#issuecomment-610061194

Semiramis answered 8/10, 2021 at 15:48 Comment(0)
A
5

Well, your title and your question body don't seem quite aligned.

The question title says "use localhost", but then in the body you say "using a server name."

Accessing the Windows 10 service via the name "localhost" from WSL2? Let's just go with "no". I can think of a possibility of how to make it work, but it would be complicated.

But I think the second is really what you are looking for, so a couple of options that I can think of for accessing the Windows host services by hostname in WSL2:

  • First, and hopefully the easiest, WSL2 supports mDNS (WSL1 did not), so you should be able to access the Windows host as {hostname}.local (where {hostname} is the name of the Windows host (literally, in bash, ping $(hostname).local, since the assigned WSL2 hostname is that of the host Windows 10 computer). That works for me. While I don't recall having to do anything special to enable this, this Super User answer seems to indicate that you have to turn it on manually.

  • The second option would be to add your Windows host IP to /etc/hosts. If your Windows IP is static, then you could just add it manually to /etc/hosts and be done. If it's dynamic, then you might want to script it. You can retrieve it from inside WSL2 via:

    powershell.exe "(Test-Connection -ComputerName (hostname) -Count 1).IPV4Address.IPAddressToString" (and other methods) and then use something like sed to change /etc/hosts.

Apricot answered 8/1, 2021 at 18:0 Comment(0)
P
1

Add the following code to ~/.bashrc or ~/.zshrc, and then use winhost to access the host ip。

sed -i -e '/winhost/d' /etc/hosts

win_ip=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')

win_host="$win_ip winhost"

echo $win_host >> /etc/hosts
Pegpega answered 17/10, 2021 at 15:9 Comment(0)
B
1

The last time I was facing this issue, I downgraded to WSL1, and all the connections started working perfectly.

You can use:

wsl --set-version Ubuntu 1

This is the easiest approach to fix all connection related issues in WSL2.

Bowman answered 11/1, 2023 at 13:1 Comment(1)
this is still a life-saverAccoucheur

© 2022 - 2024 — McMap. All rights reserved.