I am running a local AEM server in my Windows machine. The server is running on localhost:4502. I am using Ubuntu distro running in WSL2 for my development. I want to access the localhost:4502 running in the Windows machine in my WSL2 Ubuntu.
Is there any way to do that or is it not possible ?
Accessing your Windows files in the WSL terminal Just use the cd command to navigate to your WSL root folder, then navigate into the mnt folder to access your C: drive. See the example below for more details. As you can see, I managed to access my Documents folder.
The default path to access Linux files from Windows using the Windows Subsystem for Linux has been changed from \\wsl\\ to \\wsl. localhost\\ to improve performance and reliability.
WSL translates the Linux system calls into windows ones so the Ubuntu network data flows through the exact same TCP/IP stack as the windows data. In short this means to access the Linux localhost you just access the windows one, they are the same. localhost:4567 or 127.0. 0.1:4567 will do what you want.
mDNS has been a feature of WSL2 for a while now. Concatenating your WSL2 hostname
(or the equivalent command/function in your programming/language environment) with ".local"
should get you access.
For example, from Bash, try:
ping "$(hostname).local"
For instance, if your hostname
is "MyComputer", then the mDNS should be MyComputer.local
.
If ICMP is blocked (as it seems to be on new Windows 11 installs), or if you want to test the connection to the actual port, then 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>
WSL2 is running with a virtual network (vNIC) that is created by the Windows Virtual Machine Platform (a subset of Hyper-V). Inside WSL2, localhost
is the address of the vNIC.
WSL2 also sets 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
This is the address you need to use for the Windows host.
You could, of course, parse it from the route (or, as in an earlier answer, from /etc/resolv.conf
), but WSL sets up a convenience mDNS (the .local
domain) using the Windows "computer name", which is also used as the hostname of the WSL instance.
So concatenating $(hostname)
(or it's equivalent in your programming/language environment) with ".local"
should get you access.
mDNS is reliant on the Windows host to resolve the name. If you have changed your /etc/resolv.conf
under WSL, then this will likely not work.
Remember to open any necessary firewall ports. WSL2 is considered a separate network from that of the Windows host. Windows will consider network connections from WSL2 to be coming from an external source. (Credit to @RamilGilfanov for a comment pointing this out)
The first time a connection is made from WSL2 to a particular port, Windows Defender (if that is your firewall) will typically display a dialog asking if you want to grant access. However, in my experience, this dialog often gets buried under the main window due to timing of mouse-clicks, keyboard, etc., so it's easy to miss.
Remember to have your Windows service accept connections from remote hosts.
Many servers are configured by default to bind to localhost
/127.0.0.1
. Because WSL2 appears to Windows as a remote network, you'll typically need to update your configuration to bind to 0.0.0.0
or a specific address.
Note that, since the address for WSL2 changes after each reboot, it can be difficult to update your configuration each time. If at all possible, use 0.0.0.0
unless there are security concerns. Since WSL is designed for development rather than production, this shouldn't be an issue.
I was also looking for some solution to do this but currently, there is no such option available. Check out this GitHub issue:
https://github.com/microsoft/WSL/issues/4619
One solution can be this:
If you have the IP of windows(host) then it will do the job but the only problem is that IP will change every time. But, WSL2 stores your windows(host) IP in /etc/resolv.conf
file. So we can modify our etc/hosts
to map winhost
to the IP dynamically.
Add the following lines at the end of ~/.bashrc
file. This will grep the IP and modify the etc/hosts
when you boot the WSL.
export winhost=$(cat /etc/resolv.conf | grep nameserver | awk '{ print $2 }')
if [ ! -n "$(grep -P "[[:space:]]winhost" /etc/hosts)" ]; then
printf "%s\t%s\n" "$winhost" "winhost" | sudo tee -a "/etc/hosts"
fi
then run the following command to reload the changes.
$ source ~/.bashrc
now you can use winhost
instead of localhost
in WSL2(client) to access the servers running windows(host). In your case, it will be winhost:4502
but this will work for any other use cases as well like accessing Postgres/MySQL server running on windows, etc.
NOTE: Always remember to configure your firewall on windows to allow those ports so that WSL2 can access otherwise your connection may get blocked by firewall.
You need add ipv6 rule in hosts file.
Like:
127.0.0.1 example.com
::1 example.com localhost
I had a similar problem and found the solution at this link: https://github.com/microsoft/WSL/issues/5728
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With