The Linux hostname command: view and change the computer name

In any Linux system, the name of the computer, also known as the hostname, is an identifier that allows a machine to be distinguished within a network. Knowing and being able to change this name is essential for system administrators, developers and users working with multiple machine environments.

What is the hostname command?

The commandhostnameis a tool included in most Linux distributions that allows you to view or set the host name of the system. Although it does not require superuser privileges to read the value, changing it often requires root permissions.

See the current name of the equipment

To consult the current hostel, just run:

hostname

This will print the name assigned to the kernel at that time. Another alternative is to read the contents of the file/etc/hostnameor use the commandcat /etc/hostname.

Change the name temporarily

If you want to change the hostname only for the current session (the value will be lost when restart), you can use:

sudo hostel new-name

After running the command, check the change withhostnameAgain.

Change the name permanently

For the change to persist after a reboot, you must modify the system configuration files. In Debian / Ubuntu based distributions, the file/etc/hostnamecontains the static name. Edit it with your favorite editor:

sudo nano / etc / hostname

Replace the existing name by the desired and save the file. Subsequently, update the file/etc/hostsfor the IP address of loopback (127.0.0.1) to be associated with the new name:

127.0.0.1 localhost127.0.0.1 new-name

Finally, restart the hostname service or simply restart the machine:

sudo systemctl restart systemd-hostnamed

or

sudo reboot

Considerations and good practices

  • Avoid using special spaces or characters in the hostel; use letters, numbers and scripts.
  • In cloud or container environments, the hostname can be managed by orchestrators such as Kubernetes or Docker Swarm; in such cases, consult the specific documentation.
  • After changing the hostname, check that name-dependent services (e.g. SSH, databases or internal applications) continue to function properly.

With these steps, you can view and change the name of your Linux computer safely and effectively, adapting it to the needs of your infrastructure or daily workflow.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish