How to permanently change the hostname on Debian Linux 12, 11 or 10

11.07.2023 Published by:

Permanently change the hostname on Debian 12, 11 or 10: hostnamectl, the correct /etc/hosts entry, verification and common reasons the old name returns.

Technical content updated: 15 October 2026. This guide applies to Debian running systemd and requires administrator privileges. Replace the example name app01 with your own server name.

How do you change the hostname permanently?

On a standard Debian installation, run sudo hostnamectl set-hostname app01, then update the relevant local entry in /etc/hosts. Running hostname app01 alone changes the running system’s name but is not a complete persistent configuration.

1. Check the current configuration

After connecting to the correct server, inspect its current name and configuration files:

hostnamectl

cat /etc/hostname

cat /etc/hosts

Record the previous name and keep copies of both files before making changes. If mail, monitoring or applications depend on the hostname, check those dependencies first.

2. Set the new server name

Run:

sudo hostnamectl set-hostname app01

This sets, among other things, the static hostname stored in the system configuration. Omit sudo if you are already root. For a simple technical hostname, use ASCII letters, digits and hyphens, without spaces.

3. Update the relevant /etc/hosts entry

Open /etc/hosts in an editor, for example with sudo nano /etc/hosts. Replace the old hostname in its existing entry while preserving the rest of the configuration. On a typical installation without a permanent IP address, the relevant lines may look like this:

127.0.0.1 localhost
127.0.1.1 app01

Do not replace the entire file with this example. For a server with a permanent IP address or fully qualified domain name, adapt its existing mapping to the actual configuration. Keep the localhost and IPv6 entries.

4. Verify the change

Check the static hostname, the running system’s hostname and local name resolution:

hostnamectl --static

hostname

getent hosts app01

An old name in an existing shell prompt does not necessarily mean the change failed; open a new session. Setting the hostname usually does not require a system reboot, but services that depend on it should be checked separately.

Why does the old name return, or hostnamectl fail?

If the name returns after a reboot, check server management tools such as cloud-init or the hosting provider’s panel. Do not disable them automatically; establish which configuration should control the hostname.

In a container, chroot or system without a running systemd instance, hostnamectl may be unavailable. Configure the hostname through the container or platform instead. The message sudo: unable to resolve host can indicate an inconsistency between the hostname and local resolution in /etc/hosts.

Changing the hostname does not automatically update public DNS, mail records or TLS certificates. These are separate configuration tasks.

Need help administering a Linux server?

For a live business server, we can review dependencies and plan the configuration as part of our Linux server administration service. Our experience includes work for Warsaw’s Municipal Roads Authority.

For server maintenance, see also our guide to finding and deleting old files on Linux.

Documentation: hostnamectl – Debian Manpages and Debian Reference: local hostname resolution.

About the author

Leszek Iwaniec is the owner of Projectic. He works on online store implementations, custom applications and technical support. On the blog, he describes problems and solutions involving PrestaShop, Laravel and system integrations, drawing on the team’s project experience.

Back