An old hostname keeps showing up in shell prompts, SSH sessions, and local inventory long after a machine changes jobs. You can change hostname on Ubuntu in a few commands, and hostnamectl updates the saved hostname and the live system name immediately on Ubuntu Desktop, Ubuntu Server, and remote SSH sessions.
The same workflow also covers the direct /etc/hostname fallback, a no-reboot apply step for file-only changes, and a quick check for /etc/hosts entries that still point at the old local name.
These commands work the same on Ubuntu 26.04 LTS, 24.04 LTS, and 22.04 LTS. If you need to confirm the release first, check Ubuntu version before changing the hostname.
Change Hostname on Ubuntu with hostnamectl
For most systems, hostnamectl is the cleanest method because it updates the current hostname and writes the same value to /etc/hostname. Keep the name simple with lowercase letters, numbers, and hyphens so shell prompts, inventory tools, and local resolution stay predictable. On Ubuntu 26.04, 24.04, and 22.04, values like bad name and bad_name are sanitized to badname, so set the exact hostname you want instead of relying on automatic cleanup.
sudo hostnamectl set-hostname ubuntu-linux
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
Check the live hostname after the change. On the supported Ubuntu LTS releases, this updates immediately and does not require a reboot.
hostnamectl hostname
ubuntu-linux
Check the Local Host Entry on Ubuntu
Ubuntu can keep a separate local host entry under 127.0.1.1. hostnamectl does not rewrite that line, so inspect it if you want /etc/hosts to match the new hostname as well.
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 it with the new hostname.
sudo nano /etc/hosts
Update only the old hostname on the 127.0.1.1 line and leave the localhost entries unchanged.
Repeat the same check after saving the file so the local mapping matches the hostname you just set.
grep -E '^127\.0\.1\.1[[:space:]]' /etc/hosts
127.0.1.1 ubuntu-linux
Change Hostname on Ubuntu by Editing /etc/hostname
The file-based method is useful in recovery mode, stripped-down environments, or scripts where you want to edit the saved hostname directly. Changing the file alone does not rename the live system immediately, so apply the saved value right away if you do not want to reboot.
Write a New Hostname to /etc/hostname on Ubuntu
This one-line command writes the new hostname into the root-owned file. tee is used here 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 on Ubuntu Without Rebooting
After editing only the file, apply that saved value to the current session. The $(cat /etc/hostname) part reads the hostname you just saved and passes it to hostname in the same command.
sudo hostname "$(cat /etc/hostname)"
This command is silent when it succeeds, so verify the live hostname and the saved file in the next step.
Verify the File-Based Hostname Change on Ubuntu
Check both the live hostname and the saved hostname file so you know they now match.
hostname
cat /etc/hostname
ubuntu-linux ubuntu-linux
Change Hostname on Ubuntu FAQ
No. hostnamectl updates the live hostname and /etc/hostname immediately on Ubuntu 26.04, 24.04, and 22.04. If you edit /etc/hostname directly, either apply it with sudo hostname "$(cat /etc/hostname)" or reboot later.
No. hostnamectl changes the live hostname and writes the new value to /etc/hostname, but it does not rewrite a 127.0.1.1 line in /etc/hosts. If that file still shows the old hostname, update it manually so local naming stays consistent.
Yes. Changing the hostname over SSH does not drop the current session, so you can rename a remote server or VM in place. Open a new shell or reconnect later if you want prompts, terminal titles, or management tools to pick up the new name everywhere.
Conclusion
The new hostname is active on your Ubuntu system, and hostnamectl gives you the quickest no-reboot path when a workstation, VM, or remote server changes roles. If you are renaming a machine you manage over the network, install SSH on Ubuntu or check Ubuntu version so inventory and remote admin stay tidy.
Formatting tips for your comment
You can use basic HTML to format your comment. Useful tags currently allowed:
<code>command</code>command<strong>bold</strong><em>italic</em><blockquote>quote</blockquote>