How to Change Hostname on Ubuntu 22.04 (Step-by-Step)

Change hostname on Ubuntu 22.04 with hostnamectl, update /etc/hosts, handle cloud-init resets, and verify the new server name without rebooting.
A polished midnight-blue server with orange edge lighting connected to a glowing blue identity node.

Change hostname on Ubuntu 22.04 with hostnamectl, a matching local hosts entry, and a short verification pass. You can finish in about five minutes without rebooting, then open a new SSH session to see the updated name.

Prerequisites

  • Ubuntu Server 22.04 LTS (Jammy Jellyfish) running on your VPS.
  • An SSH connection as root, or a normal account with working sudo privileges.
  • A new short hostname such as web01, or an FQDN such as web01.example.com; use lowercase letters, numbers, hyphens, and dots.
  • Access to your domain’s DNS records and the server’s public IP if the name must resolve outside this VPS.
  • No special RAM or disk allocation is required for a hostname change; a VPSLake Linux VPS is suitable if you need an Ubuntu 22.04 server.

Step 1: Record the current hostname

Check the active and saved values first so you can distinguish an old local setting from a name supplied by the image or network.

cat /etc/os-release
hostnamectl status --static
hostnamectl status --pretty
hostnamectl status --transient
hostname
cat /etc/hostname

The release file should identify Ubuntu 22.04 (Jammy). Empty static or pretty values are possible on a freshly provisioned VPS and are not errors.

PRETTY_NAME="Ubuntu 22.04.5 LTS"
old-web.example.com

old-web.example.com
old-web.example.com

The static hostname is the durable operating-system value. A transient name can be supplied by DHCP or provisioning, while a pretty hostname is only a human-readable label.

Step 2: Set the persistent Ubuntu hostname

Use hostnamectl so Ubuntu changes the running kernel name and /etc/hostname together.

sudo hostnamectl set-hostname NEW_HOSTNAME
hostnamectl status --static
cat /etc/hostname

Replace NEW_HOSTNAME with web01 or web01.example.com. Do not include spaces, underscores, or a final dot. The command normally prints no success message, so the two following checks are the useful confirmation:

web01.example.com
web01.example.com

Ubuntu’s hostnamectl manual treats the static, transient, and pretty values separately. If you want an optional description for people, set it independently:

sudo hostnamectl set-hostname "NEW_PRETTY_NAME" --pretty
hostnamectl status --pretty

Replace NEW_PRETTY_NAME with a label such as Production web server. Spaces are valid in this label, but it is not the hostname used for DNS or service identity.

Step 3: Update Ubuntu’s local hosts entry

Changing the system value does not automatically make the new name resolve locally, so update /etc/hosts before testing the FQDN.

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 in this form:

127.0.0.1 localhost
127.0.1.1 NEW_HOSTNAME NEW_SHORT_NAME

For web01.example.com, replace NEW_SHORT_NAME with web01. If you chose only web01, use 127.0.1.1 web01 instead. Keep one machine-name entry; leaving both the old and new lines can produce inconsistent lookups. Save with Ctrl+O, press Enter, and exit with Ctrl+X in nano.

Test the local resolver:

getent hosts NEW_HOSTNAME
hostname --fqdn
hostname --short

For an FQDN, successful output resembles:

127.0.1.1       web01.example.com web01
web01.example.com
web01

This mapping applies only to the VPS. Create an A record for IPv4 or an AAAA record for IPv6 if other systems need to resolve the name through public DNS.

Step 4: Check cloud-init before making its setting permanent

Some Ubuntu VPS images run cloud-init during boot and can restore a provider-supplied hostname; do not install it just for this task.

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 remains correct after a reboot, no cloud-init change is needed. If cloud-init is installed and restores the previous name, add a drop-in that preserves the local value:

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 the setting it saved:

preserve_hostname: true

Replace NEW_HOSTNAME with the same value used in Step 2. The cloud-init module documentation describes preserve_hostname; if provider metadata still wins at boot, update the hostname in that provisioning configuration too.

Step 5: Refresh SSH and applications that cache the name

The active hostname changes immediately, but an existing shell prompt or application may have read the old value only once at startup.

hostname
hostnamectl status --static
cat /etc/hostname

All three lines should contain the new name:

web01.example.com
web01.example.com
web01.example.com

Open a second SSH session to refresh the prompt before closing the first one. Restart an individual application only if it caches the hostname at startup:

sudo systemctl restart YOUR_SERVICE

Replace YOUR_SERVICE with a real unit name. Changing the hostname does not require restarting networking or rebooting the VPS.

Verify the new hostname

Run this final group after editing /etc/hosts to check the persistent value, short form, FQDN, and resolver result:

hostnamectl status --static
hostname --short
hostname --fqdn
getent hosts NEW_HOSTNAME

For web01.example.com, the first three lines should be web01.example.com, web01, and web01.example.com. The last line should return the 127.0.1.1 entry or the expected DNS address. When those values agree, Ubuntu 22.04 is using the new hostname locally.

Troubleshooting

hostnamectl: invalid hostname

The static value contains a space, underscore, trailing dot, or another unsupported character. Use lowercase letters, numbers, hyphens, and dots in NEW_HOSTNAME; put spaces only in NEW_PRETTY_NAME. Check that each FQDN label starts and ends with a letter or number.

sudo: unable to resolve host appears after the change

The new static hostname and /etc/hosts disagree, so sudo cannot resolve the local machine name. Inspect the machine-name lines and leave one matching entry:

grep -nE '127\.0\.1\.1|OLD_HOSTNAME|NEW_HOSTNAME' /etc/hosts
getent hosts NEW_HOSTNAME

Replace both uppercase placeholders with real values. Update the 127.0.1.1 line, then run the checks again. If only remote computers see an old answer, the issue is public DNS caching rather than Ubuntu’s local hostname.

The hostname changes back after a 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 update the provider’s provisioning hostname if it continues to overwrite the local file.

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 workaround.

Next steps

  • Add and test the public A or AAAA record from another network if the hostname will serve websites, mail, TLS, 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 records that still reference the previous name.

FAQ

Can I use the server’s IP address as its hostname?

No. Use a short DNS-safe name such as web01, or an FQDN such as web01.example.com. An IP address belongs in DNS or /etc/hosts, not as the static hostname.

Does changing the hostname update public DNS?

No. hostnamectl changes Ubuntu’s local system identity and /etc/hostname. Public forward DNS and reverse DNS are separate records managed through your DNS provider or VPS control panel.

Will changing the hostname disconnect my SSH session?

Normally it will not; the current session remains open. Its prompt may still show the old name, so open a new session after the change and close the original only after confirming access.

Is a reboot required on Ubuntu 22.04?

No. The new name becomes active immediately and persists through /etc/hostname. Reboot only when another maintenance task requires it, or restart a specific application that caches the hostname at startup.

Samrat Ghosh
Written by

Samrat Ghosh

Founder & Infrastructure Engineer

Samrat Ghosh is the founder of VPSLake, where he builds and runs the remote desktop and VPS hosting infrastructure the company is built on. He writes hands-on guides about RDP, Windows Server, VPS management and secure remote access - the practical documentation he wishes had…

Previous Article

VPS for Prop Firm Trading: FTMO Rules and Setup

Next Article

How Much RAM Do You Need for an RDP? 4 GB vs 8 GB vs 16 GB

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *

Subscribe to our Newsletter

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