· Tutorial ·

How to change the hostname of your Linux server

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.

What is the hostname of a server?

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.

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, suppose we have a computer network called "university ":

  • university.edu could be an FQDN (assuming the domain was registered) allowing the network to be accessible from the internet.
  • computer1.university.edu would also be an FQDN allowing the machine "computer1" 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 computer.

Change the hostname of a Linux (Ubuntu) server.

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

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

Your Cloud with Ubuntu 22 from $5.60/month

Get the most out of your project with the fastest disks and most powerful CPUs in the Cloud.

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

nano /etc/hostname

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

hostname computerName

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:

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 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.

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

Your Cloud with Ubuntu 22 from $5.60/month

Get the most out of your project with the fastest disks and most powerful CPUs in the Cloud.

i