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.

More in This Category
How to Install MariaDB on Rocky Linux

How to Install MariaDB on Rocky Linux

Install MariaDB on Rocky Linux, enable the database service, complete the initial security setup, create a database and account, and verify access.

How to Configure a Static IP Address on Rocky Linux

How to Configure a Static IP Address on Rocky Linux

Configure and verify a static IPv4 address on Rocky Linux with NetworkManager, including the gateway, DNS servers, connection profile, and safe remote-change checks.

How to Change the System Language and Locale on Rocky Linux

How to Change the System Language and Locale on Rocky Linux

Learn how to check and change the system-wide language and locale on Rocky Linux with localectl and glibc language packs. This guide also explains session-only changes, LANG and LC_* precedence, and troubleshooting for broken Korean characters.

How to Install PostgreSQL on Rocky Linux

How to Install PostgreSQL on Rocky Linux

Install PostgreSQL on Rocky Linux, initialize the database cluster, enable the service, create a database and user, and review network and authentication settings.

Rocky Linux Administration Memo: Essential Commands and Configuration Notes

Rocky Linux Administration Memo: Essential Commands and Configuration Notes

A practical Rocky Linux administration reference covering packages, services, networking, storage, security controls, logs, and frequently used maintenance commands.

How to Change the Hostname on Rocky Linux

How to Change the Hostname on Rocky Linux

Learn how to change a Rocky Linux hostname with hostnamectl, verify the persistent setting, and check name resolution and services afterward.

How to Use DNF on Rocky Linux: Packages, Repositories, and Updates

How to Use DNF on Rocky Linux: Packages, Repositories, and Updates

Use DNF on Rocky Linux to search, install, remove, update, and inspect packages and repositories while keeping package-management changes controlled.

How to List Installed Packages on Rocky Linux

How to List Installed Packages on Rocky Linux

Use DNF and RPM to list, search, count, and inspect installed packages on Rocky Linux and identify which package owns a file.

How to Configure Root SSH Login on Rocky Linux Safely

How to Configure Root SSH Login on Rocky Linux Safely

Review how root SSH login is controlled on Rocky Linux, why direct root access is risky, and how to change and verify sshd settings without losing remote access.

How to Configure SELinux Modes on Rocky Linux

How to Configure SELinux Modes on Rocky Linux

Check and change SELinux enforcing, permissive, and disabled modes on Rocky Linux, understand temporary and persistent changes, and troubleshoot denials safely.