Every VPS arrives with a hostname the provider picked for you, and it is usually something like vps-a1b2c3 or ubuntu-2204. It works, but it tells you nothing, and it can quietly cause real problems: mail servers distrust it, log lines become hard to read, and reverse DNS checks fail. Learning how to rename VPS hostname settings properly takes about ten minutes, and this guide walks through the whole job on Ubuntu 24.04: pick a good name, change it with hostnamectl, set the FQDN in /etc/hosts, and match reverse DNS so everything agrees.
What a Hostname and an FQDN Actually Are
A hostname is the name your server calls itself. It shows up in your shell prompt, in log files, and in the greeting a mail server sends when it connects to another server. The short hostname is a single label, like server01. The fully qualified domain name, or FQDN, is that label plus a domain you control: server01.example.com.
The difference matters because the two names are used in different places. The short name is mostly local, for your own convenience. The FQDN is what the outside world sees in mail headers, TLS certificates, and PTR lookups. A server can have a short hostname that looks fine locally while its FQDN is broken or missing, and that mismatch is where most hostname trouble starts. Fixing it means getting three things to agree: the hostname the system reports, the FQDN in /etc/hosts, and the reverse DNS record your provider publishes.
Choose a Hostname Before You Change Anything
Pick a subdomain you control, like server01.example.com or vps1.example.com. Never set the hostname to your bare domain (example.com) if that domain hosts websites or mail. The server will start treating itself as the domain, and local mail routing gets confused in ways that are painful to debug later.
Stick to lowercase letters, numbers, and hyphens. Underscores are not valid in hostnames, and some software rejects them outright. Dots are only for separating the labels of the FQDN. If you run several servers, use a scheme you can read at a glance: web-01, db-01, mail-01, each with the same domain suffix. Future you, reading a log at 2 a.m., will be grateful.
If you have not pointed the domain at the server yet, sort that out first. The FQDN has to resolve in public DNS for the checks later in this guide to pass, so make sure the A record exists before you go further. If you need a refresher, our guide on how to point a new domain to your VPS and set up free HTTPS covers the DNS side step by step.
How to Rename VPS Hostname on Ubuntu 24.04
Check the current name first, so you know what you are replacing:
hostnamectl
hostname -f
On a fresh server, hostname -f often prints only the short name, or errors out entirely. That is the broken state this guide fixes.
Set the new name with one command, replacing the example with your own FQDN:
sudo hostnamectl set-hostname server01.example.com
hostnamectl writes /etc/hostname and updates the running kernel hostname in one go, so there is no need to edit files by hand on Ubuntu 24.04. The change takes effect immediately for new processes, but your current shell prompt still shows the old name until you log out and back in. That is cosmetic; the system already knows its new name.
One catch: some providers use cloud-init to set the hostname at every boot. If you need to change hostname Ubuntu keeps overwriting at boot, cloud-init is usually the culprit. Open /etc/cloud/cloud.cfg and set preserve_hostname: true, then run the hostnamectl command again. Without that flag, your new name will quietly revert on the next reboot.
Set the FQDN in /etc/hosts
hostnamectl sets the static hostname, but it does not touch /etc/hosts, and that file is what local lookups actually use. Open it with your editor of choice:
sudo nano /etc/hosts
Add a line with your server’s public IP, the FQDN first, and the short hostname second:
203.0.113.10 server01.example.com server01
Replace 203.0.113.10 with your real public IP. The order matters: the FQDN goes first, the short name second. These two lines are all it takes to set VPS FQDN resolution locally, and skipping this step is the most common reason hostname -f keeps returning the short name after a rename.
Verify the result right away:
hostname
hostname -f
hostname -s
hostname should print server01, hostname -f should print server01.example.com, and hostname -s should print the short name again. If hostname -f still prints only the short name, re-check the /etc/hosts line for typos, especially a missing FQDN or a wrong IP.
Match Reverse DNS to Your New FQDN
Reverse DNS is the PTR record that maps your server’s IP back to a hostname. Mail servers check it on every connection: they look up the PTR for your IP and expect it to match the FQDN your server announces. Getting your VPS hostname reverse DNS record to match the FQDN is the step most people skip, and it is the one that causes mail to land in spam or get rejected outright.
You cannot set a PTR record in your own DNS zone. It is set by whoever owns the IP range, which means your VPS provider. Log in to your provider’s panel (Hetzner, DigitalOcean, Vultr, Contabo, and most others all have a reverse DNS field in the server settings), enter the exact FQDN you just configured, and save it. The change usually takes effect within minutes, but allow up to a few hours before testing.
For the full background on how PTR records work and how to check them from the command line, see our guide to reverse DNS (PTR) on a VPS. If mail delivery is the reason you are doing this, also read how to set up email sending from your VPS without landing in spam, since a matching PTR is only one piece of mail deliverability.
Verify the New Hostname and FQDN
Run these checks from the server and from your own machine. Everything in the “should show” column has to match, or something is still out of sync:
| Command | Should show |
|---|---|
hostname |
server01 |
hostname -f |
server01.example.com |
hostnamectl status |
Static hostname: server01.example.com |
getent hosts $(hostname -f) |
Your public IP |
dig -x 203.0.113.10 +short |
server01.example.com. |
The last check queries public DNS, so it reflects the PTR record your provider publishes. If dig -x returns nothing or the old name, the PTR change has not propagated yet, or the provider panel did not save it. Wait an hour and test again before assuming something is broken. And if you are running services that cache the old hostname, restart them: Postfix, for example, reads the hostname at startup, so a systemctl restart postfix is needed before it starts announcing the new name.
Problems You Might Hit After Renaming
Most renames go smoothly, but a few issues come up often enough to be worth knowing about:
- sudo suddenly takes ten seconds. sudo tries to resolve the hostname, and if
/etc/hostsdoes not list the new name, each lookup times out. Add the FQDN line as described above and the delay disappears. - Mail still uses the old name. Postfix and other mail software read the hostname once at startup. Restart the service after renaming, and check
myhostnamein the Postfix config if it was set explicitly. - The old name is still in your prompt. That is just your current shell session. Log out and back in and the prompt updates.
- The name reverts after a reboot. Cloud-init is resetting it. Set
preserve_hostname: truein/etc/cloud/cloud.cfg, as covered earlier. - Monitoring alerts fire. Tools like Uptime Kuma or Zabbix may reference the old hostname in checks and alerts. Update the hostname in your monitoring setup so alerts keep matching the right server.
If you are still working through a fresh server, the first-15-minutes hardening checklist for Ubuntu covers SSH keys, the firewall, and automatic updates, so the rename becomes one part of a proper setup instead of a one-off fix.
A clean hostname setup is a small job with an outsized payoff: logs you can read, mail that passes basic checks, and a server whose name matches its DNS records in both directions. Set the hostname with hostnamectl, add the FQDN to /etc/hosts, match the PTR record at your provider, and run the five verification commands above. If they all agree, you are done, and the name will survive the next reboot too.