34

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?

8
  • 3
    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 4, 2017 at 18:32
  • 1
    @ifconfig That worked, thanks!
    – Leo Jiang
    Jul 4, 2017 at 23:00
  • 1
    Sorry, where is this system menu?
    – Arya
    Jul 11, 2017 at 1:40
  • 13
    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: srccodes.com/… May 15, 2022 at 13:53
  • 5
    Andersen's comment should really be the acceptable answer!
    – Robert
    Jun 20, 2022 at 15:50

2 Answers 2

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.

7
  • 1
    I feel silly, for searching this up xD
    – NerdioN
    Sep 6, 2020 at 6: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 6, 2020 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 5, 2021 at 9: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, 2021 at 11:49
  • 8
    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: srccodes.com/… May 15, 2022 at 13:55
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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.