How to Change Hostname in Ubuntu Server

How to Change Hostname in Ubuntu Server

A server rename begins by choosing the name Ubuntu should use for the machine. The hostnamectl command sets that system name, while the hosts file can map the name for local lookups.

This article walks through changing the hostname and checking whether provisioning or DNS needs a separate update.

TL;DR

Run hostnamectl set-hostname with the new machine name, then check the local hosts mapping and any cloud-init policy that may manage it.

  • Read the current static name with hostnamectl --static.
  • Set the persistent name with sudo hostnamectl set-hostname web-01.
  • Update the matching local alias in /etc/hosts when present.
  • Check DNS or provisioning separately if other systems or later boots use a different name.

What is an Ubuntu hostname?

A hostname is the label an operating system uses for its own machine identity. It helps local software identify the system, but it does not by itself register a DNS name or change the server’s address.

ValueMeaningWhere it matters
Static hostnameConfigured system name stored for persistenceNormally the preferred server name
Transient hostnameFallback name that network configuration can supplyUsed when a static name is absent
Pretty hostnameHuman-readable label with fewer character restrictionsDisplay interfaces
/etc/hosts entryLocal mapping from an IP address to one or more namesResolution by programs on this machine
DNS recordName-to-address data published for a networkResolution by other devices and clients

On Ubuntu Server, hostnamectl communicates with systemd-hostnamed. A valid static name takes priority over a transient value. The Ubuntu hostname manual describes /etc/hostname as the persistent local name used during boot, while /etc/hosts serves a separate lookup role.

Think of those files as different layers rather than duplicate copies. Setting a hostname does not automatically make the name resolvable by every application, and changing a local lookup entry does not rename the operating system.

How to change the Ubuntu server hostname step by step

Choose a valid name, set it with hostnamectl, then verify the saved name and local mapping. The examples use web-01 as a placeholder, so replace it with the name intended for this server.

Step 1: Check the current name and host mapping

Read the static name, the name currently reported by the host, and any 127.0.1.1 entry before changing anything. On this system, all three checks returned ninad-agent, and the local mapping listed that same name twice.

hostnamectl --static
hostname
awk '$1=="127.0.1.1" {print}' /etc/hosts

The visible output from that check is:

ninad-agent
ninad-agent
127.0.1.1    ninad-agent    ninad-agent
Terminal output showing the static hostname, active hostname and local hosts entry
The three read-only checks show the current system name and local mapping.

The first line is the configured static hostname. The next line is the current kernel-visible name. The final line comes from /etc/hosts and shows the local alias to compare after the rename.

Step 2: Choose a hostname that fits the system

Use a name that follows the static-hostname rules and matches the naming policy for the environment. The Ubuntu manual documents lowercase ASCII letters, numbers and hyphens for a conventional label, with a Linux maximum of 64 characters. A fully qualified domain name can contain labels separated by dots, while a short local label may fit local administration better.

Use a pretty hostname for a display label that needs spaces or punctuation. Static names used by scripts and network tools should follow hostname rules instead.

Step 3: Set the persistent hostname

Run the command with administrator privileges on the Ubuntu machine you intend to rename. systemd stores the static setting for later startup and applies the system hostname change.

sudo hostnamectl set-hostname web-01

Run this command on the target server because it changes that machine’s identity. hostnamectl stores the static name and applies it to the active system immediately, so the operating system can report the new name without a reboot.

When I checked the incomplete command, hostnamectl returned “Too few arguments.” Add the intended name after set-hostname.

hostnamectl set-hostname

Step 4: Update the existing local alias

Open /etc/hosts and replace the old machine name on its existing local-host line when one is present. Ubuntu and Debian installations often use 127.0.1.1 for this entry, but inspect the file instead of assuming that every server has the same layout.

sudo nano /etc/hosts

For example, change an entry such as 127.0.1.1 old-name to 127.0.1.1 web-01. Keep the 127.0.0.1 localhost mapping and any IPv6 localhost entries intact. If an entry contains both a full name and a short alias, keep only aliases that your local configuration needs.

The hosts file associates addresses with names for local resolution. It is not the file that sets the machine’s hostname. A typo or stale alias can cause local commands that resolve the host name to warn or pause, even while hostnamectl reports the new value.

Step 5: Verify the static name and local resolution

After saving the local mapping, compare the static hostname with the active name and ask the system resolver to look up the selected label.

hostnamectl --static
hostname
getent hosts "$(hostname)"

The first two commands should report the chosen name. getent hosts checks the system’s configured name-service lookup, including /etc/hosts and enabled resolver sources. It does not prove that a public DNS record exists.

Open a new shell or SSH session if the prompt still shows the old name. Inspect its prompt configuration if the text remains stale.

Hostname changes that need extra checks

Check the source that owns the stale value before changing another hostname layer.

