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.
Video Tutorial:
1. Note down your hostname
Here, DESKTOP-ABC222
is hostname of Ubuntu running in my Windows 10 (OS Build 19043.1083) WSL.
2. Open /etc/wsl.conf
or create the same if it does not exist.
3. Add following lines in /etc/wsl.conf
.
hostname = SrcCodes
will update the hostname in /etc/hostname
.
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"
key | value | default | notes |
---|---|---|---|
generateHosts | boolean | true | true sets WSL to generate /etc/hosts. The hosts file contains a static map of hostnames corresponding IP address. |
Reference: https://docs.microsoft.com/en-us/windows/wsl/wsl-config#network
4. Open /etc/hosts
and update
You will see some entries similar to below
Find all occurences of hostname (e.g. "DESKTOP-ABC222") and replace with new one (e.g. "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.
Then run shutdown or terminate command.
or
Verify it is stopped.
6. Finally re-launch and verify.
Launch Ubuntu and check hostname.
Verify "hosts" entry is also working correctly. You will not see following message anymore "unable to resolve host <hostname>: Name or service not known"
Comments