A hostname is the identifier of the Ubuntu (or any Linux) machine. It is used by the user and the services to carry out regular operations within the system. Moreover, the network to whom your system is connected also utilizes the hostname to distinguish the system from others within the network.
Have you ever tried changing the hostname on Ubuntu? It is possible and this post will explore all the possible approaches that can be used to change the hostname on Ubuntu 22.04, with the following outline.
Types of Hostnames in Ubuntu
Ubuntu (and other Linux systems) support three types of hostnames, i.e., Pretty, Static, and Transient. Each type has its own functionality which is described explicitly as follows:
- Pretty Hostname: The user uses a pretty hostname to identify the system. It is stored in the “/etc/machine-info” file.
- Static Hostname: The services and the user itself use this hostname to identify/refer the system locally. Static hostname can be found in the “/etc/hostname” or “/etc/hosts” files.
- Transient Hostname: A hostname stored in the kernel variable (usually the same as the static hostname). The transient hostname is set to identify the local system over the network.
How Does Ubuntu Pick the Hostname?
Ubuntu picks the hostname in a specific order (same as listed above), pretty hostname first followed by the static and transient. If Pretty is not present, the system executes the static as its hostname. Similarly, if the static is not present the system fetches the transient hostname.
Naming Conventions for a Hostname in Ubuntu
There is a set of rules to configure the hostname in Ubuntu. Among three types, the pretty hostname has no restrictions (as it belongs to the user only). The static and the transient hostnames must comply with the following naming convention:
- The maximum length for a hostname (static and transient) is 64 characters which must comply with the UTF8 encoding scheme.
- A hostname only consists of: Letters (aA-zZ), digits (0-9), Underscores (_), dots(.), or Hyphen (-).
- The chosen hostname must start with a digit/letter.
Let’s start with the approaches to changing the Hostname in Ubuntu 22.04.
How to Change the Hostname in Ubuntu 22.04?
The hostname in Ubuntu can be changed using the terminal and the GUI support. The users must have the sudo rights or root user account to proceed further. First, start with the hostnamectl command:
Approach 1: How to Change the Hostname Via the hostnamectl Command?
The hostnamectl command manages (changes/sets) the hostname in Ubuntu (and other Linux distributions). It is used to set the static, pretty, and transient hostname on Ubuntu. Here’s the detailed process:
First, check the hostname using one of the hostnamectl commands mentioned below:
$ hostnamectl status
Note: If you want to get the hostnames as per their types, i.e., pretty, static, or transient, then use the command as per the type:
$ hostnamectl –pretty #Pretty Hostname
$ hostnamectl –static #Static Hostname
$ hostnamectl –transient #Transient Hostname
Syntax to Change the Hostname
The hostnamectl command has the specific syntax to be used to change the hostname in Ubuntu:
$ sudo hostnamectl set-hostname [New-Hostname] [Options]
The [New-Hostname] refers to the name being set. The [Options] could be, “–pretty”, “–static”, or “–transient” to refer to the specific type of the hostname.
Change the Pretty Hostname
First, check the current pretty hostname of the system using the following command:
$ hostnamectl –pretty
Right now, no pretty hostname is set as can be seen. Now, use the command below to set the pretty hostname to “genie-ubuntu”. The pretty hostname to be set must be enclosed in the double quotations (the double quotation mark is a must for pretty hostname only):
$ sudo hostnamectl set-hostname “genie-ubuntu” –pretty
Confirm the changed name via the command:
Change the Static Hostname
The static hostname is used and referred to the most in Ubuntu, as it is used by the user and the services as well to identify/refer the PC. To change the static hostname, use the hostnamectl command, and check the current static hostname:
The static hostname is genie. It is changed to “LG-Ubuntu”, via the command:
$ sudo hostnamectl set-hostname LG-Ubuntu –static
Note: If you do not mention the option “–static”, the command will automatically change the static hostname. Moreover, whenever the static hostname is changed, the transient is changed automatically.
Change the Transient Hostname
The Transient hostname is set in the Linux kernel to let the network systems identify your system. First, check the current transient hostname using the command:
$ hostnamectl –transient
Use the following hostnamectl command to change the transient hostname:
$ sudo hostnamectl set-hostname Genie-The-Giant
Check the newly changed Transient hostname:
$ hostnamectl –transient
Interestingly, if you check the static hostname, it would also be the same:
It is concluded that the static and the transient hostname are changed simultaneously. The value of the transient hostname will only be different if there are other computers/machines with the same hostname in the same network.
Note: To make the changes effective in the terminal, you must have to start the new terminal session.
Approach 2: How to Change the Hostname Via the hostname Command?
The hostname command changes/sets the hostname temporarily and is applicable only for the ongoing machine session. When the machine is switched off or a reboot is attempted, the hostname is switched to the previous one:
Let’s check the current/existing hostname, using the command:
The command below will change the current hostname to ubuntu-genie:
$ sudo hostname ubuntu-genie
Verify the change:
Note: Remember, the hostname set using the hostname command is temporary only.
Approach 3: How to Change the Hostname Via the nmtui Utility?
NMTUI is a terminal-based graphical interface of the Network Manager. NMTUI is used to manage/configure all the Network Manager’s related operations, including the hostname. The detailed process is as follows:
First, open the terminal and use the command below:
The following interface appears. Navigate to the hostname option (using the arrow keys) and hit enter:
The opened screen will show the current hostname (static) of the system:
Change the hostname to the new one. Navigate to the “OK” key using the tab key and then hit enter to set the new hostname:
Enter the sudo password to activate the newly set hostname:
The following prompt confirms the hostname has been changed to UbuntuGenie, choose OK to proceed further:
Approach 4: How to Change the Hostname Via the File /etc/hostname? | Static and Transient
The “/etc/hostname” file contains the hostname of the machine (static/transient). The value stored in the “/etc/hostname” file is reflected in the hostname and hostnamectl commands output. Here’s how you can change the hostname by accessing the “/etc/hostname” file:
Open the “/etc/hostname” file in any terminal-based editor (nano in our case):
$ sudo nano /etc/hostname
You will see only the hostname set at the moment:
Change the hostname to “LG-LinuxGenie”:
Come out of the editor and reboot the system to apply the changes. After that, use any of the command hostname/hostnamectl to confirm the changes:
Note: The hostname changed using Approach 4 needs a reboot to apply the changes.
Approach 5: How to Change the Hostname Via the File /etc/machine-info? | Pretty Hostname
The “/etc/machine-info” file specifically refers to the Pretty hostname on Ubuntu. Thus, by editing this file, you would only be able to change the Pretty hostname. Here’s the detailed process:
First, access the file with any text editor (terminal-based, nano, or vim):
$ sudo nano /etc/machine-info
Right now, the hostname is “genie-ubuntu”. Change it to “LinuxGenie”. Use the keys “CTRL+S” and “CTRL+X”, to save the changes and close the editor:
The pretty hostname is set. Restart your machine to observe the changes. Once done, verify the new hostname using the command:
The newly set pretty hostname is displayed (if the pretty hostname is not set, the hostnamectl command would not show the “Pretty hostname” option).
Approach 6: How to Change the Hostname From Ubuntu Settings? | GUI
The About section in the Ubuntu settings carries the hostname which can be changed as per the requirement. Open the Ubuntu settings and navigate to the About section. Here, the Device Name represents the hostname of the machine:
Click on it and choose the new name. Finally, click on the Rename button to set/change the name:
Now, the device name has been changed to the one set above:
The changes will be reflected automatically in the command line interface as well. Similarly, if you have set the static hostname using any of the above (CLI) approaches, the hostname will also be changed here in GUI as well.
Bonus Tip: How to Resolve the Hostname With IP Address in Ubuntu 22.04?
The IP addresses and the hostnames are interlinked phenomena. Whenever you change the hostname, it is recommended (not necessarily) to update the hostname in the “/etc/hosts” file as well. This helps in recognizing the hostname with the associated IP address. The “/etc/hosts” file contains all the hosts on the current machine and the IP addresses associated with those hosts.
Start with checking the current hostname, via the command (you can use hostname as well):
The hostname is “LGenie”. Now, open the “/etc/hosts” file and insert the name “LGenie” in front of the IP Address you want to map to (usually it is 127.0.1.1):
$ sudo nano /etc/hosts
Save and exit out of the editor. This resolution lets you resolve any IP address/Hostname conflict.
In Ubuntu, the hostname can be changed using both the CLI and the GUI approaches. The terminal methods have two categories. One is by using the commands, hostnamectl, hostname, and nmtui. While the other terminal-based approach refers to modifying the files, i.e., “/etc/hostname” and “/etc/machine-info” files. While the About section of the Ubuntu settings can be accessed to change the hostname.
Among the described methods, the hostname command only changes/sets the hostname temporarily while other approaches change the hostname permanently.
This post has briefly elaborated on the methods for changing the hostname in Ubuntu 22.04.