Skip to main content
To change the hostname on Linux, run sudo hostnamectl set-hostname server1.example.com, then edit /etc/hosts so the new name points to the server (on Ubuntu and Debian, the 127.0.1.1 line). The change applies at once, with no reboot; open a new SSH session to see it in your prompt. Check it with hostnamectl. This works the same on Ubuntu, Debian, AlmaLinux and Rocky Linux.

Before you start

  • Connect with SSH as a sudo user. Root can leave out sudo.
  • A hostname is the name the server uses for itself. Use a full domain name you control (an FQDN), such as server1.example.com, especially if the server sends email. Allowed characters are letters, digits and hyphens, with dots between parts.
  • On a VPS, the name shown in the VPS Panel is separate. See rename your VPS and set a hostname: a hostname set there is only applied when the server is rebuilt, so change it inside the server as below.

How to check the current hostname

Example output (first lines):
hostname prints just the short name; hostname -f prints the full name.

How to change the hostname

1

Set the new hostname

This updates /etc/hostname and the running system. It prints nothing when it succeeds.
2

Update /etc/hosts

Change the 127.0.1.1 line (add it if missing):
Leave the localhost lines as they are. Save with Ctrl+O, Enter, Ctrl+X.
3

Open a new session

Log out and back in. Your prompt now shows alex@server1.
If sudo prints unable to resolve host server1.example.com, the new name is missing from /etc/hosts. The command still runs; fix /etc/hosts as in step 2 and the message disappears.

Why does my hostname change back after a reboot?

Many cloud and VPS images use cloud-init, which can set the hostname again at every boot from the platform’s data. To keep your own hostname, tell cloud-init to leave it alone:
1

Check whether cloud-init is installed

If the command isn’t found, cloud-init isn’t the cause.
2

Set preserve_hostname

Add:
3

Reboot and check

After reconnecting, run hostnamectl again.

How to set reverse DNS to match

For a mail server, the IP address should resolve back to the same name (reverse DNS, or PTR record), and the name should resolve to the IP (an A record at your DNS provider). On a VPS, set the PTR record in the panel: configure reverse DNS. For a dedicated server, open a ticket. To create the A record, see point a domain to your server.

How to check it worked

Expected output on Ubuntu/Debian:

Frequently asked questions

Do I need to reboot after changing the hostname?

No. hostnamectl set-hostname applies at once. Some services read the hostname only at start-up, such as a mail server (Postfix myhostname) or monitoring agents; restart those.

What is the difference between static, transient and pretty hostnames?

The static hostname is stored in /etc/hostname and used at boot. The transient hostname is the one the kernel uses right now, normally the same. The pretty hostname is a free-text label such as “Web server 1” (sudo hostnamectl set-hostname --pretty "Web server 1").

Can I just edit /etc/hostname?

Yes, but the running system keeps the old name until you reboot or run sudo hostname server1.example.com. hostnamectl does both in one step.

Does changing the hostname break anything?

Usually not. Check anything that has the old name in its config, for example /etc/postfix/main.cf, SSL certificates issued for the old name, or monitoring tools.

VPS and dedicated server guides

Every server guide in one place.

Rename your VPS

The panel name and hostname.

Configure reverse DNS

Match your IP to the hostname.

Check Linux version and system info

hostnamectl and more.

Set the time zone

Another first-day setting.

Checked against the official hostnamectl manual and the cloud-init documentation on 4 November 2026. Our test container doesn’t run systemd, so hostnamectl was not run there. Commands can differ on other distributions or releases.

Need Extra Help?

If you encounter any issues, our support team is ready to assist:

Save on Your Hosting

Ready to upgrade your hosting? Use code KB20 at checkout for 20% off your first order!

Last Updated: November 2026