How to change the hostname of Ubuntu running in Windows WSL

This article shows how to permanently change the hostname of Ubuntu running in the Windows subsystem for Linux.

How to change the hostname of Ubuntu running in Windows WSL
Photo by Alex Chumak / Unsplash

If you are using the Windows WSL (Windows Subsystem for Linux) to run Linux-based operating systems such as Ubuntu, you probably want to change the host name, since it is the same as the name of your Windows PC by default. You can do this easily with the following steps.

Preparations

You need to know your current hostname, which you can get using the hostname command. Your output should look something like this:

ck@my-old-hostname:~$ hostname
my-old-hostname

My current hostname is therefore my-old-hostname. Next you have to decide which hostname you want to use in the future. For this tutorial I am going to use my-new-hostname.

💡
Please replace my-old-hostname and my-new-hostname with the information that applies to you.

1. Edit /etc/wsl.conf

The first three steps will be taken inside of the Ubuntu distribution. The file may not exist yet, but don't worry, it will be created automatically if necessary.

sudo vim /etc/wsl.conf

2. Add the following lines to /etc/wsl.conf

[network]
hostname = my-new-hostname
generateHosts = false

The second line hostname = my-new-hostname will lead to an updated hostname in /etc/hostname. The line generateHosts = false prevents WSL from automatically generating the /etc/hosts file.

3. Update /etc/hosts

sudo vim /etc/hosts

The content should look quite similar to this one:

# 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       my-old-hostname.localdomain      my-old-hostname
192.168.0.138   host.docker.internal
192.168.0.138   gateway.docker.internal
127.0.0.1       kubernetes.docker.internal

# 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-allrouter

Next we have to replace every occurence of my-old-hostname with my-new-hostname. This can be done in vim using :%s/my-old-hostname/my-new-hostname/g in the command mode. Then save and exit using :wq or :x.

4. Shutdown Ubuntu

We now have everything set up inside the Ubuntu distribution and need to restart the distribution for it to take effect. To do this, we need to run some commands outside the distribution on the Windows PC. So start the Windows Terminal, Windows PowerShell or something different and enter the following commands.

First we list the currently running distributions:

PS C:\Users\ck> wsl --list --running
Windows Subsystem for Linux Distributions:
Ubuntu (Default)

Then we terminate the desired distribution:

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

or shutdown WSL completely:

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

Verify that the distribution is stopped:

PS C:\Users\ck> wsl --list --running
There are no running distributions.

5. Restart and verify

Launch Ubuntu again and check the hostname as already shown using the hostname command:

ck@my-new-hostname: hostname
my-new-hostname