How to resolve hostname in a WSL instance with internal IP

From a WSL2 distribution it would be great to be able to contact the host computer with IP address of the virtual Network and not with some other network address.

Edit: actually I missed that I can add to host name to correctly identify the ip of the host computer, like suggested here

In a WSL2 instance you are running inside a Virtual Environment managed by your Windows Operating system, and the greatest advantage is the ability to work with a unified filesystem and being able to have a great communication between the two system. This is perfect but I have a small problem, if I want to use the WSL2 instance to test some software I’m developing I’d like to access services running on my host operating system.

Lets discuss a typical scenario, I want to access ElasticSearch server running on my host from my WSL2 instance.

The problem is that my WSL2 instance have an autogenerated /etc/hosts file where the name of the host is set to localhost While I can agree that, from a perspective of the WSL instance it is part of the same machine, but the problem is that, if I try to connect to http://machinename:9200 I got no response because ElasticSearch runs on the host. My host windows machine is called shismatrix, and I’m expecting my WSL2 instance to use that name for network communication.

Name of the host was resolved with local IP Figure 1: Name of the host was resolved with local IP

As you can see from Figure 1 when I use shismatrix name, it will refer to loopback. This is the default of WSL, but I do not like this approach, I’d like the name of my host to resolve to the internal IP of my host and not to loopback interface. Thanks to the WSL2 architecture, I can know the real IP of my host inspecting the /etc/resolv.conf file, because WSL2 infrastructure use my host machine as DNS. This allow for a simple two line shell script to change my /etc/hosts file.

sudo sed -i '/.localdomain/d' /etc/hosts
tail -1 /etc/resolv.conf | awk '{print $2} ' | tr '\n' ' ' | sudo tee -a /etc/hosts
cat /etc/hostname | sudo tee -a /etc/hosts

I know that this will break the convention where the name of the host is used inside WSL2 instance, but I really prefer to have name of my host to really resolve host, not WSL instance. Thanks to that two line script I can now use name of the host to really refer to my host machine

Now name of my host refers correctly to the host machine virtual IP for WSL instance Figure 2: Now name of my host refers correctly to the host machine virtual IP for WSL instance

From Figure 2 you can verify that now the name shismatrix refers to address, that is actually the address of my host machine. This address is not static and can change between reboot, but thanks to the above script I can change /etc/hosts file to fix this situation.

I know that I can still refer to my hostname with standard names of my local network (I have an internal DNS), but I really prefer to use internal IP to be able to configure the firewall to allow access to certain port only from IP range of WSL2 machines. As an example I do not want my ElasticSearch instance to be accessible to every machine in the network, so I can scope a firewall rule for port 9200 to accept only connection from

The ability to use internal IP for WSL2 instance allows me to setup firewall rules to open certain services only to my WSL instances.

Gian Maria.