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.
Method 1: The Modern and Permanent Way (Recommended)
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.
- Open your terminal (Ctrl+Alt+T).
- Execute the hostnamectl command by replacing
new-hostnamewith your desired name:
sudo hostnamectl set-hostname new-hostname- Verify the change by running:
hostnamectlMethod 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.
- Open the hosts file in a text editor:
sudo nano /etc/hosts- Locate the line that maps
127.0.1.1to your old hostname:
127.0.1.1 old-hostname
- Change the old hostname to your new hostname:
127.0.1.1 new-hostname
- Save and exit the editor (in Nano, press
Ctrl+O,Enter, thenCtrl+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.
- Run the temporary command:
sudo hostname new-hostnameVerifying 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:
hostnameIf the output displays your new hostname, your configuration is complete and fully functional.