How to Change the Hostname on Rocky Linux
To change the hostname on Rocky Linux, run sudo hostnamectl set-hostname NEW_NAME. For example, you can set it to web01.example.com and verify the result with hostnamectl. Changing the system hostname does not automatically update DNS or /etc/hosts, so check name resolution if other computers or applications must reach the server by its new name.
Check the current hostname
Before making a change, record the current name. It can help you identify services or configuration files that still refer to the old name.
hostnamectl hostname cat /etc/hostname
hostnamectl displays the static and transient hostnames along with other system information. The static hostname is stored in /etc/hostname and persists across reboots. The Rocky Linux administration guide recommends using hostnamectl rather than editing that file directly.
Change the hostname with hostnamectl
Replace web01.example.com in this example with the name you intend to use. Changing the system hostname requires administrative privileges.
sudo hostnamectl set-hostname web01.example.com
Without a hostname-type option, set-hostname sets the hostname types together. You can specify --static if you only want to set the static hostname, but the command above is sufficient for a typical change. See the Red Hat hostnamectl documentation for the distinction between static, transient, and pretty hostnames.
You can also use a short name such as web01. If you identify the server by a domain name, a fully qualified domain name (FQDN) such as web01.example.com may be appropriate. Whichever format you choose, avoid conflicts with other hosts and check whether any applications expect a particular name.
Verify the change
Check both the active hostname and the persistent static setting:
hostnamectl hostname cat /etc/hostname
To display only the static hostname, run:
hostnamectl --static
The new name may appear in hostnamectl and hostname immediately while an existing shell prompt still shows the old name. Open a new login session to check the prompt. Services that read the hostname only at startup may also need to be restarted. A reboot can apply the new name across those services, but you do not need to reboot merely to check whether hostnamectl saved the change. On a production server, plan any restart around service availability.
If the new name does not resolve
Changing the system hostname does not automatically create or update DNS records or entries in /etc/hosts. If the server or its clients must resolve the new name to an IP address, check the result:
getent hosts web01.example.com
If no address is returned, or the address is incorrect, update the DNS record or the relevant /etc/hosts entry used in your environment. If an old-name mapping exists in /etc/hosts, change it only when appropriate; do not remove the localhost entries. example.com is a documentation example, not a domain to copy into your production configuration.
Common problems
The command fails with a permission error
Changing the hostname requires administrative privileges. Run the command with sudo and verify that your account has permission to use it.
The old hostname returns after a reboot
First, check hostnamectl --static and cat /etc/hostname to see whether the static name was saved. If the saved and active names keep diverging, investigate whether cloud-init, another configuration-management tool, or network configuration is assigning a different name. Identify the source of the change before repeatedly editing files.
Frequently asked questions
Do I have to reboot after changing the hostname?
You can verify the new name with hostnamectl right away. An existing shell prompt or a service that cached the old name may need a new login or a service restart. A planned reboot is another way to apply the new name across the environment.
Do I need to edit both /etc/hostname and /etc/hosts?
Normally, use hostnamectl rather than editing /etc/hostname directly. Edit /etc/hosts only if an old mapping exists there or local name resolution requires an entry that matches the actual network configuration.









