How to Change the Hostname on Debian Linux

The computer name or the hostname of a system is used to identify your computer or server on a network. This can be a local network (LAN) or the internet. You are asked to set a computer name or hostname when you install your OS and it must be unique to avoid conflicts. Later when you want to change your computer name, you have to run some commands or make changes to some configuration files to achieve this.

In this article, we will explain three ways to change your computer name, also known as hostname, through both the graphical user interface and the command line.

We have run the commands and procedures mentioned in this article on Debian 10 and Debian 11.

Changing the hostname on Debian

Method 1: Through the hostname and hosts file

The computer name or hostname can also be changed using the hostname command. To view the current hostname, simply type hostname in the Terminal:

$ hostname

Hostname command

Now to change the computer name, use the following command syntax:

$ sudo hostname new_hostname

Replace new_hostname with the one you want to set for your system.

Set new hostname using hostname command on Debian 10

It is also a temporary way of changing the computer's name. There is also a way to change the computer’s name permanently. For that, we will have to edit two different configuration files named the /etc/hostname and /etc/hosts file.

First, edit the /etc/hostname file by entering the following command in Terminal:

$ sudo nano /etc/hostname

Edit the /etc/hostname file

It will list your computer name. Change the computer’s name to a new name and then press Ctrl+O and Ctrl+X to save and exit the file simultaneously.

Then edit the /etc/hosts file as follows:

$ sudo nano /etc/hosts

You will see the computer’s name listed against the IP: 127.0.1.1

Edit the /etc/hosts file

Change this computer’s name to a new name and then save and exit the file using Ctrl+O and Ctrl+X simultaneously. Now the computer's name you have just set will remain permanent even after you restart your system.

Method 2: Through the hostnamectl command

There is another command hostnamectl using which you can change your computer’s name.

Now just type hostnamectl in your Terminal. It will display detailed information about your system including the computer name or hostname.

$ hostnamectl

Get computer device name details using hostnamectl command

To change the computer’s hostname through the hostnamectl command, use the following syntax:

$ hostnamectl set-hostname "new_hostname"

Replace new_hostname with the one you want to set for your system.

Set new hostname using hostnamectl

Again run the systemctl command, now you will see the new hostname that you have set using the hostnamectl command.

Method 3: Through the GUI

To change the hostname through GUI, click on the downward arrow in the top right corner of your Debian desktop. In the drop-down menu that appears, click on the Settings icon as shown below to launch the Settings utility.

Settings Icon

Alternatively, you can open the Settings utility from the system’s Dash menu. Press the super key on your keyboard, then in the search bar, type settings. When the utility appears, click on it to open it.

Open Settings

Now navigate to the Details tab from the left pane. You will see the device name or hostname in the About view as shown below:

About your computer

By doing so, your hostname will be changed. However, remember that changing the hostname through this method is not permanent. It will revert to the old name when you will restart your system.

Conclusion

In this article, we have learned how we can change the computer name in the Debian OS. Changing the computer name via GUI or hostname command are the temporary methods while editing the /etc/hostname and /etc/hosts configuration files provide a permanent way of changing the computer name.