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
sudoaccess 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
hostnamecommand.
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.


Formatting tips for your comment
You can use basic HTML to format your comment. Useful tags currently allowed in published comments:
<code>command</code>command<strong>bold</strong><em>italic</em><blockquote>quote</blockquote>