How to Change the Hostname on a Linux Server (Safely, the Right Way)

Changing a Linux server hostname sounds simple—and it is—but doing it properly matters. A hostname touches everything from SSH prompts, monitoring, logging, certificates, internal DNS, and even some application configs. In this guide, I’ll show you the safest and most common methods to change a Linux hostname without breaking anything.

What is a hostname (and why should you care)?

A hostname is the server’s “human-friendly” name on the network—like db01, app-prod-01, or web01.onsys.local. It’s used by:

  • admins (you!) to identify servers quickly
  • monitoring tools and alerting systems
  • logs, audit trails, and backups
  • services that rely on local name resolution (common in enterprise setups)

If it’s wrong or inconsistent, troubleshooting becomes painful fast.


Before you start: choose a good hostname

Use a naming style that’s consistent and meaningful. A few examples:

  • db01, db02 (database nodes)
  • app-prod-01 (environment + role + number)
  • web-au-mel-01 (region + city + role + number)

Keep it:

  • lowercase
  • no spaces
  • avoid underscores (best practice)
  • optionally include a domain later via DNS (FQDN)

Step 1 — Check your current hostname

Run:

hostnamectl
hostname
cat /etc/hostname

This helps you confirm what the OS thinks the hostname is, and what is stored permanently.


Step 2 — Change hostname on modern Linux (recommended)

Most modern Linux distributions use systemd, so the cleanest way is hostnamectl.

Example: change hostname to app01

sudo hostnamectl set-hostname app01

Verify:

hostnamectl

You should now see the “Static hostname” updated.


Step 3 — Update /etc/hosts (this is the step people forget)

Even if you set the hostname correctly, many tools expect the hostname to resolve locally. That’s why updating /etc/hosts is important.

Edit the file:

sudo nano /etc/hosts

Make sure you keep this line:

127.0.0.1    localhost

Then add/update hostname mapping. Common patterns:

Option A (very common on Ubuntu)

127.0.1.1    app01

Option B (map to the server’s real IP)

10.10.10.25  app01 app01.yourdomain.local

This prevents weird issues like:

  • “sudo: unable to resolve host …”
  • slow CLI commands that wait on DNS
  • services failing local resolution checks

Step 4 — Apply changes immediately

Most of the time the change is immediate. But if services still show the old hostname, restart the hostname service:

sudo systemctl restart systemd-hostnamed

If you want maximum certainty (especially on production servers with lots of services):

sudo reboot

After reboot, confirm:

hostnamectl
hostname

If you’re on older Linux (no hostnamectl)

Some older distros don’t include hostnamectl. In that case:

Temporary change (until reboot)

sudo hostname app01

Permanent change

echo "app01" | sudo tee /etc/hostname
sudo nano /etc/hosts
sudo reboot

Don’t forget DNS (if this server is network-facing)

If this hostname is used by others (apps, users, monitoring), make sure your DNS records match:

  • A record (name → IP)
  • PTR record (IP → name), if reverse DNS matters in your environment

If DNS doesn’t match, you can end up with confusion in logs, SSL/TLS issues, or monitoring showing duplicate nodes.


Cloud VM note: cloud-init can overwrite your hostname

On some cloud images (especially Ubuntu cloud builds), cloud-init may reset hostnames on reboot.

If you change the hostname and it “mysteriously” reverts later, check:

cloud-init status

You may need to adjust cloud-init settings so it doesn’t manage hostname automatically.


Quick troubleshooting

Issue: sudo: unable to resolve host <name>

✅ Fix: /etc/hosts doesn’t match the new hostname.

  • Update /etc/hosts and ensure hostname is mapped (Option A or B above).

Issue: Hostname shows different values in different places

✅ Fix: confirm with:

hostnamectl
cat /etc/hostname
grep -v '^#' /etc/hosts

Issue: Apps still show old hostname

✅ Fix: restart services, or reboot if you’re changing this on a heavily loaded server.


Final checklist

  • hostnamectl shows the new hostname
  • /etc/hostname contains the new hostname
  • /etc/hosts includes the new hostname mapping
  • DNS updated (if needed)
  • Monitoring/alerting updated (if it tracks hostname)