How to Change Hostname on Ubuntu Server and Desktop
Learn how to change hostname on ubuntu 24.04 with hostnamectl, editing /etc/hosts and /etc/hostname, cloud-init configuration, and GUI settings.

5 min read
Learn how to change hostname on ubuntu 24.04 with hostnamectl, editing /etc/hosts and /etc/hostname, cloud-init configuration, and GUI settings.
- Prerequisites and Supported Releases
- Step 1: Checking the Current Hostname on Ubuntu
- Step 2: Understanding Static, Transient, and Pretty Hostnames
A hostname is a unique human-readable label that identifies a computer or virtual server on a local network or across the wider internet.
When deploying new Ubuntu instances—especially cloud servers or cloned virtual machines—the operating system is frequently provisioned with generic default names like ubuntu, localhost, or automated provider IDs.
Updating your server's hostname to reflect its operational role (such as web-prod-01, db-master, or mail.example.com) is a crucial administration step that streamlines log aggregation, prevents network conflicts, and ensures SSL and mail certificates validate properly.
In this guide, we walk through how to change hostname on ubuntu 24.04 and 22.04 without requiring a system reboot, configure local loopback resolution, and disable cloud-init resets.
Complete step-by-step administrator and desktop guide on how to change hostname on Ubuntu 24.04 LTS (Noble Numbat) and 22.04 LTS. Explains inspecting current static, transient, and pretty hostnames with hostnamectl, setting new hostname labels, synchronizing local name resolution in /etc/hosts, preventing cloud-init from resetting hostnames on cloud VPS instances, and changing hostnames via the GNOME desktop interface.
Prerequisites and Supported Releases
This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat) & Ubuntu 22.04 LTS. The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.
Before proceeding, ensure you have the following requirements satisfied:
- A server or computer running Ubuntu 24.04 LTS or Ubuntu 22.04 LTS.
- A user account with
sudoadministrative privileges. - Access to a command-line terminal shell or GNOME desktop environment.
Step 1: Checking the Current Hostname on Ubuntu
Before applying any changes, inspect your server's active hostname configuration.
On systemd-based distributions like Ubuntu, the hostnamectl command provides detailed diagnostic output for system naming:
Run hostnamectl without arguments in your terminal shell:
# Check detailed hostname and system metadata:
hostnamectl
# Expected output confirms:
# Static hostname: ubuntu-default
# Icon name: computer-vm
# Chassis: vm
# Operating System: Ubuntu 24.04 LTS
# Kernel: Linux 6.8.0-31-generic
# Architecture: x86-64
# Quick one-word check of the current hostname:
hostname
Step 2: Understanding Static, Transient, and Pretty Hostnames
Systemd categorizes hostnames into three distinct classes:
Static Hostname: The primary traditional hostname stored on disk in /etc/hostname, used during system boot and network registration.
Transient Hostname: A temporary runtime hostname maintained by the kernel, which may be dynamically assigned by DHCP or mDNS servers at boot time.
Pretty Hostname: A free-form, UTF-8 encoded descriptive title (e.g., 'Rajesh's Ubuntu Workstation') displayed in graphical desktop interfaces.
Step 3: Changing the Hostname with hostnamectl
The official, recommended method for updating hostnames on Ubuntu is using hostnamectl set-hostname.
Valid hostnames must adhere to standard Internet naming rules: 2 to 63 characters long, containing only lowercase letters, digits, and hyphens (no spaces or periods unless configuring a Fully Qualified Domain Name).
Execute the following command, replacing web-server-01 with your desired hostname:
# Set new static hostname:
sudo hostnamectl set-hostname web-server-01
# Optionally set a descriptive Pretty Hostname for desktop environments:
sudo hostnamectl set-hostname "Web Server 01 (Production)" --pretty
# Verify the new hostname configuration:
hostnamectl
# Output confirms:
# Static hostname: web-server-01
# Pretty hostname: Web Server 01 (Production)
Step 4: Updating the /etc/hosts File
Changing the hostname using hostnamectl updates /etc/hostname, but you must also update the local loopback mapping in /etc/hosts.
If the new hostname is not mapped in /etc/hosts, system commands like sudo will throw a warning: sudo: unable to resolve host [new-hostname]: Name or service not known.
Open /etc/hosts and map your new hostname to 127.0.1.1:
# Inspect current /etc/hosts contents:
cat /etc/hosts
# Update /etc/hosts so 127.0.1.1 points to your new hostname:
# Replace 'old-hostname' with 'web-server-01':
sudo sed -i "s/127.0.1.1.*/127.0.1.1 web-server-01/" /etc/hosts
# Test local name resolution using ping:
ping -c 1 web-server-01
# Output confirms 1 packets transmitted, 1 received, 0% packet loss
Step 5: Preventing Cloud-Init from Overwriting Hostnames (Cloud VPS)
On cloud virtual servers (such as AWS EC2, DigitalOcean, Hetzner, or Linode), the cloud-init utility initializes instance metadata upon every system reboot.
By default, cloud-init may reset your hostname back to the provider's default on restart unless instructed otherwise.
To preserve your custom hostname permanently, configure cloud-init to preserve hostnames:
# Check if cloud-init is installed on your system:
if [ -f /etc/cloud/cloud.cfg ]; then
# Set preserve_hostname to true in cloud.cfg:
sudo sed -i 's/preserve_hostname: false/preserve_hostname: true/' /etc/cloud/cloud.cfg
echo "Cloud-init updated to preserve custom hostname."
fi
# Alternatively create an override configuration file:
echo "preserve_hostname: true" | sudo tee /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg
Step 6: Changing Hostname via GNOME Desktop Settings (GUI)
On Ubuntu Desktop systems, changing the device name can also be performed using the GNOME Settings graphical interface.
Open the system Settings app from the application launcher.
Scroll to the bottom of the left sidebar and select System (or About).
Click on the Device Name field, type your new hostname, and click Apply or press Enter to save.
Troubleshooting and Common Errors
When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:
sudo: unable to resolve host [new_hostname]: Name or service not known
Cause: The new hostname was set with hostnamectl, but the loopback entry in /etc/hosts was not updated.
Resolution: Edit /etc/hosts and ensure 127.0.1.1 [new_hostname] is present.
echo '127.0.1.1 web-server-01' | sudo tee -a /etc/hosts
Hostname reverts back to old name after rebooting cloud instance
Cause: The cloud-init package is resetting the hostname to provider metadata during boot.
Resolution: Set preserve_hostname: true in /etc/cloud/cloud.cfg or /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg.
sudo sed -i 's/preserve_hostname: false/preserve_hostname: true/' /etc/cloud/cloud.cfg
Could not set property: Invalid hostname
Cause: The requested hostname contains illegal characters (spaces, underscores, or special symbols).
Resolution: Use only lowercase alphanumeric characters and hyphens (e.g. web-01).
sudo hostnamectl set-hostname valid-hostname-01
Security Best Practices and Hardening
Avoid using hostnames that expose internal security topologies, hardware vendors, or proprietary client names publicly.
If you change the hostname on an active Kubernetes or Docker Swarm node, update cluster node registrations to avoid broken discovery heartbeats.
Restart long-running services that bind to the system hostname (such as Postfix MTA or Apache) after modifying the hostname.
Related Ubuntu Tutorials and Documentation
Deepen your Linux system administration skills with our related production-tested guides:
- How to Set or Change Timezone on Ubuntu Server and Desktop — Configure system clock and timezone settings with timedatectl.
- Initial Server Setup on Ubuntu 24.04 LTS — Hardening and user permissions baseline setup on Ubuntu.
- How to Set a Static IP Address on Ubuntu Server and Desktop — Configure static network interfaces and DNS routing with Netplan.
Authoritative References and Official Documentation

The Linux Command Line, 2nd Edition: A Complete Introduction
Frequently Asked Questions
Quick answers to common questions and configurations
hostnamectl updates both the kernel transient hostname and the static configuration file in real time. Simply closing and reopening your terminal session will update the shell prompt.127.0.0.1 strictly to localhost, while mapping 127.0.1.1 to the system's Fully Qualified Domain Name or hostname. This design prevents applications from incorrectly binding to localhost when they require the system's actual network identity.exec bash to restart your shell session with the updated hostname.Editorial Disclosure: Website Vidya is supported by our readers. When you purchase hosting, tools, or software through our verified partner links, we may earn an affiliate commission at no extra cost to you. We only recommend solutions we trust and rigorously verify.
Rajesh Ray
I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.
Most Read Guides
TrendingUbuntu Server Not Booting? 10 Simple Steps to Fix It Fast
How to Create a 3D Button in HTML and CSS: Easy Steps
How to Install Python and Pip on Ubuntu 24.04 LTS
How to Install Nginx on Ubuntu 24.04
How to Install Python on Ubuntu 24.04 LTS
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides
Join the Conversation & Perspective
Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.
No comments on this guide yet. Be the first to share your thoughts or ask a question!