How to Change the Hostname and Domain Name in Debian 12 Bookworm

This article provides a step-by-step guide on how to change the hostname and domain name in Debian 12 Bookworm.

Debian 12 Bookworm is a stable and powerful Linux distribution used for various purposes, from personal computing to enterprise servers. One of the essential tasks when setting up a Debian system is configuring its hostname and domain name properly. This article provides a step-by-step guide on how to change both the hostname and domain name on a Debian 12 system.

Understanding Hostname and Domain Name in Debian

Before diving into the process, it is important to understand what hostnames and domain names are:

  • Hostname: This is the name assigned to a system in a network. It uniquely identifies a machine and helps in networking.
  • Domain Name: This is a hierarchical label that indicates the domain in which the machine resides. It is useful for larger networks where multiple systems are involved.

Checking the Current Hostname and Domain Name

To check your system’s current hostname, run:

hostnamectl

This will display output similar to:

   Static hostname: debian12
         Icon name: computer-laptop
           Chassis: laptop
        Machine ID: xxxxxxxxxxxxxxxxxx
           Boot ID: xxxxxxxxxxxxxxxxxx
  Operating System: Debian GNU/Linux 12 (bookworm)
            Kernel: Linux 6.x.x-amd64
      Architecture: x86-64

To check the domain name, use:

hostname -f

If the system is not set up with a domain, the output may only display the hostname.

Changing the Hostname in Debian 12

1. Using hostnamectl Command

The easiest way to change the hostname in Debian 12 is using the hostnamectl command. Run the following command, replacing new-hostname with the desired hostname:

sudo hostnamectl set-hostname new-hostname

Verify the change by running:

hostnamectl

2. Editing the /etc/hostname File

Another method is manually modifying the hostname configuration file:

sudo nano /etc/hostname

Replace the existing hostname with the new one, save the file (CTRL+X, then Y, then Enter), and apply the change:

sudo systemctl restart systemd-hostnamed

3. Updating /etc/hosts File

To ensure smooth networking, update the /etc/hosts file with the new hostname:

sudo nano /etc/hosts

Modify the line containing the old hostname:

127.0.1.1   old-hostname

Change it to:

127.0.1.1   new-hostname

Save and exit the file.

Changing the Domain Name in Debian 12

1. Modifying /etc/hosts File

To change the domain name, update the /etc/hosts file by editing:

sudo nano /etc/hosts

Modify the entry for your system:

127.0.1.1   new-hostname.new-domain.com new-hostname

Save and close the file.

2. Editing /etc/resolv.conf (If Needed)

To set up a domain name for DNS resolution, edit /etc/resolv.conf:

sudo nano /etc/resolv.conf

Add or modify the following line:

search new-domain.com

Save and exit.

3. Configuring Persistent Domain Name with /etc/network/interfaces

For systems using static network configuration, add the domain name entry to /etc/network/interfaces:

sudo nano /etc/network/interfaces

Add or modify:

dns-domain new-domain.com

Save the file and restart networking:

sudo systemctl restart networking

Verifying the Changes

After making the changes, restart your system to ensure everything is applied properly:

sudo reboot

To check if the changes were successful, run:

hostname
hostname -f
domainname

Conclusion

Changing the hostname and domain name in Debian 12 Bookworm is a straightforward process using hostnamectl, modifying configuration files, and updating the /etc/hosts and /etc/resolv.conf files. Ensuring these changes persist across reboots is crucial for system stability and network functionality. Following these steps will help configure your Debian system correctly within your network.