How to Change Hostname on the Debian 10 Linux

Choose a different version or distribution

Introduction

Before we begin talking about how to change Hostname on Debian 10 Linux. Let’s briefly understand - What is a Hostname?

The hostname is defined as a label that is used to identify the particular system on a network. It is mainly set at the time when Debian operating system installation is there or if a user is spinning up a virtual machine then, it is dynamically assigned to instances at the startup.

In this tutorial, you will change the Hostname on Debian 10 Linux. We will also address some of the FAQs related to the Hostname.

Prerequisites

1) Make sure to log in as root or the user with sudo privileges.

Step 1 – Display the Current Hostname

1) There is a tool named  hostnamectl, which is used to change or display the hostname in Debian 10 and other Linux distributions.

2) After that, type hostnamectl without any option to view the current system hostname:

hostnamectl

The output will display the current system hostname. Like, here it is host.vegastack.com:

Output
Static hostname: host.vegastack.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 70a3f06298014fd9ac42e5dc1de1034a
           Boot ID: 1dc8b9af89a4426b99cb348f6d483757
    Virtualization: oracle
  Operating System: Debian GNU/Linux 10 (buster)
            Kernel: Linux 4.19.0-5-amd64
      Architecture: x86-64

Step 2 – Changing the System Hostname

The hostname acts as a label to identify a machine on the network. It is not advisable to set the same hostname on two different machines if they are in the same network. Use a fully qualified domain name (FQDN) as the system hostname.

Follow the below two steps to change the Hostname in Debian 10 Linux:

1. Use the hostnamectl set-hostname command to set the new hostname.

sudo hostnamectl set-hostname xyz.example.com

You won't be getting any output after running the above command, 0 is returned if it is a success, otherwise a non-zero error code will be returned.

2. After that, replace the old hostname and add a new one in etc\hosts.

                                    /etc/hosts


127.0.0.1   localhost
127.0.0.1   xyz.example.com xyz 

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

1)  Firstly, you will have to set the new hostname by running the following command:

sudo hostnamectl set-hostname xyz.example.com

The hostnamectl command will not produce output. On success, the 0 is returned. Otherwise, will show a non-zero failure code otherwise.

2) Second, you can open the /etc/hosts file. Proceed to replace the old hostname with a new one:

                                     /etc/hosts
127.0.0.1   localhost
127.0.0.1   xyz.example.com xyz 

# The following lines are desirable for IPv6 capable hosts
::1     localhost ip6-localhost ip6-loopback
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters

Step 3 – Verifying the Change

1) Use the hostnamectl command to verify if the hostname is changed successfully.

hostnamectl

You will get a hostname printed on the command line.

Output

Static hostname: xyz.example.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 70a3f06298014fd9ac42e5dc1de1034a
           Boot ID: 1dc8b9af89a4426b99cb348f6d483757
    Virtualization: oracle
  Operating System: Debian GNU/Linux 10 (buster)
            Kernel: Linux 4.19.0-5-amd64
      Architecture: x86-64

FAQs to Change Hostname on the Debian 10 Linux

What is the valid hostname?

Valid characters for the hostnames are ASCII(7) letters from a to z. The digits from 0 to 9, as well as the hyphen (-). A hostname will not usually start with a hyphen.

What is a hostname in the URL?

The hostname property of the URL interface is basically a USV-String that mainly contains the domain name of the URL.

Do I need to restart my system after changing the hostname?

Yes, it is recommended to reboot your system after changing the hostname to ensure the changes take effect throughout the system. 

Can I revert to the original hostname if I want to undo the changes?

Yes, you can revert to the original hostname by following the same steps mentioned earlier and replacing the new hostname with the old one. Remember to reboot your system for the changes to take effect.

How can I check the current hostname on Debian 10?

You can use the command hostname to check the current hostname.

What permissions are required to change the hostname?

You need to have root or superuser privileges to change the hostname on Debian 10.

How do I open the hostname configuration file?

You can use any text editor like nano or vi to open the /etc/hostname file.

Conclusion

We hope this detailed tutorial helped you to change the Hostname on Debian 10 Linux using commands and editing system files.

If you have any queries or doubts, please leave them in the comment below. We'll be happy to address them.