How to Change Ubuntu Hostname

This article provides a quick, step-by-step guide on how to change the hostname of an Ubuntu machine using both temporary and permanent methods. You will learn how to utilize the hostnamectl command, update your system configuration files, and verify that your new hostname is active across your network.

Understanding the Hostname in Ubuntu

A hostname is a unique label assigned to a device on a network. In Ubuntu, it helps identify your machine to other devices and services. When you change it, you want to ensure the change persists even after a system reboot.

The most efficient way to change your hostname in modern Ubuntu releases is by using the hostnamectl utility. This tool updates the system configuration immediately without requiring a reboot.

  1. Open your terminal (Ctrl+Alt+T).
  2. Execute the hostnamectl command by replacing new-hostname with your desired name:
sudo hostnamectl set-hostname new-hostname
  1. Verify the change by running:
hostnamectl

Method 2: Updating the Hosts File

To prevent networking errors and ensure that local services can still resolve the machine’s address, you must update the static table lookup for hostnames.

  1. Open the hosts file in a text editor:
sudo nano /etc/hosts
  1. Locate the line that maps 127.0.1.1 to your old hostname:
127.0.1.1 old-hostname
  1. Change the old hostname to your new hostname:
127.0.1.1 new-hostname
  1. Save and exit the editor (in Nano, press Ctrl+O, Enter, then Ctrl+X).

Method 3: Changing the Hostname Temporarily

If you only need to change the hostname for your current session (it will revert back to the original name after a reboot), you can use the standard hostname command.

sudo hostname new-hostname

Verifying the Changes

To confirm that your Ubuntu machine has successfully adopted the new identity, restart your terminal deployment or run the following command to check the network status:

hostname

If the output displays your new hostname, your configuration is complete and fully functional.