The hostname of a computer is its network identifier. In Ubuntu, it is visible in the terminal and is set during installation. Changing the hostname might be necessary to reflect a new role or adhere to a naming convention.

The hostname is stored in the /etc/hostname file. You can update it by editing this file or using the hostnamectl command. Using hostnamectl is the preferred method because it updates all necessary configurations.

After changing the hostname, confirm the update by checking the /etc/hostname file and running the hostname command. Rebooting the system ensures the new hostname is applied system-wide.

Steps to change system hostname in Ubuntu:

  1. Open the terminal.
  2. Check the current hostname using the hostname command.
    user@host:~$ hostname
    host
  3. Change the hostname using the hostnamectl command.
    user@host:~$ sudo hostnamectl set-hostname newhostname
    [sudo] password for user:
  4. Verify the new hostname in the /etc/hostname file.
    user@host:~$ cat /etc/hostname
    newhostname
  5. Confirm the hostname change by running the hostname command.
    user@host:~$ hostname
    newhostname
  6. Run a new shell to see the change in the shell prompt.
    user@host:~$ bash
    user@newhostname:~$

    This only works if $PS1 is set to display the hostname.

  7. Reboot the system to apply the change across the entire system (optional).
    user@newhostname:~$ sudo reboot

This guide is tested on Ubuntu:

Version Code Name
22.04 LTS Jammy Jellyfish
23.10 Mantic Minotaur
24.04 LTS Noble Numbat
Discuss the article:

Comment anonymously. Login not required.