After successfully upgrading to Debian 11 or Debian 12, you update the old hostname to reflect the new Debian version. In this brief guide, I will explain how to set or change hostname in Debian 12 Bookworm and Debian 11 bullseye systems from commandline. For the purpose of this guide, I will be using Debian 11 bullseye, but the setting up hostname is same for Debian 12 as well.
Table of Contents
View hostname in Debian
A hostname is a label assigned to a Computer in order to identify it from on LAN or WAN. A typical hostname consists of alphanumeric and special characters. We can use at most 253 characters in hostname and it shouldn't start with a hyphen or a dot.
Finding hostname in Debian Linux is easy!
To display the hostname of a Debian system, simply enter hostname
command in the Terminal:
$ hostname debian10
To show the fully qualified domain name (FQDN), use -f
flag.
$ hostname -f
You can also use --fqdn
flag to view the fully qualified domain name:
$ hostname --fqdn
All of the aforementioned commands displays the hostname only. If you want to display more details, such as hostname, chassis type, machine id, operating system details, kernel and architecture, use hostnamectl
command:
$ hostnamectl
Sample output:
Static hostname: debian10 Icon name: computer-vm Chassis: vm Machine ID: aa47d2d8352c404898f47e00562730d3 Boot ID: 9a12f2136ba74df5bd378bc837869e9b Virtualization: kvm Operating System: Debian GNU/Linux 11 (bullseye) Kernel: Linux 5.10.0-8-amd64 Architecture: x86-64
As you see in the above outputs, my Debian system's hostname is debian10, but the operating system version is Debian 11. I already upgraded it to Debian 11, but haven't changed the hostname yet. Let us change its hostname now.
Set or change hostname in Debian 12 / 11
We can set or change the hostname in couple ways. Here, I have listed 6 different methods to set hostname in Debian 12 or 11.
Method 1 - Using hostname command
To quickly change or set hostname in a Debian 11 system, simply run:
$ sudo hostname debian11
This will set the hostname as debian11
. Replace debian11 with a hostname of your choice.
To view the changes in the hostname, use one of the above commands:
$ hostname debian11
Please note that this command will only set the hostname temporarily. The changes will be gone after system reboot.
If you want to permanently set or change hostname, use any one of the following methods.
Method 2 - Using hostnamectl command
This is the standard and most commonly used method to permanently set hostname in Debian and other Linux distributions.
To set permanently set or change hostname in Debian bullseye using hostnaectl command, run:
$ sudo hostnamectl set-hostname debian11
Log out and log in again to view the changes in hostname.
$ hostnamectl Static hostname: debian11 Icon name: computer-vm Chassis: vm Machine ID: aa47d2d8352c404898f47e00562730d3 Boot ID: 9a12f2136ba74df5bd378bc837869e9b Virtualization: kvm Operating System: Debian GNU/Linux 11 (bullseye) Kernel: Linux 5.10.0-8-amd64 Architecture: x86-64
If you want to view only the hostname, use --static
flag.
$ hostnamectl --static debian11
Heads Up: You will keep getting a warning message like below, whenever you use sudo
command after changing the hostname.
sudo: unable to resolve host debian11: Name or service not known
To get rid of this warning, edit the /etc/hosts
file:
Add/update the hostname in it.
Save and close the file. Log out and log back in to take effect the changes.
Method 3 - Using nmcli command
To set hostname using nmcli
command in Debian, run:
$ sudo nmcli general hostname debian11
Replace debian11 in the above command with your own hostname. Log out and log back in to apply the changes in hostname.
Method 4 - Using sysctl command
To change or set hostname in Debian using sysctl
command, run:
$ sudo sysctl kernel.hostname=debian11
Log out and log in again to apply the changes.
Method 5 - Add or update hostname in /etc/hostname file
Edit /etc/hostname
file in your preferred editor:
$ sudo nano /etc/hostname
and add/update hostname in it:
Save the file and close it. Log out and log in again to update the hostname.
Method 6 - Update hostname in /proc/sys/kernel/hostname file
Open /proc/sys/kernel/hostname
file in a text editor and add/update the hostname in it.
Alternatively, you can append the new hostname in /proc/sys/kernel/hostname
file using the following one-liner command:
$ sudo sh -c "echo 'debian11' > /proc/sys/kernel/hostname"
In this guide, we have learned 6 possible ways to set, change and update hostname in Debian 11 bullseye from command line. Hope this helps.
Related read:
1 comment
Hostname is used everywhere in Linux. EG: Bind name server is just one. SUDO is another one according to the text.