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.
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 .
Hostnames show up everywhere: monitoring dashboards, SSH prompts, log records, certificates, and inventory systems. A correct change is runtime-correct and persistent across reboots.
hostnamectl
.
web-node01
.
hostnamectl
.
/etc/hostname
.
/etc/hosts
reflects the new hostname.
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
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.
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
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
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
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
Your current shell may cache the hostname in the prompt. Open a new session or refresh your prompt configuration to see the updated name.
Update the hostname mapping in
/etc/hosts
and confirm that
getent hosts "$(hostname)"
returns the expected address-to-name mapping.
Ensure you set the static hostname with
hostnamectl set-hostname
. A transient hostname can be overwritten by network or DHCP
behavior on some systems.
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.
sudo hostnamectl set-hostname old-server
sudo sed -i 's/\bweb-node01\b/old-server/g' /etc/hosts
hostnamectl
cat /etc/hostname
The static hostname matches the intended value, /etc/hostname matches it, and the hostname resolves locally to the expected mapping.
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.