Loading...

Lab 48: Changing System Hostname

Update a Linux hostname to match a new naming convention using hostnamectl and confirm the change through system status and config files. Validate runtime and persistent hostname state and ensure local resolver mapping aligns with the new name.

network core services

Scenario

Your company recently restructured its internal network. You’ve been asked to update hostnames to reflect new node naming conventions. The current hostname is old-server and it must be changed to web-node01 .

Operator context

Hostnames show up everywhere: monitoring dashboards, SSH prompts, log records, certificates, and inventory systems. A correct change is runtime-correct and persistent across reboots.

Objective

  • View the current hostname using hostnamectl .
  • Change the static hostname to web-node01 .
  • Confirm the hostname has been updated via hostnamectl .
  • Verify the persistent hostname in /etc/hostname .
  • Confirm the local mapping in /etc/hosts reflects the new hostname.

Concepts

  • Static hostname vs transient hostname: what persists and what can be overwritten by DHCP or network management.
  • hostnamectl as the authoritative interface for systemd-hostnamed.
  • /etc/hostname as the persistent source on many distributions.
  • /etc/hosts and predictable local resolution for the node name.
  • Where hostname changes surface: prompts, logs, monitoring, and certificates.

Walkthrough

Step 1 : View the current hostname.
Command
hostnamectl

This shows hostname state and platform metadata. The key field is the static hostname, which persists across reboots.

Static hostname: old-server
      Icon name: computer-vm
        Chassis: vm
     Machine ID: a1b2c3d4e5f6g7h8i9j0
        Boot ID: x1y2z3a4b5c6d7e8f9g0
Operating System: Ubuntu 22.04.4 LTS
         Kernel: Linux 5.15.0-88-generic
   Architecture: x86-64
Step 2 : Change the hostname to web-node01.
Command
sudo hostnamectl set-hostname web-node01

This sets the static hostname persistently. You will usually see the new name reflected immediately in hostnamectl output, and in new sessions for prompts.

Hostname successfully changed.
Step 3 : Confirm the hostname has been updated.
Command
hostnamectl

Confirm runtime state and verify the static hostname matches the required node name.

Static hostname: web-node01
      Icon name: computer-vm
        Chassis: vm
     Machine ID: a1b2c3d4e5f6g7h8i9j0
        Boot ID: x1y2z3a4b5c6d7e8f9g0
Operating System: Ubuntu 22.04.4 LTS
         Kernel: Linux 5.15.0-88-generic
   Architecture: x86-64
Step 4 : Verify persistence in /etc/hostname.
Command
cat /etc/hostname

This is the simplest proof of persistence on systems that store the hostname in this file. The content should match the static hostname.

web-node01
Step 5 : Align /etc/hosts with the new hostname.
Command
cat /etc/hosts

Many systems map the local hostname in /etc/hosts (commonly to 127.0.1.1 on Ubuntu defaults). If this mapping is wrong, local name resolution can behave unexpectedly and services may log inconsistent host identifiers.

127.0.0.1   localhost
127.0.1.1   web-node01
Step 6 : Verify what name your shell and resolver see.
Commands
hostname
getent hosts "$(hostname)"

This confirms the active hostname value and verifies that the system can resolve the hostname via local name service configuration. If resolution fails, review /etc/hosts and your NSS configuration.

web-node01
127.0.1.1   web-node01

Common breakpoints

Prompt does not change immediately

Your current shell may cache the hostname in the prompt. Open a new session or refresh your prompt configuration to see the updated name.

Local resolution returns the old hostname

Update the hostname mapping in /etc/hosts and confirm that getent hosts "$(hostname)" returns the expected address-to-name mapping.

Static vs transient hostname confusion

Ensure you set the static hostname with hostnamectl set-hostname . A transient hostname can be overwritten by network or DHCP behavior on some systems.

Cleanup checklist

This lab is usually a permanent change. If you need to revert for a shared lab image, set the hostname back and restore the /etc/hosts mapping.

Commands
sudo hostnamectl set-hostname old-server
sudo sed -i 's/\bweb-node01\b/old-server/g' /etc/hosts
hostnamectl
cat /etc/hostname
Success signal

The static hostname matches the intended value, /etc/hostname matches it, and the hostname resolves locally to the expected mapping.

Reference

  • hostnamectl : Displays and manages system hostname settings via systemd-hostnamed.
  • hostnamectl set-hostname <name> : Sets the static hostname persistently.
  • hostname : Prints the current hostname as seen by the running system.
  • getent hosts <name> : Queries name service switch (NSS) host resolution for a name.
  • /etc/hostname : Persistent hostname file on many Linux distributions.
  • /etc/hosts : Local name-to-address mapping file used for predictable local resolution.