How to Change Hostname on Debian 12

Displaying the Current Hostname

To find out your current system name by running the following command.

hostnamectl

Output:

root@server:~# hostnamectl
 Static hostname: server
       Icon name: computer-vm
         Chassis: vm 🖴
      Machine ID: 79b6eac931d64e9085fadca9e55a908c
         Boot ID: 86564aa1bbfb4455be62a1f348fae14c
  Virtualization: kvm
Operating System: Debian GNU/Linux 12 (bookworm)
          Kernel: Linux 6.1.0-9-amd64
    Architecture: x86-64
 Hardware Vendor: Red Hat
  Hardware Model: KVM
Firmware Version: 1.16.0-3.module_el8.7.0+3346+68867adb
root@server:~#

Changing the System Hostname

Now that you know the current hostname, you can change the hostname by using the following command and Optionally you can also set the pretty hostname as follows.

hostnamectl set-hostname crowncloud.net

hostnamectl set-hostname "crowncloud production server" --pretty

And also /etc/hosts file should be edited to successfully change Debian server name.

vi /etc/hosts

Output:

127.0.0.1       localhost
127.0.1.1       crowncloud.net  server

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

Then replace the old name with the new, then save the file and exit.

Debian servers might need to install cloud-init. If it is installed you will also need to edit the cloud.cfg file.

apt install cloud-init

Once the cloud-init is installed need to find "preserve_hostname: false" line in the follwoing configuration file "/etc/cloud/cloud.cfg" and change it to true.

vi /etc/cloud/cloud.cfg

preserve_hostname: true

Save the file and exit,

Verify the change

To verify the hostname has been fully changed, enter the "hostnamectl" command:

hostnamectl

Output:

root@server:~# hostnamectl
 Static hostname: crowncloud.net
 Pretty hostname: crowncloud production server
       Icon name: computer-vm
         Chassis: vm 🖴
      Machine ID: 79b6eac931d64e9085fadca9e55a908c
         Boot ID: 86564aa1bbfb4455be62a1f348fae14c
  Virtualization: kvm
Operating System: Debian GNU/Linux 12 (bookworm)
          Kernel: Linux 6.1.0-9-amd64
    Architecture: x86-64
 Hardware Vendor: Red Hat
  Hardware Model: KVM
Firmware Version: 1.16.0-3.module_el8.7.0+3346+68867adb
root@server:~#