How to change hostname on Debian 9 Linux
2 min read
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 Linux system.
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:
As you can see in the image above, the current hostname is set to
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 recommended to use a fully-qualified domain name (
) as the system hostname.
The following steps outline how to change the hostname in Debian 9.
Change the hostname using
In Debian 9, to change the system hostname and related settings you can use the
For example, to change the system hostname to
host.example.com, you can use the following command:
sudo hostnamectl set-hostname host.example.com
hostnamectlcommand does not produce output. On success, 0 is returned, a non-zero failure code otherwise.
/etc/hostsfile 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
Static hostname: host.example.com
Icon name: computer-vm
Machine ID: 2cc2688b8138434a81dd7b3133e66b2e
Boot ID: e378a0971e9e415cb70e7e953a2362bc
Operating System: Debian GNU/Linux 9 (stretch)
Kernel: Linux 4.9.0-7-amd64
and you should see your new server name printed on the console.
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.