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 the runtime and persistent hostname state and ensure the 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 hostname change is both 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.

What You’ll Practice

  • Inspecting hostname state with hostnamectl and understanding static vs transient hostname.
  • Persistently setting hostnames using hostnamectl set-hostname .
  • Verifying persistence in /etc/hostname .
  • Validating local name resolution expectations with /etc/hosts (common in VM images and default distro setups).
  • Recognizing where hostnames appear in day-to-day operations (prompt, logs, monitoring).

Walkthrough

Step 1 : View the current hostname.
Command
hostnamectl

This provides a structured view of hostname state and platform metadata. The key field for this lab is the static hostname, which persists across reboot.

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 updates the static hostname persistently. You will typically see the prompt change in new sessions and the hostname reflected immediately in hostnamectl output.

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

This confirms runtime state and verifies that the persistent static hostname now matches the expected value.

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 that the hostname change is persistent on systems that store the hostname in this file. The value should match the static hostname.

web-node01
Step 5 : Confirm /etc/hosts aligns 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-style defaults). If this mapping is wrong, local name resolution can behave unexpectedly, and some services may log confusing host identifiers.

127.0.0.1   localhost
127.0.1.1   web-node01

Reference

  • hostnamectl : Displays and manages system hostname settings via systemd-hostnamed.
  • hostnamectl set-hostname <name> : Sets the static hostname persistently.
    • <name> : The new node hostname (for example web-node01 ).
  • /etc/hostname : File storing the persistent hostname on many Linux distributions.
  • /etc/hosts : Local name resolution mapping file, often used to bind the hostname to loopback for predictable local resolution.