How to Change Hostname on Debian 13 (Step-by-Step)

Change hostname on Debian 13 with hostnamectl, update 127.0.1.1 in /etc/hosts, handle cloud-init, and verify the new name without rebooting.
A blue-lit Debian server connected to a glowing network node and abstract identity badge in a midnight-blue data center.

To change hostname on Debian 13, set the persistent name with hostnamectl and update Debian’s local name mapping. The process takes about five minutes, applies without a reboot, and leaves you with checks that confirm both the operating system and resolver use the new name.

Prerequisites

  • A VPS running Debian GNU/Linux 13 (Trixie).
  • An SSH connection as root, or a normal user with working sudo privileges.
  • A hostname made from lowercase letters, numbers, hyphens, and dots, such as web02 or web02.example.com.
  • The server’s IP address and access to your DNS provider if the name must resolve from other machines.
  • A VPSLake Linux VPS if you need a Debian 13 server with root access.

Step 1: Record the current hostname

Check the current values before editing anything so you can tell a saved hostname from one supplied temporarily by DHCP or the VPS image.

cat /etc/os-release
hostnamectl status --static
hostnamectl status --transient
hostnamectl status --pretty
hostname
cat /etc/hostname

The release file should identify Debian 13 (Trixie). Empty static, transient, or pretty values are possible on a new VPS; they are not errors by themselves. A populated result may look like this:

PRETTY_NAME="Debian GNU/Linux 13 (trixie)"
old-web.example.com
old-web.example.com

old-web.example.com
old-web.example.com

The static value is the durable name. A transient value can come from network configuration, while a pretty hostname is an optional label intended for people rather than DNS.

Step 2: Set the Debian 13 static hostname

Use hostnamectl to update the running kernel and /etc/hostname together, which avoids leaving the active and next-boot names out of sync.

sudo hostnamectl set-hostname NEW_HOSTNAME
hostnamectl status --static
cat /etc/hostname

Replace NEW_HOSTNAME with a short name such as web02 or an FQDN such as web02.example.com. Do not include spaces, underscores, or a final dot. Debian 13’s hostnamectl manual describes static, transient, and pretty hostnames; static names are limited to DNS-style characters and a maximum of 64 characters.

The command normally has no success message. Both checks should print the new value:

web02.example.com
web02.example.com

If you also want a readable description, set it separately. This does not replace the static machine name:

sudo hostnamectl set-hostname "Production web server" --pretty
hostnamectl status --pretty

Expected output:

Production web server

Step 3: Update Debian’s local name mapping

Setting the hostname does not automatically make hostname --fqdn resolve to it. Debian commonly keeps the machine name on 127.0.1.1, leaving 127.0.0.1 for localhost.

Open the hosts file:

sudo nano /etc/hosts

Keep the existing localhost and IPv6 entries. Replace the old machine-name line, or add one line like this for an FQDN:

127.0.0.1 localhost
127.0.1.1 NEW_HOSTNAME NEW_SHORT_NAME

For web02.example.com, NEW_SHORT_NAME is web02. If you chose only a short hostname, use 127.0.1.1 web02 instead. Keep a single 127.0.1.1 line for the machine; duplicate old and new entries can make lookups appear inconsistent.

Save the file, then query the resolver:

getent hosts NEW_HOSTNAME
hostname --fqdn
hostname -s

For the example FQDN, successful output is similar to:

127.0.1.1       web02.example.com web02
web02.example.com
web02

This hosts-file entry changes name resolution on this VPS only. If other computers need the FQDN, create an A record for IPv4 or an AAAA record for IPv6 with your DNS provider. A local hostname change does not create public DNS or reverse DNS.

Step 4: Check whether cloud-init manages the name

Some VPS images run cloud-init at boot and can write a provider-supplied hostname after you change the local files. Check first; installing cloud-init is not necessary for this task.

if command -v cloud-init >/dev/null 2>&1; then
  cloud-init status --long
else
  echo "cloud-init is not installed"
fi

If cloud-init is absent, continue to Step 5. If it is installed and the name stays correct after a reboot, leave it alone. If it restores the old name, tell cloud-init to preserve the locally configured value:

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 file-writing command should echo:

