How to Change Hostname on Debian 12 Linux

The name by which a specific device is recognized within a network is called the hostname. Whether one is setting up a new server or configuring the network, hostname plays an important role. 

A unique name can be set as a hostname by which other machines recognize your machine within a network. Also, the hostname can be changed at any instant time on Debian and other Linux distributions.

This post explains the methods to change the hostname on Debian 12 by both the command line method and the GUI method.

What are the Methods of Changing a Hostname on Debian 12?

On Debian 12, there are three different methods for changing the hostname:

  1. Using the Command Utilities 
  2. Editing the Hostname File
  3. Using the GUI method

Changing Hostname by all above methods has been explained in detail. 

Method 1: Using the Command Utilities

To change the hostname by executing some commands, a terminal of Debian 12 can be used. 

Different commands are used to change the hostname using the terminal which are explained in the next section. 

Command 1: Using the hostname Command Utility

To change the hostname with the “hostname” command utility, use the general syntax:

$ sudo hostname [new hostname]

For example, the hostname “itsForExample” has been set as a new hostname:

$ sudo hostname itsForExample
Change Hostname on Debian 12 Linux 1

Display the new hostname:

$ hostname
Change Hostname on Debian 12 Linux 2

Command 2: Using the hostnameclt Command Utility

To change the static hostname of the computer, use the “hostnamectl” command utility following its general syntax:

$ sudo hostnamectl set-hostname [new Hostname]

For example, set back the Hostname to “itslinuxfoss”:

$ sudo hostnamectl set-hostname itslinuxfoss
Change Hostname on Debian 12 Linux 3

The “itslinuxfoss” has been set as the new hostname of the computer:

$ hostname
Change Hostname on Debian 12 Linux 4

Command 3: Using the nmcli Command Utility

The “nmcli” is the next command utility to change the hostname on Debian 12. To change the hostname using the “nmcli”, follow the general syntax:

$ sudo nmcli general hostname [new Hostname]

To set the new hostname to “itsForExample”, use the command:

$ sudo nmcli general hostname itsForExample
Change Hostname on Debian 12 Linux 5

The Hostname has been changed successfully:

$ hostname
Change Hostname on Debian 12 Linux 6

Command 4: Using the sysctl Command Utility

The next method to change the hostname on the computer is by using the “sysctl” command. The “sysctl” command may be used to change the hostname by using the general syntax:

$ sudo sysctl kernel.hostname=[new Hostname]

For example, to set back the hostname to “itslinuxfoss” using the nmcli command:

$ sudo sysctl kernel.hostname=itslinuxfoss
Change Hostname on Debian 12 Linux 7

To confirm the changes, run the command:

$ hostname
Change Hostname on Debian 12 Linux 8

Command 5: Using the nmtui Command Utility

To change the hostname using the nmtui command, run the command:

$ nmtui
Change Hostname on Debian 12 Linux 9

Then select the “Set system hostname” from the list:

Change Hostname on Debian 12 Linux 10

Set a new hostname for the Debian 12 and click on “OK”:

Change Hostname on Debian 12 Linux 11

The Hostname has been set successfully using the nmtui command:

Change Hostname on Debian 12 Linux 12

Method 2: Editing the Hostname File

The hostname file, which can be found t /etc/hostname, contains the hostname settings. Use the nano text editor with the root privileges to open the hostname file:

$ sudo nano /etc/hostname
Change Hostname on Debian 12 Linux 13

Update the hostname:

Change Hostname on Debian 12 Linux14

Save the file with the CTRL+S and exit the nano text editor. Now confirm the changes by displaying the hostname:

$ hostname
Change Hostname on Debian 12 Linux 15

Note: Reboot the System to apply changes. 

Method 3: Using the GUI Method

The last method of changing the “Hostname” is its GUI method. For this method, open the “Settings” of Debian 12:

Change Hostname on Debian 12 Linux 16

Locate and click the “Sharing” option, also turn on the toggle button:

Change Hostname on Debian 12 Linux17

Set a new hostname:

Change Hostname on Debian 12 Linux 18

The new hostname has been set. 

These are all the methods by which the hostname can be changed on Debian 12. 

Conclusion

The hostname can be changed on Debian 12, by executing different commands, editing the hostname file, or from the “Settings”. All the mentioned methods have been explained in this post by demonstrating them with examples.