WINDOWS: Change Ubuntu hostname running on WSL

WINDOWS: Change Ubuntu hostname running on WSL

If you want to change the Ubuntu hostname running on WSL, you might have already tried updating the /etc/hostname file, only to realize that after a reboot, the host name changes back to the original hostname.

You also might have already figured that the easiest way to do this is to change the host name of the Windows host on which you are running WSL. That’s probably obvious and not something you are looking for.

So, if you are trying to change the hostname of the Ubuntu distribution running in WSL without changing the host name of the Windows host system, there are few steps to follow.

Step 1: Note down your ubuntu hostname

Make sure you note down your current ubuntu hostname, as we might need this going forward.

amajhi@ANUPAM-DESKTOP:~$ hostname
ANUPAM-DESKTOP

In this example ANUPAM-DESKTOP is the hostname which we will be replacing with the new hostname KATANA

Step 2: Update the wsl.conf file

Next you will need to update (or create if not present) the file /etc/wsl.conf

amajhi@ANUPAM-DESKTOP:~$ sudo vi /etc/wsl.conf
[sudo] password for amajhi:

Then add/update the following lines to the file.

[network]
hostname = KATANA
generateHosts = false

The “generateHosts = false” makes sure that he hostname and hosts files are not replaced back to original on reboot.

Step 3: Update the hosts file

This is an important step to prevent any host resolution issue. If this step is not done, then the system might get confused with what the hostname really is!

amajhi@KATANA:~$ sudo apt update
sudo: unable to resolve host KATANA: Name or service not known

Let’s look at the current hosts file and make it right.

amajhi@KATANA:~$ sudo vi /etc/hosts

# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateHosts = false
127.0.0.1 localhost
127.0.1.1 ANUPAM-DESKTOP.localdomain ANUPAM-DESKTOP

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

To do the appropriate changes we need to update the /etc/hosts file and replace all occurrences of the old hostname ANUPAM-DESKTOP with the new hostname KATANA.

# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateHosts = false
127.0.0.1 localhost
127.0.1.1 KATANA.localdomain KATANA

# The following lines are desirable for IPv6 capable hosts
::1 ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Step 4: Restart the WSL instance

To restart the WSL instance we need to shut down the WSL instance and relaunch ubuntu.

PS C:\Users\amajhi> wsl --shutdown

or

PS C:\Users\amajhi> wsl -t Ubuntu

Once you launch the ubuntu in WSL again, you should see the updated hostname intact and should be able to resolve host too. Let’s verify the resolution.

amajhi@KATANA:~$ sudo apt update
[sudo] password for amajhi:
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Hit:2 http://security.ubuntu.com/ubuntu jammy-security InRelease
Hit:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
64 packages can be upgraded. Run 'apt list --upgradable' to see them.

That’s it!