Updated: 28 September 2026 · Applies to: Ubuntu 24.04, Debian 12, AlmaLinux 9 and Rocky Linux 9
CentOS 7 is end of life. On Ubuntu 24.04, Debian 12, AlmaLinux 9 and Rocky Linux 9 the hostname is set with hostnamectl.
- Show the current name:
hostnamectl - Set a new one (use a fully qualified name that resolves to the server, for example
server1.example.com):hostnamectl set-hostname server1.example.com - Add it to
/etc/hosts:192.0.2.10 server1.example.com server1 - Log out and in again to see the new name in your prompt.
The change is permanent; no reboot is needed. Create an A record for the name in DNS, and ask support to set the reverse DNS (PTR) record if you send email from the server.
Note: On a cPanel server, change the hostname in WHM with Networking Setup › Change Hostname instead, so cPanel updates its own configuration. On some cloud images, cloud-init resets the hostname at boot: set preserve_hostname: true in /etc/cloud/cloud.cfg.
Hostname rules
- Use letters, digits and hyphens only; no underscores or spaces.
- Use a fully qualified domain name (FQDN) such as
server1.example.comon servers, especially mail and cPanel servers. - Check the result with
hostname(short name) andhostname -f(full name).