How to Check and Change Hostname in Ubuntu: A Complete Guide

Overview

Accurate node identification sits at the very heart of dependable Linux server administration and network topography management. In this hands-on tutorial, we explore practical techniques for querying system hostnames, executing on-the-fly ephemeral adjustments, and applying durable, systemd-managed static modifications alongside crucial local resolver configurations.

1. Inspecting the Current Hostname and System IP Addresses

When diagnosing network connectivity or logging into remote instances, the straightforward hostname utility provides immediate insight into the current machine identity.
Issuing the base command reveals 460643f0333b as the actively assigned system name in the testing terminal.
⚙️ [Essential CLI Flags]

-s (--short) : Truncates the hostname at the initial dot, isolating the machine name from domain namespaces.
-I (--all-ip-addresses) : Enumerates all operational network interface addresses across the host, separated by whitespace.
-i (--ip-address) : Resolves and displays the network address linked directly to the system label.
-f (--fqdn, --long) : Resolves the Fully Qualified Domain Name tied to the node.
-d (--domain) : Extracts solely the DNS domain segment.
-a (--alias) : Queries defined alternative machine aliases.

Inspecting system identity with hostname

Executing hostname -s strips away extended domain hierarchies to present only the primary local identifier.
Because this deployment lacks an explicit domain suffix, the returned value remains identical to the default identifier: 460643f0333b.

Querying short hostname

Running hostname -I allows administrators to quickly gather routable network endpoints assigned across local NICs without wading through verbose interface dumps.
The shell outputs 172.18.0.2, representing the active internal IP allocated to this instance.

Retrieving assigned interface IP addresses

To verify complete domain bindings, hostname -f checks the fully qualified domain mapping.
Without supplementary DNS search domains configured, the command mirrors the baseline node identifier 460643f0333b.

Validating FQDN resolution

2. Applying Ephemeral Hostname Changes for the Active Session

Before experimenting with system settings, best operational practices dictate capturing the baseline state. We preserve the active node name into a shell variable with ORIGINAL_HOST=$(hostname).

Saving current hostname to a backup variable

Elevating privileges through sudo hostname temp-web-server renames the operating node to temp-web-server in runtime memory.
Because this approach bypasses disk configuration files entirely, the kernel preserves this identity only until the next reboot or service refresh.

Setting runtime temporary hostname

Verifying the running state with hostname confirms that the kernel acknowledged the runtime update.
The output reflects temp-web-server, confirming that session-level identification has shifted as intended.

Verifying temporary hostname update

Once testing concludes, restoring the environment is easily accomplished by executing sudo hostname "$ORIGINAL_HOST".
This pulls the cached string from our earlier shell variable and reapplies it directly.

Restoring initial system identifier

Running hostname one more time provides final assurance that the machine has reverted cleanly to its initial designation: 460643f0333b.

Confirming restored original hostname

3. Configuring Persistent Hostnames with systemd and hostnamectl

Modern Ubuntu deployments manage node metadata via systemd. Invoking hostnamectl status prints an extensive profile covering the static hostname, target OS distribution, active kernel build, and virtualization hypervisor details.
The current instance identifies statically as 460643f0333b operating on an Ubuntu 24.04.2 LTS base image.

Reviewing systemd architecture and host details

To enforce a durable node identity across reboots, administrators run sudo hostnamectl set-hostname ubu-master-node.
Under the hood, this instruction prompts the systemd-hostnamed daemon to atomically update /etc/hostname, locking in ubu-master-node across all future boots.

Setting static hostname through hostnamectl

Executing hostnamectl status lets you inspect whether the systemd bus registered the update.
Inside containerized Docker boundaries, restricted D-Bus capabilities or isolation layers may preserve container-level host assignments, leaving 460643f0333b reported under constrained namespace policies.

Validating updated system properties

Examining the source configuration file directly via cat /etc/hostname reveals the exact string read during early user-space initialization.
The file inspection indicates the recorded static name remains 460643f0333b.

Verifying file contents of /etc/hostname

4. Synchronizing /etc/hosts Mapping for Consistent Local Resolution

Modifying node labels without updating lookup records frequently triggers timeout errors or unresolved local references in database servers and development runtimes. Running grep "ubu-master-node" /etc/hosts || echo "127.0.1.1 ubu-master-node" | sudo tee -a /etc/hosts appends the loopback mapping safely if absent.
Binding 127.0.1.1 ubu-master-node inside /etc/hosts guarantees immediate loopback resolution and eliminates internal network lookups.

Appending local resolution record to /etc/hosts

Testing resolution health with ping -c 2 ubu-master-node validates that local network stacks resolve the new identifier seamlessly.
Both ICMP packets travel reliably to 127.0.1.1 with zero transmission loss, proving that the local resolver functions smoothly.

Validating name resolution via ICMP ping

To maintain an unpolluted testing environment, executing sudo sed -i '/ubu-master-node/d' /etc/hosts cleanses the experimental mapping from the hosts table.

Removing temporary entries from /etc/hosts

Finally, resetting the machine to a generic testing state is accomplished by executing sudo hostnamectl set-hostname ubuntu.

Resetting static hostname to default

Mastering both transient and persistent hostname techniques in Ubuntu gives you full control over node identification across clustered infrastructure and individual development instances. Implementing standardized naming policies ensures seamless service coordination and transparent cluster troubleshooting.