Website Vidya
Linux & ServersStep-by-Step Guide

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.

RR
Rajesh RayLead Systems Educator
Published:
5 min read
PDF Cheat Sheet
How to Change Hostname on Ubuntu Server and Desktop
AI Executive SummaryWebsiteVidya AI

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.

Key Highlights
  • Prerequisites and Supported Releases
  • Step 1: Checking the Current Hostname on Ubuntu
  • Step 2: Understanding Static, Transient, and Pretty Hostnames
Grounded in full engineering runbookWebsiteVidya AI Engine

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.

Advertisement

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 sudo administrative 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.

Advertisement

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.

Advertisement

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.

Deepen your Linux system administration skills with our related production-tested guides:

Authoritative References and Official Documentation

Frequently Asked Questions

Quick answers to common questions and configurations

4 FAQs
No. 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.
Debian and Ubuntu allocate 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.
Internet standards (RFC 952 and RFC 1123) forbid underscores in hostnames because DNS treats them as invalid characters. While Linux technically permits uppercase letters, hostnames are case-insensitive and standard convention mandates lowercase characters.
Log out of your current SSH or terminal session and reconnect, or run exec bash to restart your shell session with the updated hostname.
RR
Written byVerified Author

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.

SPAM-FREE MODERATED DISCOURSE

Join the Conversation & Perspective

Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.

AT
MP
JV
+2

No comments on this guide yet. Be the first to share your thoughts or ask a question!

Recommended Reading10 Guides

Related Technical Guides

Tested runbooks, server administration walkthroughs, and troubleshooting guides

Featured Infrastructure PartnerUp to 75% OFF + 3 Months Free

Supercharge Your Linux & Cloud Workloads with Hostinger

Deploy high-speed NVMe KVM VPS, Docker containers, and LiteSpeed-accelerated WordPress servers in under 60 seconds with full root access, dedicated IPv4, and 24/7 technical support.

Pure NVMe SSD StorageFull Root Access & SSHFree Domain & Unlimited SSL99.9% Uptime SLA
Plans from$2.99/mo
Claim Hostinger DealAffiliate link • Special discount applied at checkout