Change ubuntu WSL2 Hostname permanently

It’s quite simple to change hostname in WSL2.

That means you can easily change the wsl2 part in this screenshot:

WSL2 hostname

Most of the time changing the name in /etc/hostname is enough. However, to change wsl2 hostname permanently, you will need to do some more work.

Step 1: Disable network auto generating config

First open/create a new file called wsl.conf under /etc/

Shell

Then put the following config:

sTeX

Change the hostname to whatever you like. In this case, I use wsl2 as my hostname.

Save the file. You’re halfway there.

Change the hostname in /etc/hosts and /etc/hostname

Let’s open /etc/hosts

You will see something similar to this:

LaTeX

The line you need to pay attention to is 5. Change OLD_HOST_NAME to your desired name. In my case, that would be wsl2:

LaTeX

Save the file.

Next open /etc/hostname and update the old host name to your desired hostname.

Shutdown wsl

Now, close your ubuntu windows and open powershell:

PowerShell

This will shutdown wsl.

Now, you can open ubuntu and see the new hostname:

Ubuntu WSL2 Hostname Changed
Ubuntu WSL2 Hostname Changed

Conclusion

In this post, I’ve shown you how to change hostname permanently in ubuntu wsl2 on Windows.

1 thought on “Change ubuntu WSL2 Hostname permanently”

Leave a Comment