How to Change Hostname on Ubuntu 26.04, 24.04 and 22.04

Change hostname on Ubuntu 26.04, 24.04, and 22.04. Covers hostnamectl, /etc/hosts check, /etc/hostname edits, and no-reboot fixes.

Last updatedAuthorJoshua JamesRead time4 minGuide typeUbuntu

Duplicate or stale hostnames make SSH prompts, router leases, and inventory records harder to trust. To change hostname on Ubuntu, set the static name with the hostnamectl utility and then check /etc/hosts so local resolution follows the new name.

Ubuntu 26.04 LTS, 24.04 LTS, and 22.04 LTS use the same systemd hostname workflow on Desktop, Server, and SSH-managed systems. The older hostname command still helps with temporary tests, while cloud-init and /etc/hosts are the usual places to inspect when a name reverts or triggers local-resolution errors.

Change Hostname on Ubuntu with hostnamectl

hostnamectl is the cleanest default method because it updates the live hostname and writes the static hostname to /etc/hostname. It works without rebooting on supported Ubuntu LTS releases.

Check the Current Ubuntu Hostname

Record the current hostname first, especially on a remote server where the shell prompt or inventory name may be your main orientation point.

hostnamectl hostname
cat /etc/hostname
ubuntu-26-04
ubuntu-26-04

Choose a Safe Ubuntu Hostname

Use a static hostname that works as a DNS label: lowercase ASCII letters, numbers, and hyphens, with optional single dots between labels when you intentionally use an FQDN. Each label should start and end with a letter or number, and Ubuntu keeps the kernel hostname within a 64-character limit. Avoid spaces and underscores in the static hostname; save human-friendly text for a pretty hostname.

Set the Static Hostname on Ubuntu

Set the new static hostname with hostnamectl. Replace ubuntu-linux with the name you want this machine to use.

sudo hostnamectl set-hostname ubuntu-linux

Changing the hostname over SSH does not drop the current session. Open a new shell or reconnect later if prompts, terminal titles, or inventory tools still show the old name.

If your account does not have sudo access yet, add a new user to sudoers on Ubuntu before changing the system hostname.

Verify the Hostname Change on Ubuntu

The command is silent when it succeeds. Check both the live hostname and the saved static hostname file.

hostnamectl hostname
cat /etc/hostname
ubuntu-linux
ubuntu-linux

Update the Local Host Entry on Ubuntu

hostnamectl updates /etc/hostname, but it does not rewrite a local 127.0.1.1 entry in /etc/hosts. Check that line before assuming local name resolution now matches the new hostname.

grep -E '^127\.0\.1\.1[[:space:]]' /etc/hosts
127.0.1.1 ubuntu-26-04

If that line still shows the old name, edit /etc/hosts and replace only the hostname on the 127.0.1.1 line. Leave the localhost entries unchanged.

sudo nano /etc/hosts

Repeat the check after saving the file.

grep -E '^127\.0\.1\.1[[:space:]]' /etc/hosts
127.0.1.1       ubuntu-linux

Understand Static, Pretty, and Transient Hostnames

Ubuntu’s systemd hostname service separates three related names. Most server and SSH work only needs the static hostname, but the distinction helps when desktop settings, DHCP, or temporary tests seem to disagree.

  • Static hostname: the saved system hostname stored in /etc/hostname.
  • Pretty hostname: a display name for humans, often useful on desktops, and allowed to contain spaces.
  • Transient hostname: the current runtime name, which can come from DHCP, mDNS, or a temporary hostname command.

Set an Optional Pretty Hostname on Ubuntu

A pretty hostname is display-only. Use it when you want a friendlier desktop label without changing the static hostname used for SSH prompts, scripts, and local resolution.

sudo hostnamectl set-hostname "Ubuntu Lab Workstation" --pretty
hostnamectl --pretty
Ubuntu Lab Workstation

Change Ubuntu Desktop Hostname in Settings

Ubuntu Desktop can also rename the device from GNOME Settings. Open Settings, go to System and then About, and edit the Device Name field. Older Ubuntu desktop layouts may show About directly in Settings.

