A hostname is a name given to a device connected to a network. This is a unique identifier that network administrators assign to devices during setup or maintenance. Note that hostnames are for people because humans prefer words over number strings (IP addresses).
In Ubuntu 20.04, sysadmins generally assign a hostname to the machine during the OS setup. However, as you will see in this article, there are several ways to change the hostname, both through the command line and the GUI.
But first, let’s start with a short introduction to hostnames.
Table of Content
- What is a Hostname?
- Why Would You Want To Change a Machine’s Hostname?
- How to Change Hostnames on Ubuntu 20.04
- Find the Current Hostname in Ubuntu 20.04
- Change the Hostname Temporarily on Ubuntu 20.04
- Change The Hostname Without Rebooting The Server
- Change the Hostname by Rebooting the Server
- Change the Hostname By Editing the /etc/hostname File
- Change the Hostname By Editing the /etc/hosts File
- Change Hostname on Ubuntu 20.04 Using GUI
What is a Hostname?
Most machines connected to the Internet are assigned a hostname, a unique name that identifies it on the Internet or network. Now, devices on a network don’t really need hostnames to communicate. Instead, they use IP addresses to identify and exchange information.
A hostname can have two components – a hostname and a domain name- separated by a period. The allowable length of the hostname is 255 characters.
While you can name the devices anything you want, you should follow specific guidelines and ensure that the hostnames describe the machine (and its location within the network) for quick identification.
Why Would You Want To Change a Machine’s Hostname?
Like IP addresses, no two devices can have the same hostname on a network; otherwise, a conflict would affect both devices and the network.
Many IT teams have a device naming convention that aligns with the organization’s asset management policies. In addition, several automated asset management platforms use custom hostnames for smooth management.
Network administrators can change hostnames for security reasons. One of the best practices is to choose hostnames that aren’t easy to guess to thwart network intrusions.
How to Change Hostnames on Ubuntu 20.04
Here are the prerequisites:
When changing hostnames on Ubuntu 20.04 machines, you need the following:
- An admin account or a user with sudo privileges.
- Access to command line tools and a GUI for changing hostnames through your preferred method.
Now, let’s go through the ways you can change a machine’s hostname on Ubuntu 20.04. But before that, let’s go through the process of finding the current hostname of a machine.
Find the Current Hostname in Ubuntu 20.04
Before changing the hostname of the Ubuntu machines, you need to know the current hostname of the machine.
For this, enter the following command in the terminal to display the hostname:
If you need more details, use the hostnamectl command that displays additional information about the machine.
Now that you know how to find the hostname, let’s go into the details of changing the hostname in Ubuntu 20.04.
Change the Hostname Temporarily on Ubuntu 20.04
Ubuntu 20.04 allows you to set a temporary hostname for a machine on the network. Note that this change will remain effective until the device is rebooted.
Use the following command to change the hostname temporarily:
# hostname <new hostname>
Since the command doesn’t provide any output, you need to verify that the command has successfully changed the hostname. For this, we’ll use the hostname command to get the latest hostname:
Change The Hostname Without Rebooting The Server
Sometimes, rebooting a server machine is not an option. If you need to change the hostname for such a server, we recommend using the hostnamectl command.
#hostnamectl set-hostname <new-hostname>
Remember to replace <new-hostname> with the new hostname. Since you won’t get any status updates, use the hostname command to verify the change.
Change the Hostname by Rebooting the Server
The easiest way of permanently changing the hostname is to change the entries in the system config files. The hostname records are stored in two files: /etc/hostnames and /etc/hosts.
You can easily change the system’s hostname by editing these files. The only caveat is that the changes will take effect after the system is rebooted.
We’ll now go into the details of these options.
Change the Hostname By Editing the /etc/hostname File
The /etc/hostname contains the current hostname of the system. You can assign a new hostname to the system by editing this file.
Open the file in your preferred file editor. We’ll use Vi for this demonstration.
Start with the following command in the terminal:
# vi /etc/hostname
Find the hostname of the system and replace it with the new hostname. Next, save and exit the editor.
The /etc/hosts file contains a record of all the hosts on the network. Usually, this is a simple text file you can edit with a text editor. Like the previous demonstration, we’ll use Vi for editing this file.
# vi /etc/hosts
You need to find and replace the current hostname with the new hostname. Finally, save and exit the editor.
Important: To apply the changes, you need to reboot the device. For this, use the following command:
# sudo systemctl reboot
Optional: If you’re running Ubuntu 20.04 as a cloud instance (mainly through the cloud-init package), you must edit the /etc/cloud/cloud.cfg file in a file editor. Search for the parameter preserve_hostname and set the value to TRUE.
Change Hostname on Ubuntu 20.04 Using GUI
Ubuntu supports several popular GUI options, and many admins prefer changing the hostname on Ubuntu 20.04 systems through the GUI. The process is pretty simple and involves the following steps.
Step #1: Go to Settings
To start the process, go to Settings and locate the About section.
Step #2: Find the Device Name
Find the Device Name in the About section. Usually, it’s located at the top of the screen.
Step 3: Change the Device Name
Click the Device Name, and a new dialog box opens up. You can now enter a new hostname for the system. Click the green Rename button to finalize the configuration. Note that this change is permanent and goes into effect immediately.
This article presents three methods of changing the hostname on an Ubuntu 20.04 machine: using the command line tools, editing the host files, and through the GUI. All the methods are straightforward, so you can choose whichever fits your use case best.
Discover the hosting possibilities with Redswitches, a well-known dedicated hosting company. Our excellent solutions provide customers with high-performance, safe, and dependable hosting services worldwide. We can help you if you’re an individual in need of a strong web presence or a business looking for powerful hosting. Our team’s mission is offering specialized hosting solutions that guarantee optimal uptime and peace of mind.
If you’re looking for a robust server for your Ubuntu projects, RedSwitches offers the best dedicated server pricing and delivers instant dedicated servers, usually on the same day the order gets approved. Whether you need a dedicated server, a traffic-friendly 10Gbps dedicated server, or a powerful bare metal server, we are your trusted hosting partner.
Q. Why would I need to change the hostname on Ubuntu 20.04 LTS?
Changing the hostname can help personalize your system and make it easier to identify on a network. It’s particularly useful when managing multiple Ubuntu machines.
Q. Is it necessary to restart my system after changing the hostname?
Yes, rebooting your system to apply the new hostname fully is recommended.
Q. Can I use any name as my hostname?
While Ubuntu is flexible with hostname choices, using only alphanumeric characters and hyphens is best to avoid spaces or special characters.
Q. Will changing the hostname affect my system’s performance or data?
Changing the hostname won’t impact your system’s performance or data. It’s a safe and reversible process.
Q. What’s the difference between the hostname and the fully qualified domain name (FQDN)?
The hostname is a shorter, local identifier, while the FQDN includes the hostname and domain name, making it suitable for use on the Internet.
Q. Can I change the hostname without administrative privileges?
You need dministrative privileges (sudo) to modify the hostname.
Q. Is it possible to revert to the previous hostname if needed?
Yes, you can revert to the previous hostname or choose a different one at any time using the same process outlined in the guide.
Q. Will changing the hostname affect network services or configurations?
It’s unlikely to affect network services directly, but updating any relevant configurations using the new hostname is essential to ensure proper functionality.
Q. Are there any security concerns when changing the hostname?
Changing the hostname doesn’t introduce security risks but ensures that your new hostname adheres to best practices and doesn’t inadvertently expose sensitive information.
Q. What should I do if I encounter issues or errors during the hostname change process?
If you face any problems, double-check the steps and consult Ubuntu support or online forums to resolve specific issues.