
You provisioned a new VPS, cloned a virtual machine, or ran a fresh Ubuntu install. The machine is ready. But the hostname still reads ubuntu or something like ip-172-31-44-12 from your cloud provider. Every SSH session, every log file, every monitoring alert now carries that meaningless label.
This Linux server tutorial walks you through exactly how to change hostname on Ubuntu 26.04 LTS using three proven methods: the hostnamectl command, direct file editing, and the GNOME desktop interface. You will also learn how to update /etc/hosts, lock in your change against cloud-init overwrites, and verify that every layer of the system reflects the new name. No reboots required.
What Is a Hostname and Why Does It Matter?
Before touching a single command, you need to understand what you are actually changing. Ubuntu 26.04 LTS manages three distinct hostname types through hostnamectl:
- Static hostname — stored in
/etc/hostname, persists across reboots, and is the one you almost always want to change. - Transient hostname — a runtime kernel-level name, typically pushed by DHCP or cloud-init. It overrides the static hostname if cloud-init is not properly configured.
- Pretty hostname — a free-form display label with UTF-8 support, used by desktop environments. It has zero effect on networking or logs.
Here is a quick reference so the difference between them stays clear:
| Hostname Type | Stored In | Survives Reboot? | Affects Networking? |
|---|---|---|---|
| Static | /etc/hostname |
Yes | Yes |
| Transient | Kernel RAM | No | Yes |
| Pretty | machine-info |
Yes | No |
Confusing these three types is the most common reason sysadmins believe their hostname change “did not stick.” When DHCP or cloud-init pushes a transient name at boot and preserve_hostname is set to false, your static change gets silently overwritten every time the server restarts.
A correct hostname also prevents real operational problems. Wrong or default hostnames produce misidentified log entries, broken Kerberos and LDAP authentication bindings, naming collisions in container orchestration, and confusion during incident response at 2 AM when you are staring at 50 log lines from servers all named ubuntu.
Valid Hostname Format: Rules You Must Follow (RFC 1123)
Ubuntu 26.04 LTS follows RFC 1123 for hostname validation. If your hostname does not meet these rules, hostnamectl will silently sanitize the value without throwing an error, and you will end up with the wrong hostname.
Allowed characters:
- Lowercase letters
athroughz - Digits
0through9 - Hyphens
-(but not at the start or end)
What gets rejected or silently fixed:
- Spaces and underscores —
bad_namebecomesbadnamewith zero warning - Uppercase letters — converted to lowercase
- Special characters like
@,!,#
Length limits:
- Maximum 63 characters per label
- Maximum 253 characters for a full FQDN
Recommended naming convention: Use a role-environment-location format. Examples: web-prod-nyc01, db-staging-sg02, cache-dev-fra01. This format makes Ansible inventory management, Nagios/Zabbix monitoring setup, and multi-server SSH sessions significantly easier to manage.
Prerequisites Before You Begin
Run these checks before making any changes. They take under two minutes and prevent common failures.
- Confirm Ubuntu 26.04 LTS:
lsb_release -aorcat /etc/os-release - Confirm sudo privileges:
sudo -v— if this fails, you cannot proceed - Record the current hostname:
hostnamectl status— save this output before touching anything - Check for cloud-init (critical on VPS/cloud instances):
systemctl status cloud-init - Confirm systemd version: Ubuntu 26.04 ships with systemd 256+, which makes
hostnamectlfully stable
If you are on a cloud server from AWS, DigitalOcean, Hetzner, Vultr, or Azure, the cloud-init check is not optional. Skipping it means your change will revert within 30 seconds of the next reboot.
Step 1: Check the Current Hostname
Always start with a baseline. Run this command to see all three hostname types at once:
hostnamectl status
Expected output on a fresh Ubuntu 26.04 VPS:
Static hostname: ubuntu
Icon name: computer-vm
Chassis: vm
Machine ID: 4ba47688027d4ab2a45704ea84959b93
Boot ID: d65ce7b470db47e5949eeefd55606d33
Operating System: Ubuntu 26.04 LTS
Kernel: Linux 7.0.0-14-generic
Architecture: x86-64
Pay attention to the Static hostname and Transient hostname fields. If they show different values, cloud-init or DHCP is already overriding your static name.
Also note the current value from /etc/hostname directly:
cat /etc/hostname
This single-line file is what the system reads at boot. Whatever is in here is what loads as the static hostname on every restart.
Step 2: Change Hostname on Ubuntu 26.04 Using hostnamectl (Recommended Method)
hostnamectl is the systemd-native tool for hostname management. It performs two actions in a single atomic operation: it updates the live kernel hostname immediately and writes the new value to /etc/hostname. No reboot required.
Set the New Static Hostname
sudo hostnamectl set-hostname web-prod-01
Replace web-prod-01 with your actual hostname following the RFC 1123 rules above.
Why use hostnamectl and not just edit the file? Running sudo hostname new-name only updates the live kernel name and is lost on reboot. Editing /etc/hostname alone only changes what persists after the next boot. hostnamectl handles both layers simultaneously.
Set a Pretty Hostname (Optional)
If you manage Ubuntu Desktop or a system with a management GUI, you can set a human-friendly display name:
sudo hostnamectl set-hostname "Web Production Server 01" --pretty
This has zero effect on SSH sessions, DNS lookups, or log files. It only appears in desktop environment interfaces. Skip this on headless servers.
Verify the Hostname Change
Run three verification commands — each checks a different layer:
hostnamectl status
hostname
cat /etc/hostname
All three must return the same hostname. If hostnamectl status shows the new name but cat /etc/hostname still shows the old one, something interrupted the write operation. If hostname shows something different from the other two, a transient override is active.
Expected output after a successful change:
Static hostname: web-prod-01
web-prod-01
web-prod-01
Step 3: Update /etc/hosts to Match the New Hostname
This step is non-negotiable, and most tutorials skip it. hostnamectl intentionally does not modify /etc/hosts. If the 127.0.1.1 line in that file still points to the old hostname, local name resolution will break.
The consequences are not subtle. Applications using gethostbyname() will fail to resolve the machine’s own address. The sudo command will throw a warning every time you run it: sudo: unable to resolve host old-hostname. Postfix will refuse to send mail. Kerberos and LDAP client bindings will report authentication errors.
Check the Current /etc/hosts Entry
grep -E '^127\.0\.1\.1' /etc/hosts
If this returns a line with your old hostname, you need to update it.
Edit /etc/hosts
sudo nano /etc/hosts
Find this line:
127.0.1.1 old-hostname
Change it to:
127.0.1.1 web-prod-01
If you also use a Fully Qualified Domain Name (FQDN), format it like this:
127.0.1.1 web-prod-01.yourdomain.com web-prod-01
Do not touch the 127.0.0.1 localhost line. That entry handles loopback resolution and is separate from hostname mapping. Leave it exactly as it is.
Verify /etc/hosts After Saving
grep -E '^127\.' /etc/hosts
Confirm both the 127.0.0.1 and 127.0.1.1 lines look correct before closing the file.
Step 4: Lock In the Change Against cloud-init (Cloud VPS Users)
If you are on any cloud provider, this is the most important step in this entire guide. Cloud-init runs on every boot. By default, preserve_hostname is set to false, which tells cloud-init to pull the hostname from the cloud metadata service and overwrite whatever is in /etc/hostname.
This means your hostname change from Step 2 will be completely erased within 30 seconds of the next reboot unless you fix this setting.
Check Whether cloud-init Is Active
systemctl status cloud-init
If this returns active (exited) or active (running), cloud-init is present. You must complete this step.
Edit cloud.cfg
sudo nano /etc/cloud/cloud.cfg
Find this line:
preserve_hostname: false
Change it to:
preserve_hostname: true
Save and exit with Ctrl+X, then Y, then Enter.
Verify the cloud-init Setting
grep 'preserve_hostname' /etc/cloud/cloud.cfg
Expected output:
preserve_hostname: true
With this flag set to true, cloud-init will read whatever hostname is already in /etc/hostname and leave it alone on every subsequent boot.
Step 5: Change Hostname via Direct File Edit (For Scripts and Recovery Mode)
The hostnamectl method covers the vast majority of use cases. However, there are situations where it is not available: recovery mode shells, minimal cloud images without systemd fully initialized, or automation scripts that run at first-boot before systemd is active.
Write the New Hostname to /etc/hostname
printf '%s\n' 'web-prod-01' | sudo tee /etc/hostname
Why use tee and not echo > /etc/hostname? Shell redirection with > runs as your current user before sudo can elevate privileges. On a root-owned file like /etc/hostname, that silent permission failure means the file never gets written. tee receives the output of printf via the pipe and writes it as root, which actually works.
Apply the Change to the Live System Without Rebooting
sudo hostname "$(cat /etc/hostname)"
This reads the value you just wrote to /etc/hostname and pushes it into the running kernel immediately. The command runs silently on success.
Verify Both Layers Match
hostname && cat /etc/hostname
Both outputs must be identical. If they differ, the live kernel is running a different hostname than what will load on the next reboot, and that inconsistency will cause problems.
Step 6: Change Hostname via Ubuntu 26.04 Desktop GUI
For Ubuntu Desktop users who prefer not to use the terminal, GNOME Settings offers a straightforward rename option.
- Click the top-right corner and open Settings
- Navigate to System and then About
- Click the Device Name field
- Type the new hostname in the Rename Device dialog box
- Click Rename to confirm
- Log out and back in to refresh the shell prompt
Important limitation: The GNOME rename dialog only sets the static hostname. It does not update /etc/hosts. Always run the /etc/hosts verification from Step 3 after using the GUI method, because the sudo: unable to resolve host warning will still appear otherwise.
The GUI method is only relevant for Ubuntu Desktop. Ubuntu Server has no graphical interface by default, and production servers should always use hostnamectl for accuracy and auditability.
Step 7: Refresh Your Shell Prompt Without Logging Out
After a successful hostname change, your current terminal session will still display the old hostname in the prompt. This is normal behavior. The $HOSTNAME environment variable is loaded when the shell process starts, not updated dynamically mid-session.
To refresh the current session immediately:
exec bash
This replaces the current shell process with a new one, which reads the updated hostname at initialization. For SSH sessions, disconnect and reconnect. The new hostname will appear in the prompt on the new connection.
Full Verification Checklist
Run this full sequence after completing all steps to confirm that every layer of hostname resolution is consistent:
# Check all three hostname types
hostnamectl status
# Confirm the live kernel hostname
hostname
# Confirm the persistent file
cat /etc/hostname
# Confirm /etc/hosts mapping
grep -E '^127\.' /etc/hosts
# On cloud servers: confirm cloud-init setting
grep 'preserve_hostname' /etc/cloud/cloud.cfg
A mismatch between any two of these layers is the root cause of issues that appear in applications, authentication systems, and monitoring agents hours or days after you thought the hostname was changed correctly.
Troubleshooting Common Hostname Issues on Ubuntu 26.04
Problem 1: Hostname Reverts to the Old Name After Reboot
Root cause: cloud-init is overwriting your static hostname from the cloud metadata service.
Fix:
sudo nano /etc/cloud/cloud.cfg
# Set: preserve_hostname: true
Verify with grep 'preserve_hostname' /etc/cloud/cloud.cfg.
Problem 2: sudo Throws “unable to resolve host” Warning
Root cause: /etc/hosts still maps 127.0.1.1 to the old hostname.
Fix:
sudo nano /etc/hosts
# Update: 127.0.1.1 old-hostname to 127.0.1.1 new-hostname
This warning appears on every sudo command until you fix this line.
Problem 3: Hostname Contains an Underscore and Got Silently Changed
Root cause: Ubuntu 26.04 strips underscores and other invalid characters during the hostnamectl write without any error output.
Fix: Use only lowercase letters, numbers, and hyphens. Set the hostname again with a compliant name:
sudo hostnamectl set-hostname web-prod-01
Problem 4: Shell Prompt Still Shows the Old Hostname
Root cause: The current shell session loaded $HOSTNAME when it started.
Fix:
exec bash
Or open a new terminal window. No reboot needed.
Problem 5: Hostname Change Not Reflected in DNS or LDAP
Root cause: Changing the OS-level hostname does not update external DNS A-records, reverse DNS entries, or LDAP machine account attributes. These are separate infrastructure layers.
Fix: DNS records must be updated manually through your DNS provider’s panel or via dynamic DNS (nsupdate). LDAP machine accounts need to be updated by your directory service administrator or through your provisioning automation (Ansible, Puppet, Chef).