How to change hostname in Debian 10

How to change hostname in Debian 10

Change hostname in Debian 10

The host name is set during the installation of the operating system, and for virtual machines is dynamically assigned when the instance is created. Because using a host name causes the network to distinguish between the two systems, it is important that each machine in the same network has a different host name. In this tutorial, you will learn how to change the hostname of Debian 10 without restarting the machine.

Prerequisites

Before starting the hostname change in Debian 10, the system needs a non-root user account with sudo privileges.

Check the host name

Execute the following command to check the host name of the system.

hostnamectl

The output looks like this:

   Static hostname: linux4one
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: 2381ec6bc94945e781c843f399507ea2
           Boot ID: 2902f9a594944fe28d8ac90759229ef0
  Operating System: Debian GNU/Linux 9 (stretch)
            Kernel: Linux 4.15.0-43-generic
      Architecture: x86-64

As shown in the output above, the machine hostname is linux4one.

Change hostname using hostnamectl

hostnamectl The command is also used to easily change the host name of the system.

The following is the basic syntax for changing the system’s host name. hostnamectl command:

sudo hostnamectl set-hostname NEW_NAME

Where NEW_NAME is the new host name for the system.

Next, enter the host name of the system linux4one Execute the following command for new_hostname.

sudo hostnamectl set-hostname new_hostname

Now the system hostname is linux4one To new_hostname.

Edit the host name in the / etc / hosts file

Here you can also change the host name for system editing /etc/hosts File.

In session /etc/hosts File in your favorite text editor:

sudo nano /etc/hosts

Next, change the host name of the opened file to the new one.

Change host name using host file Change host name using host file

Check the host name

We are currently using one of the above methods to change the hostname of the system. To confirm the change, run the following command:

hostnamectl

The output looks like this

   Static hostname: new_hostname
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: 2381ec6bc94945e781c843f399507ea2
           Boot ID: 2902f9a594944fe28d8ac90759229ef0
  Operating System: Debian GNU/Linux 9 (stretch)
            Kernel: Linux 4.15.0-43-generic
      Architecture: x86-64

As you can see, the host name linux4one To new_hostname.

Conclusion

You learned how to change the hostname in Debian 10. If you have any questions about this, don’t forget to comment below.

Sidebar