How to Change the Hostname on Ubuntu 14.04, 16.04, 18.04, 20.04, and 22.04

The hostname on your Ubuntu system serves as a vital identifier, facilitating the recognition and communication of your server within your network. Whether you’re establishing a new server or modifying an existing hostname, Ubuntu offers a straightforward process. It’s worth noting that the approach for changing the hostname has adapted with each Ubuntu release, so we’ll delve into the procedure for different versions: Ubuntu 14.04 (Trusty Tahr), Ubuntu 16.04 (Xenial Xerus), Ubuntu 18.04 (Bionic Beaver), Ubuntu 20.04 (Focal Fossa), and Ubuntu 22.04 (Jammy Jellyfish).

Ubuntu 14.04 (Trusty Tahr)

Ubuntu 14.04 uses the Upstart init system and follows a more traditional method to change the hostname.

  1. Log in as Root or a Superuser: You’ll need root or superuser privileges to make these changes.
  2. Edit the Hosts File: Open the /etc/hosts file in your preferred text editor, such as nano:
   sudo nano /etc/hosts
  1. Locate and Modify the Hostname Entry: In the file, you’ll find an entry that associates the hostname with the loopback address (127.0.0.1). Change the hostname in this entry to your desired hostname.
  2. Edit the Hostname File: Open the /etc/hostname file with your text editor:
   sudo nano /etc/hostname
  1. Replace the Existing Hostname: Replace the current hostname with your desired hostname and save the file.
  2. Reboot Your System: To apply the changes, you’ll need to reboot your Ubuntu 14.04 system:
   sudo reboot

Ubuntu 16.04 (Xenial Xerus)

Ubuntu 16.04 and later versions use the systemd init system, simplifying the process of changing the hostname.

  1. Log in as Root or a Superuser: Gain root access to your Ubuntu 16.04 system.
  2. Change the Hostname: Use the hostnamectl command to change the hostname:
   sudo hostnamectl set-hostname newhostname

Replace newhostname with your desired hostname.

  1. Reboot Your System: To apply the changes, reboot your Ubuntu 16.04 system:
   sudo reboot

Ubuntu 18.04 (Bionic Beaver), Ubuntu 20.04 (Focal Fossa), and Ubuntu 22.04 (Jammy Jellyfish)

Changing the hostname in Ubuntu 18.04, 20.04, and 22.04 is similar because these versions all use systemd.

  1. Log in as Root or a Superuser: Log in with superuser privileges on your Ubuntu 18.04, 20.04, or 22.04 system.
  2. Change the Hostname: Use the hostnamectl command to set the new hostname:
   sudo hostnamectl set-hostname newhostname

Replace newhostname with your desired hostname.

  1. Edit the Hosts File: It’s a good practice to edit the /etc/hosts file to ensure that it matches the new hostname. Open the file with your preferred text editor:
   sudo nano /etc/hosts
  1. Locate and Modify the Hostname Entry: Find the line that associates the old hostname with the loopback address (127.0.0.1) and replace it with the new hostname. Save the file.
  2. Reboot Your System: To apply the changes, reboot your Ubuntu 18.04, 20.04, or 22.04 system:
   sudo reboot

In conclusion, changing the hostname on Ubuntu 14.04, 16.04, 18.04, 20.04, and 22.04 is a straightforward process, although the method may vary slightly between versions. Whether you’re using the Upstart or systemd init system, you can easily modify your Ubuntu system’s hostname to suit your specific needs.