If you are running Ubuntu on Windows WSL (Windows Subsystem for Linux) and wondering how to change hostname permanently then follow all steps mentioned below.
Once hostname is changed, if you get "unable to resolve host <hostname>: Name or service not known" then follow the step #4 for the fix.
1. Note down your
[email protected]:~$ hostname DESKTOP-ABC222
DESKTOP-ABC222 is hostname of Ubuntu running in my Windows 10 (OS Build 19043.1083) WSL.
/etc/wsl.conf or create the same if it does not exist.
[email protected]:~$ sudo nano /etc/wsl.conf [sudo] password for abhijit:
3. Add following lines in
[network] hostname = SrcCodes generateHosts = false
hostname = SrcCodes will update the hostname in
generateHosts = false will prevent WSL from automatic generation of
/etc/hosts file. Otherwise,
hosts file change will be overwritten during re-launch of Ubuntu and we'll get "unable to resolve host <hostname>: Name or service not known"
[email protected]:~$ sudo apt update sudo: unable to resolve host SrcCodes: Name or service not known
|generateHosts||boolean||true||true sets WSL to generate /etc/hosts. The hosts file contains a static map of hostnames corresponding IP address.|
/etc/hosts and update
[email protected]:~$ sudo nano /etc/hosts
You will see some entries similar to below
# 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 DESKTOP-ABC222.localdomain DESKTOP-ABC222 192.168.2.14 host.docker.internal 192.168.2.14 gateway.docker.internal 127.0.0.1 kubernetes.docker.internal 188.8.131.52 ip-44-99-0-122.lazerpenguin.com # 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
Find all occurences of hostname (e.g. "DESKTOP-ABC222") and replace with new one (e.g. "SrcCodes").
127.0.1.1 SrcCodes.localdomain SrcCodes
Save and exit from the nano editor.
5. Close Ubuntu and re-launch.
Change will not reflect immediately. You must wait ~8 seconds as mentioned below in the document.
If you launch a distribution (ie. Ubuntu), modify the wsl.conf file, close the distribution, and then re-launch it. You might assume that your changes to the wsl.conf file have immediately gone into effect. This is not currently the case as the subsystem could still be running. You must wait ~8 seconds for the subsystem to stop before relaunching in order to give enough time for your changes to be picked up. You can check to see whether your Linux distribution (shell) is still running after closing it by using PowerShell with the command: wsl --list --running. If no distributions are running, you will receive the response: "There are no running distributions." You can now restart the distribution to see your wsl.conf updates applied.
If you don't want to wait, then open Windows Terminal or Windows PowerShell run
wsl --shutdown to shut down the WSL 2 VM or terminate a specific distribution (e.g. "Ubuntu") using
wsl -t Ubuntu and relaunch it.
Check what all are running.
PS C:\Users\Abhijit\Desktop> wsl --list --running Windows Subsystem for Linux Distributions: Ubuntu (Default)
Then run shutdown or terminate command.
PS C:\Users\Abhijit\Desktop> wsl --shutdown
PS C:\Users\Abhijit\Desktop> wsl -t Ubuntu
Verify it is stopped.
PS C:\Users\Abhijit\Desktop> wsl --list --running There are no running distributions. PS C:\Users\Abhijit\Desktop>
6. Finally re-launch and verify.
Launch Ubuntu and check hostname.
[email protected]:~$ hostname SrcCodes
Verify "hosts" entry is also working correctly. You will not see following message anymore "unable to resolve host <hostname>: Name or service not known"
[email protected]:~$ sudo apt update [sudo] password for abhijit: Get:1 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB] Hit:2 http://archive.ubuntu.com/ubuntu focal InRelease Hit:3 http://archive.ubuntu.com/ubuntu focal-updates InRelease Hit:4 http://archive.ubuntu.com/ubuntu focal-backports InRelease Fetched 114 kB in 1s (135 kB/s) Reading package lists... Done Building dependency tree Reading state information... Done All packages are up to date. [email protected]:~$