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:
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
Display the new hostname:
$ hostname
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
The “itslinuxfoss” has been set as the new hostname of the computer:
$ hostname
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
The Hostname has been changed successfully:
$ hostname
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
To confirm the changes, run the command:
$ hostname
Command 5: Using the nmtui Command Utility
To change the hostname using the nmtui command, run the command:
$ nmtui
Then select the “Set system hostname” from the list:
Set a new hostname for the Debian 12 and click on “OK”:
The Hostname has been set successfully using the nmtui command:
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
Update the hostname:
Save the file with the CTRL+S and exit the nano text editor. Now confirm the changes by displaying the hostname:
$ hostname
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:
Locate and click the “Sharing” option, also turn on the toggle button:
Set a new hostname:
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.