DEV Community

Adrien
Adrien

Posted on

Fixed IP for WSL instances

Bored of having to check for your WSL instance IP everytime you reboot your computer? I found the solution!

The issue

I recently published an article on how to find the IP of a WSL instance. At the time, something I did not know and just figured out is that the IP is dynamic. Meaning it changes everytime you restart your computer. Maybe even when you stop/start your instance, too lazy to check. Let me know.

This messes with your configuration. For example, I have a GUI app to play with my DB (for the curious, I use DBeaver). I configured the DB connections like in the image:

Untitled

Now, it works on setting it up. But then, you have to change this every time you reboot your computer.

The solution

While looking for a way to assigned fixed IP addresses to WSL instances, I figured that there is currently no way to do this.

After more readings, I found out that setting the IP address in the hosts file would make things easier. You configure your connections with a custom domain, say wsl.local, and now, you have only one place where to update the IP address and all your apps are good to go.

But that's still tedious, and someone worked on that problem. Here comes WSL2Host.

WSL2Host

WSL2Host installs itself as a service. Every time a WSL instance launches, it updates the hosts file with the IP of the instance and assign it a domain based on the name of the distribution the WSL instance is running. For example, "Ubuntu-18.04" becomes ubuntu1804.wsl.

And that's all! It's a great piece of software! Read the installation instructions and you'll be good2go.

Troubleshooting

When I set it up, I had an issue where the service was registered but was not running at startup. I could run the service manually tho but that's not the purpose of it.

After watching at the system logs, I found an explicit error message (🙏) that stated my current user I was logged in with did not have the rights to run such a service. To fix that, look for the service in Services, it's called WSL2Host.

Untitled 1

Access to its properties via right click, go to the Connexion tab, and make sure the service log on as Local System account.

Untitled 2

You should be good2go from there.

Enjoy!

Top comments (1)

Collapse
 
eduardoluizgs profile image
Eduardo Luiz

Using the local service account in my scenario generated the following error: failed to get infos: wsl list all failed: wsl -l -v failed: exit status 1

I noticed that the application can also work with the user account provided at installation.

In my case, the error happens because the user account informed when installing the app does not have the right to logon as a service.

Accessing the WSL2Host service configuration in services.msc panel and configuring the username and password again in the Logon tab, windows adds the right to logon as a service to the user account and the app is able to run normally.