Change hostname on Debian 11 with hostnamectl, a Debian-style /etc/hosts entry, and a few resolver checks. You can finish in about five minutes without rebooting, then reconnect to SSH with the new server name.
Prerequisites
- A VPS running Debian GNU/Linux 11 (Bullseye).
- Root access or a normal user with
sudoprivileges over SSH. - A new hostname using lowercase letters, numbers, hyphens, and dots; choose a short name such as
web01or an FQDN such asweb01.example.com. - The server’s IP address and access to your DNS provider if other systems must resolve the name.
- A VPSLake Linux VPS if you need a Debian server for this procedure.
Step 1: Check the current hostname
Record the existing values first so you can distinguish Debian’s persistent hostname from the name returned by local name resolution.
cat /etc/os-release
hostnamectl status --static
hostnamectl status --pretty
hostname
hostname --fqdn
The release information should identify Debian 11 or Bullseye. A new server may have no pretty hostname; that blank result is normal.
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
old-web.example.com
old-web.example.com
old-web.example.com
The static value is the name stored for the next boot. The pretty value is only a human-readable label, while hostname --fqdn depends on the resolver configuration in /etc/hosts or DNS.
Step 2: Set the persistent hostname
Use hostnamectl so the running system and /etc/hostname are updated together instead of editing the file and leaving the current kernel name unchanged.
sudo hostnamectl set-hostname NEW_HOSTNAME
hostnamectl status --static
cat /etc/hostname
Replace NEW_HOSTNAME with web01 or web01.example.com. Do not include the brackets, spaces, underscores, or a trailing dot. A static hostname should be a DNS-safe name; use the pretty-hostname option below if you want spaces in a description.
Expected output for an FQDN is:
web01.example.com
web01.example.com
If you want a descriptive label in addition to the operational hostname, set it separately:
sudo hostnamectl set-hostname "Production web server" --pretty
hostnamectl status --pretty
That label is optional and is not used as a DNS record. Debian 11’s hostnamectl documentation describes the separate static, transient, and pretty hostname types.
Step 3: Make the name resolve locally
Debian commonly maps the machine’s own name to 127.0.1.1. Adding the new name there prevents local tools from falling back to localhost or an old hostname before public DNS is available.
Open the hosts file:
sudo nano /etc/hosts
Keep the existing localhost and IPv6 lines. Replace the old machine-name line, or add the following line if no 127.0.1.1 entry exists:
127.0.0.1 localhost
127.0.1.1 NEW_HOSTNAME NEW_SHORT_NAME
Replace NEW_HOSTNAME with the full name, such as web01.example.com, and NEW_SHORT_NAME with its short form, web01. If you chose only web01, use 127.0.1.1 web01 instead. Do not create two competing 127.0.1.1 lines for the same server.
Save the file, then test both the resolver and the kernel’s hostname:
getent hosts NEW_HOSTNAME
hostname --fqdn
hostname -s
Successful output resembles:
127.0.1.1 web01.example.com web01
web01.example.com
web01
This local entry affects only this VPS. To make the FQDN work from other computers, create an A record for IPv4 or an AAAA record for IPv6 in your DNS provider. If you use an FQDN for mail, TLS, or monitoring, also arrange matching reverse DNS through the system that manages the server IP.
Step 4: Prevent cloud-init from replacing the name
Some Debian VPS images run cloud-init during boot and apply the provider’s hostname again. Check for it before changing configuration; you do not need to install cloud-init just to rename the server.
if command -v cloud-init >/dev/null 2>&1; then
cloud-init status --long
else
echo "cloud-init is not installed"
fi
If the name stays correct after a reboot, leave the configuration alone. If cloud-init is installed and the hostname returns after reboot, preserve your manual value with a small drop-in:
sudo install -d -m 0755 /etc/cloud/cloud.cfg.d
printf '%s\n' 'preserve_hostname: true' | sudo tee /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg
sudo hostnamectl set-hostname NEW_HOSTNAME
The tee command should print the line it saved:
preserve_hostname: true
Replace NEW_HOSTNAME again with the same static name. The cloud-init module reference documents how preserve_hostname prevents its hostname module from changing the system name. If your provider deliberately manages the hostname from metadata, update that setting as well so two systems do not keep competing values.
Step 5: Refresh your SSH session
The hostname changes immediately, but an existing shell prompt may have been built with the old value. Verify the persistent file and current kernel value, then reconnect so your prompt and login-time scripts start with the new name.
hostnamectl status --static
cat /etc/hostname
hostname
Expected output is the same name on all three lines:
web01.example.com
web01.example.com
web01.example.com
Open a second SSH session before closing the first one. A reboot is not part of this procedure. Restart an individual application only if that application reads the hostname once at startup; changing the name alone does not require restarting networking.
Verify the new hostname
Run this final set of checks after editing /etc/hosts. It confirms the persistent setting, the short and fully qualified forms, and the address returned by Debian’s resolver:
hostnamectl status --static
hostname -s
hostname --fqdn
getent hosts NEW_HOSTNAME
For web01.example.com, the result should look like this:
web01.example.com
web01
web01.example.com
127.0.1.1 web01.example.com web01
If those values agree, Debian 11 is using the new hostname locally. Test the public DNS record separately from another network if the name must be reachable outside the VPS.
Troubleshooting
hostnamectl: invalid hostname
The static name contains a character Debian rejects, commonly a space, underscore, or trailing dot. Use lowercase letters, numbers, hyphens, and dots for NEW_HOSTNAME; put spaces only in a separate --pretty value. Also check that no label begins or ends with a hyphen.
hostname --fqdn still returns the old name
The persistent hostname changed, but /etc/hosts still has the old 127.0.1.1 mapping or has duplicate entries. Inspect the relevant lines and keep one mapping for the new name:
grep -nE '127\.0\.1\.1|OLD_HOSTNAME|NEW_HOSTNAME' /etc/hosts
getent hosts NEW_HOSTNAME
Replace OLD_HOSTNAME and NEW_HOSTNAME with real values before interpreting the result. If /etc/hosts is correct but remote machines see the old name, update public DNS and allow cached answers to expire.
The hostname changes back after reboot
Cloud-init, DHCP metadata, or the VPS image is applying a name at boot. Check cloud-init status --long, add the 99-preserve-hostname.cfg file from Step 4 when appropriate, and update the provider’s provisioning hostname if it continues to overwrite the local setting.
Failed to set hostname: Access denied
Changing the hostname requires root privileges. Run the command with sudo, confirm that your account is allowed to use it with sudo -v, or reconnect as root. If sudo is not available, an administrator must grant the account the required privilege.
Next steps
- Add or update the public A/AAAA record, then query it from a separate machine.
- Set a matching PTR record if mail delivery, monitoring, or an external security service checks reverse DNS.
- Update SSH configuration, monitoring inventory, backups, and TLS certificates that still refer to the previous name.
FAQ
Do I need to reboot Debian 11 after changing the hostname?
No. hostnamectl applies the new name immediately and writes the persistent hostname file. Reconnect your SSH session to refresh the prompt, and restart only an application that caches the hostname at startup.
Is an FQDN required for a Debian VPS?
No. A short name such as web01 works for a standalone machine or private network. An FQDN is preferable when the server participates in TLS, mail, monitoring, or a multi-server environment, but it must also resolve through /etc/hosts or DNS.
Does hostnamectl change public DNS or reverse DNS?
No. It changes the Debian operating system’s hostname only. Public forward DNS and reverse DNS are separate records managed by your DNS provider or VPS control panel.
Why does Debian use 127.0.1.1 in /etc/hosts?
Debian commonly reserves 127.0.1.1 for the local machine name while 127.0.0.1 remains the localhost address. This lets local programs resolve the server’s own name without requiring a public DNS lookup or binding the name to a changing network address.