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.
Display the current hostname for Debian Linux
To view the current hostname, enter the following command:
$ hostnamectl
Static hostname: localhost Icon name: computer-vm Chassis: vm 🖴 Machine ID: 34fdfb323daa4e5c9afa8c53fb17b99e Boot ID: d6663d2eed24496f90aaf7b615a8f67d Virtualization: kvm Operating System: Debian GNU/Linux 12 (bookworm) Kernel: Linux 6.1.0-10-cloud-amd64 Architecture: x86-64 Firmware Version: 2:1.10.2-58953eb7
As you can see in the image above, the current hostname is set to localhost.
Change the Hostname use hostnamectl set-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 (FQDN) as the system hostname.
For example, to change the system hostname to host.example.com in Debian 12, you can use the following command:
$ sudo hostnamectl set-hostname host.example.com
Next, edit the /etc/hosts file, run:
$ vi /etc/hosts
Find all references to oldname and replace with newname except for the following entries:
127.0.0.1 localhost 10.10.10.10 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
Save and close the file.
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: 34fdfb323daa4e5c9afa8c53fb17b99e Boot ID: d6663d2eed24496f90aaf7b615a8f67d Virtualization: kvm Operating System: Debian GNU/Linux 12 (bookworm) Kernel: Linux 6.1.0-10-cloud-amd64 Architecture: x86-64 Firmware Version: 2:1.10.2-58953eb7
you should see your new server name printed on the console.
Changing Debian host names remotely using ssh
From Linux desktop or server, run:
$ hostnamectl set-hostname -H [username]@hostname $ hostnamectl set-hostname {new_name_here} -H [email protected] $ hostnamectl set-hostname host.example.com -H [email protected]
The hostnamectl tool will use SSH to connect to the remote system. One can see help page by passing the –help option to the hostnamectl as follows:
$ hostnamectl --help
Conclusion
On Debian Linux hostnamectl command used to query and change the system hostname and related settings. For more information see the man locally using the command:
$ man hostnamectl