Changing the hostname on a Linux system keeps machine names aligned with their roles, locations, or inventory records. Clear hostnames make it easier to identify systems in SSH prompts, monitoring dashboards, and log files, especially in multi-server environments.
On modern systemd-based distributions, hostname information is managed by systemd-hostnamed and stored primarily in /etc/hostname. The hostnamectl utility communicates with this service to set the static hostname, and optionally pretty and transient hostnames, so that shells, daemons, and network tools agree on how the system identifies itself.
The procedure assumes a systemd-based Linux distribution and access to a user account with sudo privileges. Changing a hostname can temporarily confuse services that cache names or rely on reverse DNS, so adjustments to DNS records, SSH known-hosts, and configuration management should accompany the change on production systems.
Steps to change hostname in Linux:
- Open a terminal.
- Check the current hostname.
$ hostname linux-lab
Shows the short system name used in prompts and log files.
- Display detailed hostname information using hostnamectl.
$ hostnamectl status Static hostname: linux-lab Icon name: computer-container Chassis: container Machine ID: b7197b121c7b43be81a7c8a9fd9a6ce0 Boot ID: 9a994286288f466ca3c6c29b344f450e Virtualization: docker Operating System: Ubuntu 24.04.3 LTS Kernel: Linux 6.12.54-linuxkit Architecture: arm64Confirms that systemd manages the hostname and shows the current static value.
- Set a new static hostname using hostnamectl.
$ sudo hostnamectl set-hostname linux-lab.example.net Could not set static hostname: Failed to set static hostname: Device or resource busy
Changing the hostname on a production system without updating DNS, monitoring, and configuration management can cause failed name lookups and confusing log entries.
Some container runtimes block static hostname changes; use a VM or host system if hostnamectl reports a static hostname error.
- Confirm that the running session reports the new hostname.
$ hostname linux-lab
- Verify that the new hostname is stored in /etc/hostname.
$ cat /etc/hostname linux-lab
An incorrect entry in /etc/hostname can cause inconsistent prompts or service identification after reboot.
- Open a new terminal session to ensure prompts and logins display the updated hostname.
$ hostname linux-lab
Opening a fresh session ensures remote shells, SSH connections, and login banners show the new system name.
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
