How to Set the Hostname on a Linux VPS
Give your server a name that survives reboots, shows up correctly in logs, and stops the "sudo: unable to resolve host" warning the moment you change it.
Every Linux server ships with a generic hostname like localhost or vps-12345. That's fine for a single box, but the moment you have two servers your logs become ambiguous, your shell prompt loses meaning, and automation tools can't tell them apart. This guide sets a proper hostname — both the short name and a fully qualified version — in a way that survives reboots cleanly.
Three hostnames, not one
Modern Linux actually tracks three hostnames at once. Knowing which is which saves a lot of confusion later.
- Static — the name in
/etc/hostname. Set once, persists across reboots. This is what you usually want to change. - Pretty — a human-readable label like "Yossi's Web Server". Mostly cosmetic, shown in some GUIs.
- Transient — runtime-only value, can be set by DHCP or container orchestrators. Resets on reboot.
FQDN vs short name
A short hostname is just web01. A fully qualified domain name (FQDN) is web01.example.com. Some software (Postfix, certbot, distributed databases) requires an FQDN. The convention: set hostname to the short form and put the FQDN in /etc/hosts alongside it.
1See what's currently set
# All three hostnames at once hostnamectl # Or read individually hostname # short name hostname -f # FQDN (resolved via /etc/hosts and DNS) cat /etc/hostname # what survives reboot
2Change the static hostname
hostnamectl is the one-stop tool on systemd-based distros. It updates /etc/hostname, the running kernel's hostname, and the systemd machine info file in one shot.
# Replace web01 with your chosen short name sudo hostnamectl set-hostname web01 # Optional: set a friendly "pretty" label sudo hostnamectl set-hostname "Production Web Server" --pretty # Verify hostnamectl
Your shell prompt won't update mid-session
The \h in your bash prompt is read once at login. Log out and back in (or run exec bash) and you'll see the new name in the prompt.
3Wire the hostname into /etc/hosts
This is the step people forget — and then sudo complains "unable to resolve host web01" every time. Many programs (including sudo) resolve their own hostname before doing anything else, and they look at /etc/hosts first. The entry needs to match the new name exactly.
# Edit the file sudo nano /etc/hosts # Ensure these two lines exist (the second is the important one) 127.0.0.1 localhost 127.0.1.1 web01.example.com web01
The 127.0.1.1 line is a Debian/Ubuntu convention for the machine's own hostname. RHEL family typically uses a real interface IP there, but 127.0.1.1 works on both.
4Confirm DNS resolution works for the FQDN
# Should print the FQDN hostname -f # Should resolve to a real IP (the /etc/hosts entry, or a real DNS A record) getent hosts web01.example.com # Confirm sudo no longer complains sudo -n echo "hostname is happy"
What about cloud-init? It may overwrite your change
On VPS images that ship with cloud-init (most Ubuntu, AlmaLinux, and Rocky images), the first boot can re-set the hostname from VPS metadata. If your hostname change is ignored after a reboot, this is the cause.
# Open the cloud-init config sudo nano /etc/cloud/cloud.cfg # Set these two values preserve_hostname: true manage_etc_hosts: false
After that change, hostnamectl set-hostname is the only thing that controls the hostname — cloud-init won't touch it.
Setting a real FQDN (the proper way)
If you own a domain and want the server reachable as web01.example.com from anywhere, you need DNS, not just /etc/hosts. /etc/hosts only affects the local machine; the wider internet doesn't see it.
- In your DNS provider, create an A record: name
web01, value — your VPS's public IP. - Wait a few minutes for propagation. Test from another machine:
dig web01.example.com. - Once DNS resolves, leave
/etc/hostsas the local fallback — some services prefer it. - If the server sends mail, also add a PTR (reverse DNS) record via your VPS provider's control panel. We provide a self-serve PTR editor in every client area.
Frequently asked questions
What's the difference between hostname and hostnamectl?
hostname is the legacy command and only changes the running kernel's hostname — it's lost on reboot. hostnamectl updates /etc/hostname, the running kernel, and the systemd machine metadata in one step. Use hostnamectl on any modern Linux.My hostname keeps reverting to something like vps-12345 after reboot. Why?
preserve_hostname: true or configure your network manager not to accept the DHCP-provided hostname.Do I need to restart the server after changing the hostname?
hostnamectl set-hostname applies immediately to the running kernel. Existing sessions and apps that already cached the old hostname keep using it — restart them individually, or just open a new shell.Can the hostname contain dots, dashes, or uppercase?
Can VPS-SERVER HOST set the hostname for me at provisioning?
Naming a fleet of servers?
Order multiple VPS instances at once and set the hostname per server right in checkout — consistent naming, zero post-deploy work.
Deploy a VPS Open a Ticket