Changing the hostname on WSL [closed]

Posted on Jun 4, 2023

Question

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?

Answer

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.