Can't change hostname in WSL #15940

Closed
opened 2026-01-31 04:52:53 +00:00 by claunia · 3 comments
Owner

Originally created by @baseely on GitHub (Nov 20, 2021).

Windows Terminal version

1.11.2921.0

Windows build number

10.0.19043.0

Other Software

No response

Steps to reproduce

I'm using Ubuntu 20.04 in WSL:
cat /etc/os-release
NAME="Ubuntu"
VERSION="20.04.1 LTS (Focal Fossa)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 20.04.1 LTS"
VERSION_ID="20.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=focal
UBUNTU_CODENAME=focal

Try to change the hostname using the below command:
hostnamectl set-hostname <test-host-name>

Expected Behavior

Change the hostname to the new one test-host-name

Actual Behavior

It gives this error message instead:
System has not been booted with systemd as init system (PID 1). Can't operate. Failed to create bus connection: Host is down

Originally created by @baseely on GitHub (Nov 20, 2021). ### Windows Terminal version 1.11.2921.0 ### Windows build number 10.0.19043.0 ### Other Software _No response_ ### Steps to reproduce I'm using Ubuntu 20.04 in WSL: `cat /etc/os-release` NAME="Ubuntu" VERSION="20.04.1 LTS (Focal Fossa)" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 20.04.1 LTS" VERSION_ID="20.04" HOME_URL="https://www.ubuntu.com/" SUPPORT_URL="https://help.ubuntu.com/" BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/" PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy" VERSION_CODENAME=focal UBUNTU_CODENAME=focal Try to change the hostname using the below command: `hostnamectl set-hostname <test-host-name>` ### Expected Behavior Change the hostname to the new one `test-host-name` ### Actual Behavior It gives this error message instead: `System has not been booted with systemd as init system (PID 1). Can't operate. Failed to create bus connection: Host is down`
claunia added the Product-WSLNeeds-TriageNeeds-Tag-Fix labels 2026-01-31 04:52:53 +00:00
Author
Owner

@elsaco commented on GitHub (Nov 21, 2021):

@baseely that's a feature, not a bug. WSL is not using system daemon (systemd) and that's by design. To change the hostname on a WSL instance you have to add /etc/wsl.conf if not present already. If you cat /etc/hosts you'll find this at the top:

# 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

Here's sample wsl.conf I use:

[network]
hostname = ubuntu
generateHosts = false

There are more options to add to /etc/wsl.conf if you'd like to customize your instances.

@elsaco commented on GitHub (Nov 21, 2021): @baseely that's a feature, not a bug. WSL is not using system daemon (systemd) and that's by design. To change the hostname on a WSL instance you have to add `/etc/wsl.conf` if not present already. If you `cat /etc/hosts` you'll find this at the top: ``` # 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 ``` Here's sample wsl.conf I use: ``` [network] hostname = ubuntu generateHosts = false ``` There are more options to add to `/etc/wsl.conf` if you'd like to customize your instances.
Author
Owner

@zadjii-msft commented on GitHub (Nov 22, 2021):

If anything, this discussion belongs over at https://github.com/microsoft/WSL, I'm not sure we on the Terminal team have anything to add here. Thanks!

@zadjii-msft commented on GitHub (Nov 22, 2021): If anything, this discussion belongs over at https://github.com/microsoft/WSL, I'm not sure we on the Terminal team have anything to add here. Thanks!
Author
Owner

@datmt commented on GitHub (Sep 27, 2022):

It's quite tricky since you need to change at several places.
First, set this in /etc/wsl.conf

[network]
hostname = wsl2
generateHosts = false

Then, make sure you have replace the old entries with your new hostname entry in /etc/hosts

127.0.1.1      your_new_hostname.localdomain        your_new_hostname
``

no need to change in `/etc/hostname`

Open powershell, shutdown wsl and you are done:

wsl --shutdown


I did a tutorial here: https://datmt.com/linux/change-ubuntu-wsl2-hostname-permanently/
@datmt commented on GitHub (Sep 27, 2022): It's quite tricky since you need to change at several places. First, set this in `/etc/wsl.conf` ``` [network] hostname = wsl2 generateHosts = false ``` Then, make sure you have replace the old entries with your new hostname entry in `/etc/hosts` ``` 127.0.1.1 your_new_hostname.localdomain your_new_hostname `` no need to change in `/etc/hostname` Open powershell, shutdown wsl and you are done: ``` wsl --shutdown ``` I did a tutorial here: https://datmt.com/linux/change-ubuntu-wsl2-hostname-permanently/
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#15940