How to Change the Hostname on Debian 12?


A hostname is a human-readable and unique custom name assigned to a device connected to a network. It identifies a device or a server on a network. The hostname is combined with a DNS (Domain Name Server) domain to form a domain name. It is essential for different network-related tasks and is utilized by devices to interact with each other. The hostname is user-generated, and a user sets the hostname during the Debian 12 installation. However, a user can change the hostname whenever required.

This article will demonstrate how to change a hostname on Debian 12 (Bookworm) systems. This article is organized as follows:

Let’s begin by discussing the types of hostnames.

What are the Types of Hostnames?

The hostnames are classified into three types:

  • Static Hostname: The static hostname is the main hostname set during the installation of Debian 12. It is mainly used to identify devices on the internet.
  • Pretty Hostname: The pretty hostname is a hostname that is used for display purposes. It is a user-friendly hostname and enables a user to add special characters.
  • Transient Hostname: A transient hostname is stored in the Linux kernel and is only valid till the system is restarted.

How to Set a Hostname?

A hostname always begins and ends with letters. Additionally, a static and transient hostname can contain a hyphen “” and a period “.”, while pretty hostnames can contain any special character. Further details about setting a hostname are as follows:

  • Length of a hostname: 2 – 64 Characters.
  • Numbers: 0-9
  • Alphabets: aA-zZ

How to Check a Hostname on Debian 12?

The hostname command line utility is used to show and modify the hostname. To find a hostname via the hostname utility on Debian 12, run the following command:


The above output indicates that the system’s hostname is debian.

Alternatively, the hostnamectl command line utility displays the types of hostnames and modifies the hostname. To find a hostname via the “hostnamectl” command on Debian 12, run the following command:


How to Change the Hostname via the Command Line Utilities on Debian 12?

A hostname is changed on Debian 12 by command line utilities like hostnamectl, hostname, sysctl, nmcli, and NMTUI which will be demonstrated In the following sections:

How to Change the Hostname via the hostnamectl Command on Debian 12?

The hostnamectl command changes the static and pretty hostname. To change the static hostname from “debian” to debian12 permanently, execute the following command:

sudo hostnamectl set-hostname debian12

The error-free execution of the above command indicates that the hostname is successfully changed. The hostname will reflect on the terminal after the terminal is restarted. Therefore, restart the terminal and verify the hostname via the hostnamectl command as follows:

The static hostname is now changed from debian to debian12.

Alternatively, the newgrp command can be used to change the hostname at the terminal (without restarting it):


To change the pretty hostname to debian~12, execute the following command:

sudo hostnamectl set-hostname “debian~12” –pretty

To verify the pretty hostname change, run the following command:


How to Change the Hostname via the hostname Command on Debian 12?

The hostname command changes the transient hostname. The hostname change via the hostname command is valid till the system is rebooted. To change the transient hostname to “debian”, run the following command:


To verify the change, run the following command:


How to Change the Hostname via the sysctl Command on Debian 12?

The sysctl command is utilized to configure kernel parameters. The systctl command is also used to change the transient hostname. To change the transient hostname to linuxgenie, the hostname parameter of the kernel is modified by running the following command:

sudo sysctl kernel.hostname=linuxgenie

To verify the change, run the following command:


How to Change the Hostname via the nmcli Command on Debian 12?

The nmcli command line utility controls the network manager. The nmcli command also changes the static hostname. To change the static hostname from debian to debian12, run the following command:

sudo nmcli general hostname debian12

Where general is a sub-command of nmcli that manages the general settings of Network Manager.

Restart the terminal and verify the hostname via the hostnamectl command as follows:


How to Change the Hostname via the NMTUI (Network Manager Text User Interface) on Debian 12?

The NMTUI is a graphical command line utility to manage a network. Along with configuring network interfaces, the NMTUI enables a user to change the static hostname permanently. To change the hostname, from “debian” to “debian12”, via NMTUI, first launch the NMTUI utility executing the following command:

Select “Set system hostname” and press OK:

A new window: “Set Hostname” is opened. Type the host name, i.e., “debian12” in the “Hostname” field and press OK:

A user is prompted for authentication as NMTUI changes the hostname permanently. Type the password and press “Authenticate”:

A new window is opened, prompting the user for confirmation to update the hostname to “debian12”. Press “OK” to confirm:

Restart the terminal and use the hostnamectl command to verify:


How to Change the Hostname Using the Configuration Files on Debian 12?

The static hostname is changed by editing the below-listed configuration files located in the etc directory:

  • /etc/hosts: The hosts file is a text file that maps the hostname to the IP address
  • /etc/hostname: The hostname file is also a text file that contains the hostname. To change the hostname permanently

To change the static hostname from “debian” to “debian12”, follow the below steps:

Step 1: Edit the /etc/hosts File

Launch the file /etc/hosts in any text editor, for example, nano text editor by running the following command:


The /etc/hosts file has two occurrences of static hostname, i.e., “debian” as shown below:

Edit the /etc/hosts file and replace the hostname “debian” with “debian12” as shown in the below figure:

Finally, save and close the /etc/hosts file by pressing the combination of keys [Ctrl + O] and [Ctrl + X] respectively.

Step 2: Edit the /etc/hostname File

To edit the /etc/hostname file, first launch the file /etc/hostname in any text editor, for example, nano text editor by executing the following command:


Then, edit the file and replace the static hostname “debian” with “debian12”:

Finally, save and close the /etc/hostname file by pressing the combination of keys [Ctrl + O] and [Ctrl + X] respectively.

Step 3: Verify the Change

Restart the system and use the hostnamectl command to verify:


How to Change/Update the Hostname via GUI on Debian 12?

The GUI (Graphical User Interface) enables a user to conveniently change the pretty hostname via the Settings and Sharing section.

To change the pretty hostname via the Settings section, open the Debian settings by searching it from the applications menu:

Then, navigate to the About section (in the left pane) and locate the current hostname, which is shown as “Device Name“. To change the hostname, from “debian” to “debian12”, click “Device Name” as shown below:

 

A new window: “Rename Device” will open. Type the new hostname, i.e., “debian12” and click the “Rename” button:

This changes the pretty hostname. To verify the change, check the “Device Name” in the “About” section in “Settings”:

 

The device name/hostname is now changed from “debian” to “debian12”.

Alternatively, the pretty hostname is also changed via the “Sharing” section (in the left pane). To change the pretty hostname from “debian” to “debian12” via the Sharing section, follow the below path:

Setting Menu >> Sharing >> Turn on toggle button >> Type desired hostname in the “Computer Name” field”:

The new hostname, i.e., debian12 is updated.

That’s all about changing the hostname on Debian 12 systems.

Conclusion

There are multiple ways to change a hostname in Debian 12. A static hostname is changed via command line utilities: hostnamectl, nmcli, nmtui, and by editing the /etc/hosts and /etc/hostname configuration files. On the other hand, the pretty hostname is changed via hostnamectl command and Graphic User Interface (GUI) while the transient hostname is changed via command line utilities: hostname and sysctl. The static and pretty hostnames are changed permanently, and the changes are in effect after restarting the terminal while the transient hostname is valid till the system is rebooted. This article demonstrated diverse ways of changing a hostname on Debian 12 (Bookworm) systems.

Print Friendly, PDF & Email