How to Change Hostname on Debian Linux

How to Change Hostname on Debian Linux

This tutorial explains how to change the hostname on Debian 10 Buster without restarting the system.

The hostname is set at the time when the Debian operating system is installed or if you are spinning up a virtual machine it is dynamically assigned to the instance at startup.

Prerequisites

To be able to change the system hostname you need to be logged in as root or user with sudo privileges.

Display the Current Hostname

In Debian 10 and all other Linux distributions using systemd, you can change and display the hostname of a given system. with the hostnamectl tool.

To view the current system hostname, type hostnamectl without any option:

~] hostnamectl 
   Static hostname: proxy-kj-nginx
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 8d63a028e37747ff949e6f56d76fb283
           Boot ID: 31fe9d1ed6b04e0099022f96e646d5cf
    Virtualization: vmware
  Operating System: Debian GNU/Linux 10 (buster)
            Kernel: Linux 4.19.0-5-amd64
      Architecture: x86-64

Change the System Hostname

A hostname is a label that identifies a machine on the network. You shouldn’t set the same hostname on two different machines on a same network. It is recommended to use a fully-qualified domain name (FQDN) as the system hostname.

There are two steps involved when changing the system hostname on Debian 10 . First, set the new hostname using the hostnamectl set-hostname command followed by the desired hostname and then update the /etc/hosts file with the new hostname.

For example, to change the system hostname to server1.example.com, you would do the following steps:

  1. First set the new hostname by running:
~] sudo hostnamectl set-hostname server1.example.com
  1. Second, open the /etc/hosts file and replace the old hostname with the new one.
/etc/hosts
127.0.0.1   localhost
127.0.0.1   server1.example.com server1 

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

Verify the Change

To verify that the hostname was successfully changed, once again use the hostnamectl command:

~] hostnamectl
   Static hostname: server1
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 8d63a028e37747ff949e6f56d76fb283
           Boot ID: 31fe9d1ed6b04e0099022f96e646d5cf
    Virtualization: vmware
  Operating System: Debian GNU/Linux 10 (buster)
            Kernel: Linux 4.19.0-5-amd64
      Architecture: x86-64

The new system hostname will be printed on the command line.

SUBSCRIBE FOR NEW ARTICLES

@
comments powered by Disqus