Read in 2 minutes

last updated 

How to change hostname on Debian 9

This tutorial is also available for:

centos 7 ubuntu 18.04

This tutorial will guide you through the steps required to change the hostname on your Debian 9 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.

The method described in this tutorial will work without the need of restarting your system.

Prerequisites

Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges.

Display the Current Hostname

To view the current hostname, enter the following command:

hostnamectl

As you can see in the image above, the current hostname is set to host.linuxize.com.

Advertisement

Change the Hostname

A hostname is a label that identifies a machine on a network and should be unique within the network infrastructure. It is recommends to use a fully-qualified domain name (FQDN) as the system hostname.

The following steps outline how to change the hostname in Debian 9.

  1. Change the hostname using hostnamectl.

    In Debian 9, to change the system hostname and related settings you can use the hostnamectl command.

    For example to change the system hostname to host.example.com, you can use the following command:

    sudo hostnamectl set-hostname host.example.com

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

  2. Edit the /etc/hosts file.

    Open the /etc/hosts file and replace your old hostname with the new one.

    /etc/hosts
    127.0.0.1   localhost
    127.0.0.1   host.example.com
    
    # 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: host.example.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 2cc2688b8138434a81dd7b3133e66b2e
           Boot ID: e378a0971e9e415cb70e7e953a2362bc
    Virtualization: qemu
  Operating System: Debian GNU/Linux 9 (stretch)
            Kernel: Linux 4.9.0-7-amd64
      Architecture: x86-64

and you should see your new server name printed on the console.

Conclusion

This tutorial we have shown you how to easily change your Debian server hostname without restarting the machine.

Feel free to leave a comment if you have any questions.