In this manual we will explain in a simple way the steps to follow to change the hostname of your Linux server. Before going into details, it is convenient to review what exactly is a hostname.
The "hostname" or also known as a "computer name" is simply a name that identifies a server (or computer) within a computer network. Essentially, hostnames serve a similar purpose to domain names and allow a machine to be identified without the need to memorize IP addresses.
Thus, a machine connected to a private network with IP 192.168.1.30 can be assigned, for example, the name "machine1" while a machine with IP 192.168.1.31 can be assigned the hostname "machine2". In this way, both the administrator and the users will be able to interact with both machines without having to use IP addresses that can be difficult to memorize or that might even change.
Fully Qualified Domain Name or FQDN literally means "fully qualified domain name" and designates the name of a network accessible from the Internet. An FQDN address can include a hostname to allow a machine to be located from the Internet using the DNS system.
For example, suppose we have a computer network called "university ":
In general, an FQDN provides a unique address that can be used by any computer on the Internet to communicate with any other computer.
💡 Attention: This manual follows the steps required to change a hostname in Ubuntu 18.04. For other distributions, the steps required may be different.
This manual assumes that you have superuser privileges. You may be required to use "sudo" before each command.
1. Change the hostname by editing the /etc/hostname file.
This command will open a text editor that will allow you to edit the hostname. Once the changes have been made, simply save the file and close the editor. It is important to note that the changes are not read immediately by the system so it will be necessary to restart the server or change the hostname explicitly with a command.
2. Change the hostname with a command to avoid having to reboot.
Using the "hostname" command you can change the hostname for the current session only. At the next reboot, the server will read again the name written in the "/etc/hostname" file. In any case, this command allows us to change the hostname without restarting the server, so it is suitable to apply the changes immediately.
3. Set the FQDN of the server.
It may be very desirable to set the FQDN of the server as well, allowing the server to know its own fully qualified name.
To do this, edit the "/etc/hosts" file:
Next, we will create a new entry with the following data:
<ip-address> <hostname.domain.extension> <hostname>.
22.214.171.124 computer1.university.edu computer1
The first term is the IP address, under which the computer can be located. The second term is the FDQN and the third term is an alias, allowing team1 to locate itself simply under the name "computer1".
In this way, we will have established the FQDN for the server and it will no longer be necessary for the server to use the DNS system to establish a connection to itself.