Change hostname on Ubuntu 24.04 with the built-in hostnamectl command, then make local name resolution agree with it. The change takes about five minutes, applies immediately, and does not normally require a reboot.
Prerequisites
- An Ubuntu Server 24.04 LTS VPS.
- An SSH connection as
root, or a regular account with workingsudoprivileges. - A terminal editor such as
nano; no additional RAM or disk space is needed for a hostname change. - A short name such as
app01, or an FQDN such asapp01.example.commade from lowercase letters, numbers, hyphens, and dots. - Access to your DNS provider if other computers must resolve the FQDN. Local resolution works without a public domain.
- A VPSLake Linux VPS if you need an Ubuntu 24.04 server for this procedure.
Step 1: Inspect the current hostname
Why: recording the static, transient, and fully qualified values helps you spot a name supplied by cloud provisioning or DNS.
cat /etc/os-release
hostnamectl status --static
hostnamectl status --transient
hostnamectl status --pretty
hostname
hostname --fqdn
The release file should identify Ubuntu 24.04. A blank static, transient, or pretty value is possible on a newly provisioned VPS and is not itself an error. A populated server might return output like this:
PRETTY_NAME="Ubuntu 24.04 LTS"
old-app.example.com
old-app.example.com
old-app.example.com
old-app.example.com
The static hostname is the durable operating-system setting. A transient hostname can come from the network, while the pretty hostname is only a human-readable label.
Step 2: Set the persistent Ubuntu hostname
Why: hostnamectl changes the running system and the persistent hostname configuration through systemd in one operation.
sudo hostnamectl set-hostname NEW_HOSTNAME
hostnamectl status --static
cat /etc/hostname
Replace NEW_HOSTNAME with the name you selected, such as app01 or app01.example.com. Do not use spaces, underscores, or a trailing dot. The hostnamectl documentation describes the static, transient, and pretty hostname types.
The set command normally prints nothing. The two checks should both show your new value:
app01.example.com
app01.example.com
An optional pretty hostname can make administrative screens easier to read, but it does not replace the machine name used by services:
sudo hostnamectl set-hostname "Production application server" --pretty
hostnamectl status --pretty
Expected output:
Production application server
Step 3: Align /etc/hosts with the new name
Why: the hosts file gives Ubuntu a local answer for its own name before public DNS is available or has finished propagating.
Open the file:
sudo nano /etc/hosts
Keep the existing localhost and IPv6 entries. Replace the old machine-name line, or add one 127.0.1.1 entry. For an FQDN, use this pattern:
127.0.0.1 localhost
127.0.1.1 NEW_HOSTNAME NEW_SHORT_NAME
For app01.example.com, replace NEW_HOSTNAME with app01.example.com and NEW_SHORT_NAME with app01. If you chose only app01, use 127.0.1.1 app01 instead. Keep one current machine-name entry rather than leaving both the old and new lines in place.
Save the file, exit nano, and query Ubuntu’s resolver:
hostname --fqdn
hostname -s
getent hosts NEW_HOSTNAME
For the FQDN example, successful output resembles:
app01.example.com
app01
127.0.1.1 app01.example.com app01
This edit affects name lookups on the VPS only. Create an A record for IPv4 or an AAAA record for IPv6 with your DNS provider when the hostname must resolve from the internet. Forward DNS and reverse DNS are separate; request a matching PTR record through the system that manages your VPS address, such as the VPS control panel.
Step 4: Prevent cloud-init from restoring the old name
Why: Ubuntu cloud images may apply a provider-supplied hostname during boot, so check for cloud-init before deciding whether a persistence override is necessary.
if command -v cloud-init >/dev/null 2>&1; then
cloud-init status --long
else
echo "cloud-init is not installed"
fi
If the hostname stays correct after a reboot, no cloud-init change is needed. If cloud-init is installed and brings back the previous name, create a drop-in rather than editing the main configuration file:
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
The tee command should echo:
preserve_hostname: true
The cloud-init module reference documents preserve_hostname. If the provider still applies a name from instance metadata, update that provisioning value as well; otherwise it can overwrite the local setting on a later boot.
Step 5: Refresh your SSH session
Why: the kernel uses the new name immediately, but an existing shell prompt or application may have cached the old value when it started.
Confirm the active name and the file used at boot:
hostname
hostnamectl status --static
cat /etc/hostname
All three lines should contain the new hostname:
app01.example.com
app01.example.com
app01.example.com
Open a second SSH session to get a fresh prompt before closing the first one. Most services do not need a restart for this change; restart only an application that reads the hostname once at startup:
sudo systemctl restart YOUR_SERVICE
Replace YOUR_SERVICE with the real systemd unit name. Do not restart networking or reboot just to activate the hostname.
Verify the new hostname
Run this final check after updating /etc/hosts. It tests the saved value, the short form, the FQDN result, and the address returned by the local name-service configuration.
hostnamectl status --static
hostname -s
hostname --fqdn
getent hosts NEW_HOSTNAME
For app01.example.com, the first three lines should be the FQDN, app01, and the FQDN again. The final line should return the 127.0.1.1 mapping or the expected DNS address. If you used only a short hostname, hostname --fqdn may return that short value when no domain is configured; that is normal.
Troubleshooting
hostnamectl: invalid hostname
The static name contains a space, underscore, trailing dot, or unsupported punctuation. Use lowercase letters, numbers, hyphens, and dots in NEW_HOSTNAME; put spaces only in the optional pretty hostname. Also check that each FQDN label starts and ends with a letter or number.
hostname --fqdn still shows the old name
The system hostname changed, but /etc/hosts still has the previous 127.0.1.1 mapping or contains duplicate machine-name lines. Replace the stale entry, keep one line for the new hostname, and run the verification commands again. If only another computer sees the old name, update public DNS and allow its cached result to expire.
The old hostname returns after reboot
Cloud-init, DHCP metadata, or the VPS image 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 change the provider’s provisioning hostname if it continues to overwrite the server.
Failed to set hostname: Access denied
The current account lacks the privilege required 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
- Add and test the public A or AAAA record from another network if the name is used for a website, TLS, mail, or monitoring.
- Configure a matching PTR record in the VPS control panel when reverse DNS matters.
- Update SSH inventory, backups, monitoring, certificates, and configuration-management records that still identify the server by its old name.
FAQ
Do I need to reboot Ubuntu 24.04 after changing the hostname?
No. hostnamectl updates the active name and the persistent setting immediately. Start a new SSH session to refresh the prompt, and restart only an application that caches the name during startup.
Is an FQDN required for a VPS hostname?
No. A short name such as app01 is enough for a standalone server or private network. Choose an FQDN when TLS, mail, monitoring, or multi-server tooling needs a distinct DNS identity, and configure the corresponding local or public DNS record.
Will this change update forward DNS or reverse DNS?
No. It changes Ubuntu’s local system identity. Public forward DNS is managed by your DNS provider, while reverse DNS is a separate PTR setting managed through the VPS provider or address owner.
Why is 127.0.1.1 used in /etc/hosts?
Ubuntu commonly keeps 127.0.0.1 for localhost and uses 127.0.1.1 for the local machine name. That lets programs resolve the VPS’s own hostname without tying the local entry to an address that may change.