Home About Me

Changing a Debian Hostname: Temporary and Persistent Routes

When you need to change the hostname on a Debian machine, there are two common paths: a quick temporary change with hostname, and a persistent change that survives reboots. The second can be done by editing the relevant files directly or, on systemd-based Debian, by using hostnamectl.

Using hostname for a temporary change

hostname both displays and sets the current hostname.

# View current hostname
➜  ~ hostname
tkvm
# Change to new name box
➜  ~ hostname box
➜  ~ hostname
box

This change is temporary. After a reboot, the old name returns. To make it persistent, edit /etc/hostname and /etc/hosts as well.

# Change the value in /etc/hostname to the new name
➜  ~ cat /etc/hostname
box
# Change the value after 127.0.1.1 in /etc/hosts to the new name
➜  ~ cat /etc/hosts
127.0.0.1       localhost
127.0.1.1       box

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

After these edits, the hostname remains the new name across reboots.

Using hostnamectl on systemd-based Debian

Debian systems that use systemd include hostnamectl for hostname management.

# Use hostnamectl to view current host information
➜  ~ hostnamectl
 Static hostname: bbq
       Icon name: computer-vm
         Chassis: vm 🖴
      Machine ID: 5e07831af04e410dbce86f486718a532
         Boot ID: a7b7b947133943c994e7b1bbbee3b95a
  Virtualization: kvm
Operating System: Debian GNU/Linux 12 (bookworm)
          Kernel: Linux 6.1.0-32-amd64
    Architecture: x86-64
 Hardware Vendor: QEMU
  Hardware Model: Standard PC _i440FX + PIIX, 1996_
Firmware Version: 1.16.2-debian-1.16.2-1

# Set the new hostname
➜  ~ hostnamectl set-hostname tkvm
# Check the result
➜  ~ hostnamectl
 Static hostname: tkvm
       Icon name: computer-vm
         Chassis: vm 🖴
      Machine ID: 5e07831af04e410dbce86f486718a532
         Boot ID: a7b7b947133943c994e7b1bbbee3b95a
  Virtualization: kvm
Operating System: Debian GNU/Linux 12 (bookworm)
          Kernel: Linux 6.1.0-32-amd64
    Architecture: x86-64
 Hardware Vendor: QEMU
  Hardware Model: Standard PC _i440FX + PIIX, 1996_
Firmware Version: 1.16.2-debian-1.16.2-1

Using hostnamectl simplifies the process and avoids manually editing /etc/hostname and /etc/hosts.