The hostname of a computer identifies it within a local network. In Ubuntu, it appears when launching the terminal. Although the hostname is set during installation, you may need to change it to match new roles or naming conventions.

Ubuntu stores the hostname in the /etc/hostname file. You can change it by editing this file or using the hostnamectl utility, which simplifies the process.

You can verify the change by checking the /etc/hostname file and running the hostname command. Rebooting the system ensures the new hostname is recognized system-wide.

Steps to change system hostname in Ubuntu:

  1. Launch the terminal.
  2. Check the current hostname using the hostname command.
    user@host:~$ hostname
    host
  3. Set the new hostname using hostnamectl.
    user@host:~$ sudo hostnamectl set-hostname newhostname
    [sudo] password for user:
  4. Check the content of the /etc/hostname file.
    user@host:~$ cat /etc/hostname
    newhostname
  5. Run the hostname command again to confirm the hostname change.
    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 your system for the change to be applied system-wide (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.