Change hostname on Ubuntu 26.04 with hostnamectl, a matching /etc/hosts entry, and a few focused checks. You can normally finish in about five minutes without rebooting, then reconnect over SSH to see the new name in your prompt.
Prerequisites
- Ubuntu Server 26.04 LTS (Resolute) running on the VPS.
- An SSH session as
root, or a regular user with workingsudoprivileges. - A short hostname such as
web03, or an FQDN such asweb03.example.com; use lowercase letters, numbers, hyphens, and dots only. - The server’s address if you want a local
/etc/hostsmapping, plus access to your DNS provider if other systems must resolve the FQDN. - A VPSLake Linux VPS with root access if you need an Ubuntu 26.04 server for this procedure.
Step 1: Record the current hostname
Check the active, persistent, and resolver-facing values first so you can spot a stale name after the edit.
cat /etc/os-release
hostnamectl --static
hostnamectl --transient
hostnamectl --pretty
hostname
cat /etc/hostname
The release file should identify Ubuntu 26.04 LTS (Resolute). Empty transient or pretty values are normal; the static value is the name saved for future boots.
PRETTY_NAME="Ubuntu 26.04 LTS (Resolute Raccoon)"
old-web.example.com
old-web.example.com
old-web.example.com
old-web.example.com
The exact release string can vary with the image’s update level. The important part is that the OS is Ubuntu 26.04 and you have noted the old hostname before changing it.
Step 2: Set the new static hostname
hostnamectl changes the running hostname and writes the persistent system value through systemd, so you do not need to edit /etc/hostname by hand.
sudo hostnamectl set-hostname NEW_HOSTNAME
hostnamectl --static
Replace NEW_HOSTNAME with a short name such as web03 or an FQDN such as web03.example.com. Do not use spaces, underscores, a trailing dot, or a name longer than 64 characters. The command normally prints nothing; the second command should show your new value:
web03.example.com
Ubuntu’s hostnamectl distinguishes static, transient, and pretty names. The static name is the operational identity used at boot; a pretty name is optional and can contain spaces, but it is not a substitute for the static hostname. See the Ubuntu 26.04 hostnamectl manual for these hostname types and their limits.
Step 3: Make the new name resolve locally
Changing the system name does not automatically make hostname --fqdn resolve, so update Ubuntu’s local hosts record as well.
Open the file:
sudo nano /etc/hosts
Keep the existing localhost and IPv6 records. Replace the old machine-name line or add one 127.0.1.1 line. For an FQDN, use:
127.0.0.1 localhost
127.0.1.1 NEW_HOSTNAME NEW_SHORT_NAME
For web03.example.com, replace NEW_SHORT_NAME with web03. If you chose only web03, use 127.0.1.1 web03 instead. Keep one machine-name entry rather than leaving both the old and new names on separate 127.0.1.1 lines.
Save with Ctrl+O, press Enter, then exit with Ctrl+X. Test the resolver:
getent hosts NEW_HOSTNAME
hostname --fqdn
hostname --short
For the example FQDN, successful output resembles:
127.0.1.1 web03.example.com web03
web03.example.com
web03
Ubuntu’s networking documentation uses 127.0.1.1 for a local machine name and explains that /etc/hosts entries normally take precedence over DNS. This mapping affects the VPS only; create an A or AAAA record separately when other machines must resolve the name.
Step 4: Prevent a cloud-init reset when necessary
Some VPS images use cloud-init to apply a provider-supplied hostname during boot, so check for it only if the name later changes back.
if command -v cloud-init >/dev/null 2>&1; then
cloud-init status --long
else
echo "cloud-init is not installed"
fi
If cloud-init is absent, continue to Step 5. If it is installed and the hostname remains correct after a reboot, no change is needed. If it restores the old value, create a small drop-in that preserves the hostname you set locally:
sudo install -d -m 0755 /etc/cloud/cloud.cfg.d
printf '%s\n' 'preserve_hostname: true' | sudo tee /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg
sudo hostnamectl set-hostname NEW_HOSTNAME
Replace NEW_HOSTNAME with the same value from Step 2. The tee command should echo:
preserve_hostname: true
The cloud-init hostname documentation documents this setting. If the provider still supplies a name from instance metadata, change that provisioning value too; otherwise it can compete with the local setting at the next boot.
Step 5: Refresh the SSH session
The kernel uses the new name immediately, but an existing shell prompt or application that cached the old value may not redraw it.
hostname
hostnamectl --static
cat /etc/hostname
All three lines should contain your chosen name:
web03.example.com
web03.example.com
web03.example.com
Open a second SSH session to get a freshly generated prompt before closing the first one. Most services do not need a restart for this change; restart an individual application only if it reads the hostname once at startup:
sudo systemctl restart YOUR_SERVICE
Replace YOUR_SERVICE with the real unit name for that application. Do not restart networking just to apply a hostname, and do not reboot unless another maintenance task requires it.
Verify the new hostname
Run this final set to confirm the saved name, short form, FQDN lookup, and local address mapping:
hostnamectl --static
hostname --short
hostname --fqdn
getent hosts NEW_HOSTNAME
For web03.example.com, the first three lines should be the FQDN, web03, and the FQDN again. The final line should return the 127.0.1.1 entry or the expected DNS address. If these results agree, Ubuntu 26.04 is using the new hostname locally.
Troubleshooting
hostnamectl: invalid hostname
The static value contains a character Ubuntu rejects, usually a space, underscore, or trailing dot. Use lowercase letters, digits, hyphens, and dots in NEW_HOSTNAME; put spaces only in an optional pretty hostname. Check each FQDN label as well as the 64-character overall limit.
hostname --fqdn still shows the old name
The static hostname changed, but /etc/hosts still has the previous 127.0.1.1 mapping or contains two competing machine-name lines. Edit the file, keep one line for the new name, and run the Step 3 checks again. If only remote computers see the old result, update public DNS rather than changing the local file again.
The old hostname returns after reboot
Cloud-init, DHCP, or image provisioning is applying a name during startup. Check cloud-init status --long, add the 99-preserve-hostname.cfg drop-in from Step 4 when cloud-init is responsible, and update the provider’s hostname setting if metadata continues to overwrite it.
Failed to set hostname: Access denied
The current account lacks permission to change the system name. Run the command with sudo, confirm that sudo -v succeeds, or reconnect as root; changing permissions on /etc/hostname is not a safe fix.
Next steps
- Create the public A or AAAA record and test it from another network if the name is used for a website, TLS, mail, or monitoring.
- Request a matching PTR record through the system that manages your VPS IP when reverse DNS matters.
- Update SSH inventory, backups, monitoring, certificates, and configuration management entries that still use the old hostname.
FAQ
Do I need to reboot Ubuntu 26.04 after changing the hostname?
No. hostnamectl applies the static name immediately and stores it for the next boot. Reconnect over SSH to refresh the prompt, and restart only an application that caches the hostname during startup.
Can I use a short hostname without a domain?
Yes. A name such as web03 is suitable for a standalone VPS or private network. Use an FQDN when TLS, mail, monitoring, or multi-server tools need a unique identity, and make sure it resolves through /etc/hosts or public DNS.
Does changing the hostname update DNS or reverse DNS?
No. This procedure changes Ubuntu’s local system identity. Public forward DNS and reverse DNS are separate records managed by your DNS provider or the system that controls the VPS address.
What is the difference between static and pretty hostnames?
The static hostname is the machine name saved in /etc/hostname and used by the operating system. A pretty hostname is an optional human-readable label that may contain spaces; it does not replace the static name used by services and boot configuration.