Migrate to RunxBuild and earn up to $50 in hosting credit on your first deposit.

Calculate your savings
unxBuild

Change the Hostname on a Linux Machine Permanently

Sean

Platform Writer

Jun 30, 2026
4 min read

A permanent hostname change on Linux is one command with hostnamectl on systemd systems, or two file edits plus a reboot on legacy systems. The hostnamectl approach is the right one for any modern distribution (Ubuntu 16+, Debian 8+, CentOS 7+, Fedora, Arch).

Change the Hostname on a Linux Machine Permanently

Table of contents

The hostnamectl approach (permanent)

The right command for systemd systems:

sudo hostnamectl set-hostname new-hostname.example.com

This sets all three hostname types (static, pretty, transient) and writes the change to /etc/hostname and /etc/machine-info. The change is immediate and survives a reboot.

The team that wants to verify: hostnamectl (no arguments) shows the current hostname, cat /etc/hostname shows the static hostname.

The /etc/hosts edit (often forgotten)

The hostnamectl approach does not edit /etc/hosts automatically. The team that does not edit /etc/hosts gets hostname -f returning the old value:

  1. Edit /etc/hosts with sudo nano /etc/hosts.
  2. Find the line that maps 127.0.1.1 to the old hostname.
  3. Change it to the new hostname.
  4. Save and exit.

This is the most-skipped step. The team that does it once and never has to debug a hostname mismatch is the team that learns it early.

The legacy approach (without systemd)

For the team on a non-systemd distribution (CentOS 6, older Ubuntu, Alpine):

  1. sudo nano /etc/hostname and change the hostname.
  2. sudo nano /etc/hosts and update the 127.0.1.1 line.
  3. sudo reboot.

The reboot is required because the kernel-level hostname is set at boot; the file edit takes effect on the next boot.

What changes vs what doesn’t

What changes:

  • hostname and hostname -f return the new value.
  • New log entries use the new hostname.
  • TLS certificates with the hostname in the CN/SAN need to be regenerated.
  • Some daemons (Postfix, nginx) need a reload.

What doesn’t change:

  • The IP address.
  • The MAC address.
  • The SSH host keys (tied to the host, not the hostname).
  • The machine-id (a UUID assigned at install).

What usually goes wrong

The four pitfalls:

  • Forgot /etc/hosts. The team that runs hostnamectl set-hostname but does not edit /etc/hosts gets hostname returning the new value and hostname -f returning the old.
  • Forgot to reload daemons. Postfix keeps using the old hostname in logs until it is reloaded.
  • Picked an invalid hostname. A hostname with underscores or a leading hyphen is rejected by some tools.
  • Picked a too-long hostname. Labels over 64 characters are rejected. Total over 255 characters is rejected.

The DNS impact

Changing the hostname has DNS implications:

  • Forward DNS. The team’s DNS records (A, AAAA, CNAME) may need updating if the hostname is used in a public DNS zone.
  • Reverse DNS (PTR). The PTR record mapping IP to hostname is typically set by the IP owner (cloud provider, ISP). The team that changes the hostname on a server with a public IP needs to update the PTR record too.
  • Internal DNS. Most internal DNS is dynamic (mDNS, Avahi, Active Directory). The team that uses static internal DNS may need to update records manually.

The team that changes the hostname on a server with a public IP and forgets the PTR record has email delivery issues (many mail servers check PTR).

The TLS certificate regeneration

If the hostname is in a TLS certificate, the certificate needs regeneration:

  • Self-signed (mkcert, openssl). Regenerate with the new hostname.
  • Let’s Encrypt. Re-run certbot with the new hostname; the certificate is reissued automatically.
  • CA-issued (DigiCert, etc.). Generate a new CSR with the new hostname, submit to the CA, install the new certificate.

The team that changes the hostname on a TLS-enabled service and forgets to regenerate the certificate has a service that warns every visitor about hostname mismatch.

FAQ

How do I make the hostname change permanent?

hostnamectl set-hostname writes to /etc/hostname and /etc/machine-info. The change survives a reboot.

Do I need to reboot?

No, with hostnamectl. Yes, with the legacy approach.

Can I use a domain name as the hostname?

Yes. The team that uses web-prod-01.example.com as the hostname has a fully-qualified hostname, which is the right choice for a public-facing server.

Why does hostname -f return the old value?

Because /etc/hosts was not updated. The team that edits both /etc/hostname and /etc/hosts has the right behavior.

Does the hostname change require updating /etc/hosts?

Yes, with hostnamectl set-hostname alone. The team that skips /etc/hosts has hostname -f returning the old value.

Can I use hostnamectl on Alpine Linux?

No, Alpine uses OpenRC, not systemd. Edit /etc/hostname and /etc/hosts manually, then sudo hostname $(cat /etc/hostname).

How long does hostnamectl take?

Milliseconds. The change is immediate; the running services see the new name on their next lookup.

If you are sizing the infrastructure for the kind of project this post covers, the RunxBuild hosting calculator is the right place to model the line items. The compute, the memory, the storage, the bandwidth, the database - each one is a separate number, and the team’s mental model for the platform is the sum of those numbers. The RunxBuild dashboard is where the team sees the actual usage in one place.

Useful related references:

#hostname#linux#hostnamectl#systemd#permanent