How to Change Hostname on Debian 11

Displaying the Current Hostname

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

hostnamectl

Output:

[email protected]:~# hostnamectl
   Static hostname: Debian11
   Pretty hostname: crowncloud
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 39941301335d45acb6747d0b884bd638
           Boot ID: 1806121a0c0443978ee6d51db83b308e
    Virtualization: kvm
  Operating System: Debian GNU/Linux 11 (bullseye)
            Kernel: Linux 5.10.0-8-amd64
      Architecture: x86-64

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

# 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:

[email protected]:~# hostnamectl
   Static hostname: crowncloud.net
   Pretty hostname: crowncloud production server
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 39941301335d45acb6747d0b884bd638
           Boot ID: 1806121a0c0443978ee6d51db83b308e
    Virtualization: kvm
  Operating System: Debian GNU/Linux 11 (bullseye)
            Kernel: Linux 5.10.0-8-amd64
      Architecture: x86-64