The network is one of the most significant components of the modern information technology infrastructure. When your computer is connected to a network, other devices can communicate with your computer using the computer name, which is also known as the hostname. If your computer does not have a unique hostname in the network, then you must change it.
In this tutorial, we will learn how to change the hostname in the Ubuntu Linux system.
- A computer running Ubuntu Linux distribution.
- A user account with either the root user or the sudo privileges.
- Use a unique system hostname for each computer in the same network. The same hostname for multiple computers can cause problems.
What is a System Hostname
A hostname is a unique name or label of your machine that is used for device identification and network communication. It is assigned automatically when you install your Ubuntu operating system and dynamically assigned to a VM. It is a name by which other machines identify your device. Do not use the same hostname for more than one computer in the same network.
When you work on a command prompt, the hostname reminds you which computer you are working on. You do not have to remember the machine IP address, which may change due to updates in network configuration. You can change the hostname to assign a descriptive name to your device, such as ubuntu-desktop.
The hostnames are classified in the following three categories:
- Static hostname is defined by the user and stored in the /etc/hostname file.
- Pretty hostname is a user-friendly descriptive name that provides more details about the system type and configuration.
- Transient hostname is maintained by the Linux kernel. This hostname can be changed at run time by DHCP or nDNS servers. The default transient hostname is the same as the static hostname.
You should always use a fully-qualified domain name, for example, hostname.example.com.
Display Current Hostname on Ubuntu System
Check the current hostname of your device before you decide to change it. Ubuntu provides
hostnamectl commands to check the current hostname.
To display only the hostname of your device, use the hostname command:
To display hostname along with other important information, such as VM machine ID, computer laptop chassis, Kernel version, and OS architecture, type:
You can temporarily change the hostname using the following command:
sudo hostname ubuntu-desktop
Change Hostname on Ubuntu (Without Reboot)
To change the hostname permanently on your Ubuntu computer without a system reboot, use the hostnamectl command. Follow these steps to change the hostname on ubuntu:
1. Change the hostname using the set-hostname option as follows:
hostnamectl set-hostname ubuntu-linux
When using hostnamectl set-hostname command you don't need to open and edit /etc/hostname file.
2. Check the new hostname using the hostnamectl command because the hostnamectl does not display any output like the hostname command:
If you are using a systemd based Ubuntu distribution, such as Ubuntu Linux 16.04 or later, then you can just use the hostnamectl command for changing the hostname.
The basic syntax of the hostnamectl command is as follows:
hostnamectl set-hostname new-hostname
Where new-hostname is the new name that you want to assign to your computer.
To change the hostname from ubuntu-linux to ubuntu-laptop, type:
sudo hostnamectl set-hostname ubuntu-laptop
Change Hostname on Ubuntu (Reboot Required)
You can also change the hostname permanently, by editing the /etc/hostname and /etc/hosts configuration files. However, this procedure requires that you reboot your Ubuntu computer. Follow these steps to change your hostname on Ubuntu by editing the /etc/hostname and /etc/hosts configuration files:
1. Edit the /etc/hostname file using a text editor of your choice:
sudo nano /etc/hostname
2. Replace the old hostname with the new hostname.
3. Edit the /etc/hosts file using an editor of your choice:
sudo nano /etc/hosts
4. Replace the old hostname with the new hostname.
5. Reboot your Ubuntu computer:
6. Check if the hostname is updated after the reboot:
Note: If your Ubuntu operating system is installed on a cloud instance and the cloud-init tool is installed, then you also need to edit the /etc/cloud/cloud.cfg configuration file. Set the preserve_hostname parameter to true in the /etc/cloud/cloud.cfg file before rebooting your computer.
Change the Pretty Hostname
The static hostname is the only hostname that is used for network communication. However, the pretty hostname is displayed only to the user. If you have multiple Ubuntu systems, then the pretty hostname comes in handy. Use the following command to change the pretty hostname on your Ubuntu Linux computer:
hostnamectl set-hostname "My Ubuntu Linux laptop" --pretty
Change Hostname on Ubuntu Using GUI
You can also change the Ubuntu hostname using the Graphical User Interface (GUI) if you are not comfortable with the command-line interface. Follow these steps to change the Ubuntu hostname using GUI:
1. Open Settings and click About.
2. Locate and click the Device Name field:
3. Replace the current hostname with the new hostname:
4. Click Rename.
We are using Ubuntu 22.04 LTS in this tutorial. The screens may be different in other versions of the Ubuntu Linux operating system.
In this tutorial, we learned to change the hostname in Ubuntu Linux. We have described three different methods that you can use to change the hostname- using commands, editing configuration file, and using GUI. You can use any of these procedures based on your requirement. If you do not want to use the command-line interface, then you can use the GUI mode.