How do I change the host name under Debian 11 with hostnamectl?
What's in the box?
Under Linux, the hostnamectl command is used to manage the machine's host name: it's very useful if you want to give your server or workstation another name from the command line. Here, we'll take the example of a Debian 11 machine, but it works in the same way on Linux systems where there are systemd.
If you're looking to perform basic tasks under Debian, here are two more articles:
- Debian 11 : comment ajouter ou changer la passerelle par défaut ?
- Définir une adresse IP statique sur Debian 11
In general, I invite you to browse the site, as there are many interesting articles on Linux.
1. The hostnamectl command
Before we look at how to use this command, a brief introduction is in order. First of all, you need to know that the " hostname "of a machine corresponds to its host name, and that the hostnamectl command is used to manage the host name.
Under Linux, including Debian 11, the host name is stored in the " /etc/hostname "of the system. As a result, the order will allow you to change the host name without having to manually open and edit this file.
This command is simple to use, but indispensable for anyone wishing to learn Linux administration.
2. How do I display the current host name?
Let's get down to business! I'm using a Debian 11 machine. Currently, the host name of this machine is "debian". Yes, that's not very original. To see the current name, you can read the contents of the " /etc/hostname" command, use the hostname but also use hostnamectl.
cat /etc/hostname
hostname
hostnamectl
Even if the result of the hostnamectl command is more complete, we always get confirmation that the host name is "debian". Finally, you can also obtain the machine name by looking at the Terminal prompt "root@debian", which means you're logged in as "root" on the "debian" machine.
Visit hostnamectl command returns other information, such as chassis type, machine ID, operating system, memory, etc. Linux kernel versionarchitecture, etc.
3. How do I use the hostnamectl command?
Under Linux (Debian 11, Ubuntu, etc.), the hostnamectl command can handle two different hostnames:
- Le nom d’hôte statique : le nom d’hôte principal, indiqué dans le fichier /etc/hostname
- Le nom d’hôte pretty : le nom d’usage, qui est secondaire et qui sera inclus à /etc/machine-info
Important note: the host name pretty can contain spaces, unlike a static host name.
The aim is to change the host name from "debian" to "debian11". To do this, we use this syntax :
hostnamectl set-hostname debian11
or
hostnamectl set-hostname debian11 --static
Following this command, the contents of the "/etc/hostname" file are different: it returns "debian11"!
Now we'll determine a secondary name (pretty) for this machine, where you can be more precise. For example, the name "Debian 11 Tutobox.fr" using the "-pretty" option instead of "-static". Here's the magic command:
hostnamectl set-hostname "Debian 11 Tutobox.fr" --pretty
Then, if we look at the host name with hostnamectl, we see a new line appear!
As for the " /etc/machine-info" it does exist and contains this information.
cat /etc/machine-info
To display only the static name or the pretty name, use one of these two commands (depending on the information required):
hostnamectl --static status
hostnamectl --pretty status
4. Conclusion
After reading this article, you will be able to change the hostname of your Linux machine using the hostnamectl command ! If you need to explore further options, please consult the help section:
hostnamectl --help
Resources :