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.
$ hostnamectl status
Static hostname: host
Icon name: computer-vm
Chassis: vm
Operating System: Ubuntu 24.04.3 LTS
Kernel: Linux 6.8.0-90-generic
If hostnamectl is missing or the system bus is unavailable, this machine uses a different hostname-management path and these steps do not apply unchanged.
$ 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.
$ 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.
$ 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.
$ 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