Use the terminal method instead when you are connected over SSH, working on Ubuntu Server, or need to inspect the static, pretty, and transient hostnames separately.

Change Hostname on Ubuntu by Editing /etc/hostname

The file-based method is useful in recovery shells, stripped-down environments, or scripts where you want to write the saved hostname directly. Changing the file alone does not rename the live system immediately, so apply the saved value before verifying.

Write a New Hostname to /etc/hostname on Ubuntu

This command writes the new hostname into the root-owned file. tee is used because plain > redirection happens before sudo can write to /etc/hostname.

printf '%s\n' 'ubuntu-linux' | sudo tee /etc/hostname
ubuntu-linux

Apply the Saved Hostname Without Rebooting

Apply the saved hostname to the running system with hostname -F. The -F option reads the hostname from the file you just wrote.

sudo hostname -F /etc/hostname

Verify the File-Based Hostname Change

Check both the live hostname and the saved file so you know they now match.

hostname
cat /etc/hostname
ubuntu-linux
ubuntu-linux

Temporarily Change Hostname on Ubuntu

The hostname command without -F changes the live runtime hostname only. It is useful for short tests, but it does not update /etc/hostname or /etc/hosts, so the saved static hostname returns after reboot or after another hostnamectl change.

sudo hostname ubuntu-temporary
hostname
ubuntu-temporary

Troubleshoot Ubuntu Hostname Changes

Fix sudo Unable to Resolve Host After Rename

If sudo starts warning about the new hostname, the 127.0.1.1 line in /etc/hosts usually still points at the old name.

sudo: unable to resolve host ubuntu-linux: Name or service not known

Compare the active hostname with the local hosts entry, then update /etc/hosts as shown earlier.

hostnamectl hostname
grep -E '^127\.0\.1\.1[[:space:]]' /etc/hosts

Prevent cloud-init from Reverting the Hostname

Cloud images, provider-managed VPS builds, and Ubuntu guests cloned from cloud-init templates may let cloud-init control the hostname. If a manual rename reverts after reboot, first check whether cloud-init is present.

command -v cloud-init
/usr/bin/cloud-init

If command -v cloud-init prints no path, skip this cloud-init step and keep troubleshooting /etc/hostname, /etc/hosts, or the platform that manages the machine.

The cloud-init hostname documentation describes preserve_hostname: true as the setting that prevents cloud-init from updating the system hostname or /etc/hostname. Add a small drop-in, then apply the hostname again with hostnamectl.

sudo install -d -m 0755 /etc/cloud/cloud.cfg.d
printf '%s\n' '#cloud-config' 'preserve_hostname: true' | sudo tee /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg
sudo hostnamectl set-hostname ubuntu-linux
#cloud-config
preserve_hostname: true

Conclusion

The Ubuntu hostname is now set as a saved static name, with local resolution, temporary test names, and cloud-init behavior separated instead of mixed together. For related admin work, enable SSH on Ubuntu before remote maintenance, or check Ubuntu version when you need to confirm another machine’s release.

Share this guide

Help another Linux user troubleshoot faster

Share this guide with someone troubleshooting Linux systems or saving it for later.

Follow LinuxCapable

Want more LinuxCapable guides in Google?

Add LinuxCapable as a preferred source so Google can show more of our fresh Linux tutorials in Top Stories and From your sources when relevant.

Add LinuxCapable as a preferred source on Google
Search LinuxCapable

Need another guide?

Search LinuxCapable for package installs, commands, troubleshooting, and follow-up guides related to what you just read.

Found this guide useful?

Support LinuxCapable to keep tutorials free and up to date.

Buy me a coffeeBuy me a coffee
Before commenting, please review our Comments Policy.
Formatting tips for your comment

You can use basic HTML to format your comment. Useful tags currently allowed in published comments:

You type Result
<code>command</code> command
<strong>bold</strong> bold
<em>italic</em> italic
<blockquote>quote</blockquote> quote block

Got a Question or Feedback?

We read and reply to every comment - let us know how we can help or improve this guide.

Verify before posting: