In this manual, we will explain in a simple way the steps that you must follow to change the hostname of your Linux server. Before going into details, however, it is convenient that we review what exactly this is from the hostname.
The "hostname" or also known as "computer name" is simply a name that identifies a server (or computer) within a computer network. Essentially hostnames serve a purpose similar to domain names and allow you to identify a machine 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 the machine with IP 192.168.1.31 can be assigned the hostname "machine2". In this way, both the administrator and users can interact with both machines without using IP addresses that can be difficult to memorize or could even change.
Fully Qualified Domain Name or FQDN 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.
💡 Attention: This manual follows the steps necessary to change a hostname in Ubuntu 18.04. For other distributions, the necessary steps could be different.
This manual assumes that you have superuser privileges. It may require that you use "sudo" before each command.
1. Change the hostname by editing the file /etc/hostname
This command will open a text editor that will allow us to edit the name of the server. Once the changes are made, we will only have to 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 restart
Using the command "hostname" we can change the name of the computer only for the current session. On the next reboot the server will read again the name written in the file "/etc /hostname". In any case, this command allows us to change the hostname without having to restart the server, so it is appropriate to apply the changes immediately.
3. Set the server FQDN
It can be very desirable to also set the FQDN of the server, allowing it to know its own fully qualified name.
To achieve this, we must edit the file "/etc/hosts":
Next we will create a new entry with the following data:
<dirección-ip> <hostname.dominio.extension> <hostname>
For example: 18.104.22.168 machine1.university.edu machine1`
The first term is the IP address, under which the equipment can be located. The second term is the FDQN and the third term is an alias, allowing the “equipo1” itself to locate itself simply under the name "equipo1".
In this way, we will have established the FQDN for the server and it will no longer be necessary for it to use the DNS system to establish a connection with itself.