33

sudo hostname XYZ does not seems to work in WSL.

I recently reinstalled Windows 10 and my wsl hostname is DESKTOP-XXXXXX. I'm trying to change it to something else. I tried the following:

sudo hostname wsl

I also changed /etc/hostname to "wsl" and changed /etc/hosts to "127.0.1.1 wsl.localdomain wsl".

Every time I restart WSL, it resets the hostname to the windows name. How do I change the wsl hostname permanently?

Community
  • 1
  • 1
Leo Jiang
  • 24,497
  • 49
  • 154
  • 284
  • 2
    Have you tried to change the host system's name from `System --> Change Settings --> Change`? I am not sure, but it would make sense for WSL to update its hostname to match that of the host computer. – ifconfig Jul 04 '17 at 18:32
  • @ifconfig That worked, thanks! – Leo Jiang Jul 04 '17 at 23:00
  • 1
    Sorry, where is this system menu? – Arya Jul 11 '17 at 01:40
  • 7
    Because this is the top result on Google: To change independently from windows. In WSL do: `sudo nano /etc/wsl.conf`, add the lines `hostname = your-new-hcleaost-name`, and `generateHosts = false` under `[network]`, then in CMD on windows do `wsl --shutdown` to make it take effect. Source: https://www.srccodes.com/change-hostname-ubuntu-microsoft-windows-subsystem-for-linux-wsl-wsl2-wsl-conf-unable-resolve-hosts-name-service-not-known-list-running-shutdown-vm-srccodes/ – André C. Andersen May 15 '22 at 13:53
  • 2
    Andersen's comment should really be the acceptable answer! – Robert Jun 20 '22 at 15:50
  • create `/etc/wsl.conf` file and add the lines `[network]` `hostname = ` and `generateHosts = false`. Finally restart wsl2 from powershell with `wsl --shutdown`. Restart wsl2 to see the changes reflect. – alex devassy Oct 04 '22 at 10:55
  • you can edit /etc/wsl.conf like alex but leave generatedHost=true and add your hostname to windows hosts file to add your hostname – imaginabit Nov 08 '22 at 23:15
  • Andre comment is correct and the solution in the link worked for me – Sherif O. Feb 04 '23 at 04:35

2 Answers2

26

Try to change the host system's name from System --> Change Settings --> Change. In some sense, it makes sense for WSL to update its hostname to match that of the host computer.

ifconfig
  • 6,242
  • 7
  • 41
  • 65
  • 1
    I feel silly, for searching this up xD – NerdioN Sep 06 '20 at 06:28
  • 6
    It's not a silly question to wonder about, @NerdioN. It also makes perfect sense to me why someone would automatically assume that WSL isn't that attached to the host Windows OS like that. – ifconfig Sep 06 '20 at 21:48
  • 7
    I wanted to use the hostname in WSL to help distinguish between multiple distributions installed with WSL. I think I'll have to customise my prompt instead. – Andrew Apr 05 '21 at 09:25
  • 3
    Someone would want to change the WSL distro's hostname to make it distinct from the host Windows machine. Wonder why would you suggest making changes in Windows to still reflect in the WSL distros. – KAR Jul 24 '21 at 11:49
  • 5
    I'd add it as an answer, but the question is locked. To change independently from windows. In WSL do: `sudo nano /etc/wsl.conf`, add the lines `hostname = your-new-hcleaost-name`, and `generateHosts = false` under `[network]`, then in CMD on windows do `wsl --shutdown` to make it take effect. Source: https://www.srccodes.com/change-hostname-ubuntu-microsoft-windows-subsystem-for-linux-wsl-wsl2-wsl-conf-unable-resolve-hosts-name-service-not-known-list-running-shutdown-vm-srccodes/ – André C. Andersen May 15 '22 at 13:55
  • 1
    @AndréC.Andersen That file doesn't exist on my WSL. Is it supposed to? – ifconfig May 15 '22 at 18:07
  • See official documentation: https://learn.microsoft.com/en-us/windows/wsl/wsl-config – Alireza Rezaee Jun 20 '22 at 07:48
24

In Windows 10 Home 64-bit, Fall Creators Update edition (Version 1709, Build 16299.19) I used:

SettingsSystemAboutRename this PC

After that a restart is required.

vronjec
  • 259
  • 1
  • 13
WinEunuuchs2Unix
  • 1,801
  • 1
  • 17
  • 34