As a Linux user, you may find yourself needing to change the hostname of your Debian 12 server for various reasons. Whether you want to give your server a more personalized name or you’re simply rebranding your business, changing the hostname is a straightforward process. In this article, we will guide you through the steps to change the hostname on Debian 12.

Finding the Current Hostname

Before changing the hostname, it’s essential to know the current system name. To find out your current hostname, open the terminal and run the following command:

hostnamectl

The output will display various details about your system, including the current hostname. For example:

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

Changing the System Hostname

Once you have determined the current hostname, you can proceed to change it. The process is relatively simple and can be done using the hostnamectl command. Additionally, you have the option to set a pretty hostname for aesthetic purposes. Here’s how you can change the hostname on Debian 12:

  1. Open the terminal and run the following command to change the hostname to “example.net”:
hostnamectl set-hostname example.net
  1. If you prefer a pretty hostname, you can use the following command to set it as “example production server”:
hostnamectl set-hostname "example production server" --pretty

Editing the /etc/hosts File

To successfully change the Debian server name, you also need to edit the /etc/hosts file. This file contains mappings between IP addresses and hostnames for your system. Follow these steps to edit the file:

  1. Open the terminal and run the following command to open the /etc/hosts file:
vi /etc/hosts
  1. Locate the line that starts with 127.0.1.1 and replace the old hostname with the new one. For example, change:
127.0.1.1 example.net server

to:

127.0.1.1 example.net server
  1. Save the file and exit the text editor.

Handling Debian Servers with cloud-init

In some cases, Debian servers may require the installation of cloud-init to change the hostname. If your server doesn’t have cloud-init installed, you can install it using the following command:

apt install cloud-init

After installing cloud-init, you need to modify the cloud.cfg file. Here’s how you can do it:

  1. Open the terminal and run the following command to open the cloud.cfg file:
vi /etc/cloud/cloud.cfg
  1. Locate the line that says preserve_hostname: false and change it to preserve_hostname: true.
preserve_hostname: true
  1. Save the file and exit the text editor.

Verifying the Hostname Change

To ensure that the hostname has been successfully changed, you can verify it by running the hostnamectl command again:

hostnamectl

The output should display the updated hostname, both the static and pretty versions. For example:

Static hostname: example.net
Pretty hostname: example 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

Congratulations! You have successfully changed the hostname on your Debian 12 server.

Additional Information

If you’re looking for reliable and efficient cloud hosting solutions, consider Shape.host. With their Linux SSD VPS, you can enjoy scalable and secure hosting services tailored to your needs.

In conclusion, changing the hostname on Debian 12 is a simple process that can be done using the hostnamectl command and editing the /etc/hosts file. By following the steps outlined in this article, you can personalize your server’s hostname and enhance your overall server management experience.