preserve_hostname: true

The cloud-init module reference documents preserve_hostname. If the VPS provider continues applying a hostname from instance metadata, update that provisioning value as well; otherwise it can overwrite the local setting on a later boot.

Step 5: Refresh your SSH session

The new name is active immediately, but an existing shell prompt may have been created with the old value. Confirm the active and persistent names, then open a second SSH session before closing the first one.

hostname
hostnamectl status --static
cat /etc/hostname

All three lines should contain the chosen hostname:

web02.example.com
web02.example.com
web02.example.com

Most applications do not need a restart just because the hostname changed. Restart an individual service only when it reads the hostname once at startup:

sudo systemctl restart YOUR_SERVICE

Replace YOUR_SERVICE with the real unit name for that application. A reboot is optional and is not required to apply this change.

Verify the new hostname

Run this final group after editing /etc/hosts. It checks the saved name, short form, fully qualified form, and the address returned by Debian’s name-service configuration.

hostnamectl status --static
hostname -s
hostname --fqdn
getent hosts NEW_HOSTNAME

For web02.example.com, the first three lines should be the FQDN, web02, and the FQDN again. The last line should return the 127.0.1.1 mapping or the expected DNS address. You can also confirm that no stale local hostname remains:

grep -nE '127\\.0\\.1\\.1|OLD_HOSTNAME|NEW_HOSTNAME' /etc/hosts

Replace both uppercase placeholders before running the search. If the values agree, Debian 13 is using the new hostname locally.

Troubleshooting

hostnamectl: invalid hostname

The static value contains a space, underscore, trailing dot, or another unsupported character. Use lowercase letters, numbers, hyphens, and dots in NEW_HOSTNAME; put spaces only in the separate --pretty value. Also check that each FQDN label starts and ends with a letter or number.

hostname --fqdn still shows the old name

The persistent hostname changed, but /etc/hosts still has the previous 127.0.1.1 mapping or has two competing machine-name lines. Replace the stale entry, keep one mapping for the new name, and run getent hosts NEW_HOSTNAME again. If only remote computers see the old name, update public DNS and allow cached answers to expire.

The old hostname returns after reboot

Cloud-init, DHCP metadata, or the VPS image is applying a name during boot. Check cloud-init status --long, add the 99-preserve-hostname.cfg file from Step 4 if cloud-init is responsible, and update the provider’s hostname setting if it continues to overwrite the server.

Failed to set hostname: Access denied

The current account does not have permission to change the system name. Run the command with sudo, verify that sudo -v succeeds, or reconnect as root; changing permissions on /etc/hostname is not a fix.

Next steps

  • Add the public A or AAAA record and test it from another network if the hostname is used for a website, mail, TLS, or monitoring.
  • Request a matching PTR record through the system that manages your VPS IP when reverse DNS matters.
  • Update SSH inventory, backups, monitoring, certificates, and configuration-management records that still use the old name.

FAQ

Do I need to reboot Debian 13 after changing the hostname?

No. hostnamectl changes the active name and writes /etc/hostname immediately. Reconnect over SSH to refresh the prompt, and restart only an application that caches the hostname during startup.

Should I use a short hostname or an FQDN?

A short name such as web02 is sufficient for a standalone VPS or private network. Choose an FQDN when TLS, mail, monitoring, or multi-server tools need a unique identity, and make sure it resolves through /etc/hosts or public DNS.

Does changing the hostname update DNS or reverse DNS?

No. The command changes Debian’s local system identity 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 for the machine name?

Debian commonly keeps 127.0.0.1 for localhost and uses 127.0.1.1 for the local machine name. That lets programs resolve the server’s own name without depending on public DNS or binding it to a changing network address.

Samrat Ghosh
Written by

Samrat Ghosh

Founder & Infrastructure Engineer

Samrat Ghosh is the founder of VPSLake, where he builds and runs the remote desktop and VPS hosting infrastructure the company is built on. He writes hands-on guides about RDP, Windows Server, VPS management and secure remote access - the practical documentation he wishes had…

Previous Article

Chrome Remote Desktop Alternative: 6 Options for 2026

Next Article

Windows 365 vs a Windows RDP VPS: Cost and Control Compared

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