A new VPS often comes with a hostname like vps-123456 or the name you entered at order time. Giving the server a meaningful name makes prompts, logs, monitoring and email headers clearer. On systemd distributions, hostnamectl changes it permanently in one command.
Check the current hostname
hostnamectl
hostname # short name
hostname -f # fully qualified domain name (FQDN), resolved through /etc/hosts or DNS
Set a new hostname
sudo hostnamectl set-hostname web1.example.com
This writes /etc/hostname and applies the change immediately, with no reboot needed. Your current shell prompt updates when you open a new session (exec bash).
Short name or FQDN?
Opinions differ, and both work. Common practice:
- Set
/etc/hostnameto the short name (web1) or the FQDN (web1.example.com) consistently across your servers. - Make sure
hostname -freturns the FQDN through/etc/hosts.
Mail servers care most. Postfix uses the FQDN in its greeting, and it should match the server’s PTR (reverse DNS) record. See email VPS and PTR records.
Hostname rules: letters, digits and hyphens; each label up to 63 characters; no underscores; not starting or ending with a hyphen.
Update /etc/hosts
After a rename, sudo may print:
sudo: unable to resolve host web1: Name or service not known
The new name isn’t in /etc/hosts. Add it:
127.0.0.1 localhost
127.0.1.1 web1.example.com web1
# or with the server's public IP
203.0.113.10 web1.example.com web1
Debian and Ubuntu traditionally use 127.0.1.1 for the hostname. Put the FQDN first, then the short name. Check with:
hostname -f # web1.example.com
getent hosts web1
Pretty and static hostnames
hostnamectl knows three kinds:
- static — the one in
/etc/hostname, used at boot; - transient — the running kernel hostname, which DHCP can change;
- pretty — a free-form label with spaces and capitals, like
Web Server 1 (Tokyo).
sudo hostnamectl set-hostname "Web Server 1" --pretty
sudo hostnamectl set-hostname web1 --static
Cloud-init resets my hostname
Many VPS images use cloud-init, which may set the hostname from the provider’s metadata at every boot and undo your change. If the name reverts after a reboot:
grep -E 'preserve_hostname|manage_etc_hosts' /etc/cloud/cloud.cfg
Set the following in /etc/cloud/cloud.cfg, or in a file in /etc/cloud/cloud.cfg.d/:
preserve_hostname: true
If manage_etc_hosts: true is set, cloud-init also regenerates /etc/hosts from a template. Either turn it off, or edit the template (/etc/cloud/templates/hosts.debian.tmpl and similar). See cloud-init.
Without systemd
echo web1 | sudo tee /etc/hostname
sudo hostname web1 # apply now
Things to update after renaming
- Prompt: open a new shell.
- Mail:
/etc/mailnameand Postfixmyhostnameif you send email. - Monitoring and logs: agents may report under the old name until restarted (Netdata, Prometheus node labels, rsyslog).
- Self-signed certificates and SSH host keys: they don’t include the hostname in a way that breaks, but configs that match on the name (Ansible inventories,
~/.ssh/config) may need updating. - Databases: MySQL replication or Galera configs sometimes reference hostnames.
- The panel: the hostname you see in your provider’s control panel is a label and doesn’t change with hostnamectl.
DNS: make the name resolvable
For other machines to reach web1.example.com, create a DNS A record pointing to the server’s IP. The hostname on the server doesn’t create DNS entries by itself. See DNS records explained.
Quick reference
| Task | Command |
|---|---|
| Show | hostnamectl |
| Set | sudo hostnamectl set-hostname name |
| FQDN check | hostname -f |
| Hosts entry | 127.0.1.1 fqdn short in /etc/hosts |
| Stop cloud-init reverting | preserve_hostname: true |
Related: first 10 minutes on a new VPS and timezone.