CaseSymptomCheck or fix
Static name looks right but local command complainsResolver cannot find the machine’s new nameInspect /etc/hosts and update the existing local alias, then check with getent hosts.
Old name returns after boothostnamectl showed the new name before rebootCheck cloud-init configuration and provider metadata for a hostname value applied at startup.
Terminal still shows old texthostname returns the new value but the prompt does notOpen a new shell or SSH session, then inspect the prompt configuration if it remains stale.
Other machines cannot find the new nameLocal commands resolve it but remote clients do notUpdate the authoritative DNS record or the inventory/service discovery system that remote clients use.
The command prints an argument errorhostnamectl set-hostname has no name after itRun hostnamectl set-hostname web-01 with the intended name and required privileges.

Cloud-init can set /etc/hostname from provider metadata during boot when preserve_hostname is not set, as its documentation explains.

Check whether cloud-init is installed, then review its active configuration and provider policy. If it owns the name, change that source of truth instead of repeating a local rename that provisioning will replace.

grep -R "preserve_hostname" /etc/cloud/cloud.cfg /etc/cloud/cloud.cfg.d

The search may return no matching line, which means those paths alone did not expose an explicit setting. Check cloud-init’s merged configuration and your provider’s metadata instructions before adding preserve_hostname: true, because that setting prevents cloud-init from updating the system hostname.

Cloud-init can also manage /etc/hosts separately. Its documentation describes a mode that rewrites the file from a template on each boot, as well as a mode that refreshes the local hostname entry while leaving other lines alone. If a hosts edit disappears, investigate that setting and template rather than repeatedly editing generated output.

A local hostname and a network DNS name are separate. If SSH clients, monitoring systems or applications must reach the server by a public or private domain name, update the relevant DNS record and verify it from the client network. Changing hostnamectl does not change the IP address or create forward and reverse DNS records.

Renaming a server can also affect software that stores its host name in configuration, certificates, monitoring labels or cluster membership. Check those dependencies when the machine participates in a managed service, and use the service’s supported rename procedure before changing the operating-system name.

A fully qualified domain name has labels separated by dots. Choose whether the server’s static hostname should use that full name or a short label before changing related records.

Active Directory, TLS and inventory systems can each keep a reference to the old name. Update those records through their own procedures because hostnamectl only changes the Linux system name.

Cloud-init can manage the system hostname and /etc/hosts through separate settings. preserve_hostname controls hostname updates, while manage_etc_hosts controls hosts-file generation.

For remote administration, keep an active SSH session open while checking a rename that may affect service identity or DNS. Automation can couple the name to inventory or registration, so verify the target through the normal management channel before ending the maintenance window.

Local name resolution follows the system’s configured name-service sources. getent hosts is useful because it asks the same resolver path that many programs use, rather than reading only one file. A successful lookup can come from /etc/hosts, DNS or another configured source, so compare its address and name with the source you intended to change.

If getent returns an address you did not expect, inspect the hosts file for duplicate aliases and review the resolver configuration before editing more entries. Do not add a second 127.0.1.1 line just to preserve the old name. Update the existing mapping deliberately, and retain additional aliases only when a local service still depends on them.

DNS checks need a client-side view when the name must work beyond the server itself. Query the organization’s configured DNS service from a machine on the relevant network and compare the returned address with the intended server address. A local hosts entry can make the name work on Ubuntu while every other device continues to receive a different answer.

Provisioning metadata, monitoring, backup policy and certificates can consume the current name. Check each system’s procedure before renaming, and coordinate cluster members that retain host inventory or health checks.

After applying the hostname command, verify the static value, active value and local lookup separately. Confirm remote DNS or service discovery from a client that relies on it. The results locate remaining work in Ubuntu, a local mapping, cloud provisioning or a network naming service.

Use the error itself to choose the next check. An argument error points to an incomplete command, local lookup failure points to the hosts mapping, a post-boot reversal points to provisioning, and remote lookup failure points to DNS or service discovery. That separation keeps a local hostname adjustment from becoming an unnecessary change to unrelated network settings.

What to check after changing the hostname

Check the DNS or service-discovery answer from a client that depends on the renamed server, then compare it with the intended server address.

Client resultNext check
The new name resolves to the intended addressConfirm the client can reach the service by that name.
The name is missing or points elsewhereReview the authoritative DNS or service-discovery entry used by that client.

Conclusion

Use hostnamectl for the Ubuntu system name, then verify its local mapping and the service that owns the name at boot or across the network. The Ubuntu hostnamectl manual, Ubuntu hosts manual and cloud-init hostname reference document those distinct layers.

For related command-line basics, see our Ubuntu terminal guide and instructions to check the Ubuntu version.

FAQ

These answers distinguish a system rename from reboot, DNS and provisioning changes.

Do I need to reboot after changing the Ubuntu hostname?

Usually no. hostnamectl updates the active system name immediately, though an existing shell prompt or service may retain its own state.

Why does the hostname change back after reboot?

Cloud-init or another provisioning layer may set the hostname during startup. Check the configuration and provider metadata that own the machine name.

Does changing the hostname update DNS?

No. A system hostname and DNS records are separate. Update the relevant DNS provider or network service when other machines must resolve the name.