The hostname is the unique label assigned to your Ubuntu server or desktop on a network. It is the name that appears in your terminal prompt (e.g., user@hostname:~$) and identifies your machine to other devices on the local network.
Whether you are setting up a fresh cloud instance or reorganising your home lab, changing the default autogenerated hostname to something descriptive (like webserver-01 or database-primary) is a crucial administration task. In this guide, we will show you how to change the hostname permanently using the command line.
Method 1: Using the hostnamectl Command (Recommended)
In modern versions of Ubuntu (systemd-based systems like 16.04 and newer), the hostnamectl command is the safest and most efficient way to change the hostname. It automatically updates the system kernel and the static hostname configuration file.
- Open your terminal or connect to your server via SSH.
- To view your current hostname configuration, run:
hostnamectl - To set a new hostname, use the `set-hostname` flag followed by your desired name. For example, to change the name to
ubuntu-server, type:sudo hostnamectl set-hostname ubuntu-server
This command does not produce any output if successful. However, the change takes effect immediately without requiring a full system reboot.
Step 2: Update the /etc/hosts File
Even though hostnamectl updates the main system name, you must also update your /etc/hosts file. This file maps IP addresses to hostnames locally. If you skip this step, certain local services (like Apache or sudo itself) may struggle to resolve the system’s own name, leading to delays and “unable to resolve host” errors.
- Open the hosts file in a text editor like nano:
sudo nano /etc/hosts - Look for the line that starts with
127.0.1.1(or sometimes127.0.0.1) followed by your old hostname. - Delete the old hostname and replace it with the new hostname you just created. For example:
127.0.1.1 ubuntu-server - Save the file and exit the editor. If you are using nano, press CTRL + O, hit Enter to save, and then press CTRL + X to exit.
Method 2: The Traditional Way (Editing Configuration Files)
If you are using an older version of Ubuntu that does not support hostnamectl, you can achieve the exact same result by manually editing the static configuration file.
- Open the `/etc/hostname` file in your text editor:
sudo nano /etc/hostname - The file contains only one line of text: your current hostname. Delete it, and type your new hostname.
- Save and exit the file (CTRL + O, Enter, CTRL + X).
- Just as in Method 1, you must also update the `/etc/hosts` file:
sudo nano /etc/hosts - Change the old name to the new name next to `127.0.1.1` and save the file.
- To apply the changes without restarting the machine, run the following command to instruct the kernel to read the new file:
sudo hostname -F /etc/hostname
Verifying the Change
To verify that the change was successful, simply type hostname into your terminal and press Enter. The system will output your new name.
Please note that your terminal prompt (the text before your cursor) will not update to show the new hostname until you close the current terminal session and open a new one, or log out and log back in via SSH.