Changing the hostname gives a Linux machine a stable identity in shell prompts, logs, inventory, monitoring, and remote-management tools. Renaming a fresh install that still uses a generic name such as host is a common cleanup task after provisioning a new VM, cloud instance, or physical server.
On current systemd-based systems such as Ubuntu Server 24.04, hostnamectl talks to systemd-hostnamed to manage the host name. Updating the hostname this way changes the running system immediately and writes the persistent static value to /etc/hostname, which takes precedence over transient names learned from DHCP or other network configuration.
The examples here target a current Ubuntu server. Use a lowercase DNS label such as web-01 unless your environment requires a fully qualified domain name. On Ubuntu-style installs, check /etc/hosts afterward because the old short name may still be mapped on 127.0.1.1, and treat a hostname that changes back later as a sign that cloud-init or configuration management still owns the system identity.
Steps to change hostname in Linux with hostnamectl:
- Review the current hostname state and confirm that hostnamectl is available on the host.
$ hostnamectl status Static hostname: host Icon name: computer-vm Chassis: vm Operating System: Ubuntu 24.04.3 LTS Kernel: Linux 6.8.0-90-genericIf hostnamectl is missing or the system bus is unavailable, this machine uses a different hostname-management path and these steps do not apply unchanged.
- Set the new hostname with hostnamectl.
$ sudo hostnamectl hostname web-01
Use a single lowercase DNS label such as web-01 unless the environment standard requires a fully qualified domain name such as web-01.example.net.
If the old hostname appears in DNS records, SSH trust, TLS certificates, monitoring targets, or configuration-management data, update those references separately before treating the rename as complete.
- Confirm that the running host and the persistent hostname file now show the new name.
$ hostnamectl --static web-01 $ hostname web-01 $ cat /etc/hostname web-01
If /etc/hostname does not match hostnamectl –static, or the name reverts later, another management layer such as cloud-init or configuration management is rewriting it.
- Check whether Ubuntu still maps the old short name on 127.0.1.1 in /etc/hosts.
$ grep -n '127.0.1.1' /etc/hosts 2:127.0.1.1 host
If this command prints nothing, there is no host-specific local mapping to change on this system.
- Update the 127.0.1.1 line in /etc/hosts if the previous step still showed the old name.
$ sudoedit /etc/hosts
127.0.0.1 localhost 127.0.1.1 web-01
Keep the localhost entries unchanged and replace only the host-specific hostname fields on the 127.0.1.1 line.
Related: How to edit the hosts file in Linux
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.
