How to change the hostname of your Linux server

Atualizado em 2026-10-23 00:01:39 141 visitas

In this manual, we will explain in a simple way the steps you must follow to change the hostname of your Linux server. Before going into detail, it is useful to review exactly what a hostname is.

What is a server's 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 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 'equipo1' while the machine with IP 192.168.1.31 can be assigned the hostname 'máquina2'. In this way, both the administrator and the users will be able to interact with both machines without the need to use IP addresses, which can be difficult to memorize or could even change.

What is a 'Fully Qualified Domain Name'?

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, let's assume we have a computer network called 'universidad':

  • universidad.edu could be an FQDN (assuming the domain was registered), allowing the network to be accessible from the internet.
  • equipo1.universidad.edu would also be an FQDN, allowing the machine 'equipo1' to be accessible from the internet.

In general, an FQDN provides a unique address that can be used by any computer on the internet to communicate with any other.

Changing the hostname of a Linux server (Ubuntu)

info This manual follows the steps necessary to change a hostname in Ubuntu 22.04. For other distributions, the necessary steps may be different. It is assumed that you have superuser privileges. You may need to use 'sudo' before each command.

Before starting the tutorial, don't you have a Cloud server with Ubuntu 22.04? Don't worry! With just one click, you can easily deploy it with SW Hosting.

1. Change the hostname by editing the /etc/hostname file

nano /etc/hostname

This command will open a text editor that allows us to edit the server name. Once the changes are made, we only need 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

hostname computerName

Using the hostname command, we can change the computer name, only, for the current session. Upon the next reboot, the server will read the name written in the /etc/hostname file again. In any case, this command allows us to change the hostname without needing to restart the server, making it suitable for applying the changes immediately.

3. Set the server's FQDN

It may be very desirable to also set the server's FQDN, allowing it to know its own fully qualified name.

To achieve this, we must edit the /etc/hosts file:

nano /etc/hosts

Next, we will create a new entry with the following data:

<ip-address> <hostname.domain.extension> <hostname>

For example:

185.32.12.35 equipo1.universidad.edu equipo1

The first term is the IP address under which the computer can be located. The second term is the FQDN and the third term is an alias, allowing equipo1 itself to be located 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.

Remember that if you do not yet have a Cloud server with Ubuntu 22.04, you can easily deploy it with SW Hosting.