Change hostname in Rocky Linux 9 with hostnamectl, a local /etc/hosts mapping, and checks for NetworkManager or cloud-init overrides. The new name becomes active immediately, and the complete setup usually takes about five minutes without a reboot.
Prerequisites
- A VPS running Rocky Linux 9, preferably updated to the latest available 9.x release.
- An SSH session as
root, or a normal user with workingsudoprivileges. - A short name such as
web01, or an FQDN such asweb01.example.com; use lowercase letters, numbers, hyphens, and dots for the static hostname. - The server’s primary IPv4 address and access to your DNS provider if other systems must resolve the FQDN.
- No additional RAM or disk space is needed for a hostname change.
- A VPSLake Linux VPS if you need a Rocky Linux server with root access.
Step 1: Inspect the current Rocky Linux name
Record the current values first so you can separate the saved hostname from a transient name supplied by networking or provisioning.
cat /etc/rocky-release
hostnamectl status --static
hostnamectl status --pretty
hostnamectl status --transient
nmcli general hostname
ip -4 route get 1.1.1.1
The release line should identify Rocky Linux 9. A blank pretty or transient value is normal on some VPS images. In the route output, note the address after src; use that value as SERVER_IP later.
Rocky Linux release 9.x (Blue Onyx)
old-web.example.com
old-web.example.com
old-web.example.com
1.1.1.1 via 203.0.113.1 dev ens3 src 203.0.113.40 uid 0
The example address is documentation-only. Substitute the real address returned by your VPS rather than 203.0.113.40.
Step 2: Set the persistent hostname
Use hostnamectl because it changes the running system and writes Rocky Linux’s persistent /etc/hostname value through systemd-hostnamed.
sudo hostnamectl set-hostname NEW_HOSTNAME
hostnamectl status --static
cat /etc/hostname
Replace NEW_HOSTNAME with a name such as web01 or web01.example.com. Do not include spaces, underscores, or a trailing dot. The setter normally prints nothing; both checks should now return the new static name:
web01.example.com
web01.example.com
If you want a readable label for administrators, set the optional pretty hostname separately. It can contain spaces, but it is not the machine name used for DNS:
sudo hostnamectl set-hostname "NEW_PRETTY_NAME" --pretty
hostnamectl status --pretty
For example, NEW_PRETTY_NAME could be Production web server. Rocky Linux 9 uses the same hostname types and hostnamectl workflow described in the RHEL 9 hostname documentation.
Step 3: Add local name resolution
Changing the system identity does not publish DNS, so add a local hosts entry when services on the VPS need to resolve the new FQDN reliably.
Open the hosts file:
sudo nano /etc/hosts
Keep the existing localhost and IPv6 lines. Replace an old machine-name line if one exists, or add this line using the src address from Step 1:
SERVER_IP NEW_HOSTNAME NEW_SHORT_NAME
For web01.example.com, the short alias is web01, so a completed entry could look like this:
203.0.113.40 web01.example.com web01
Replace SERVER_IP, NEW_HOSTNAME, and NEW_SHORT_NAME with your values before saving. If you chose only web01, use SERVER_IP web01. Keep one current machine-name entry instead of leaving old and new mappings together.
Test the local resolver:
getent hosts NEW_HOSTNAME
hostname --fqdn
hostname -s
Successful output resembles:
203.0.113.40 web01.example.com web01
web01.example.com
web01
This mapping affects the VPS only. Create an A record for IPv4 or an AAAA record for IPv6 with your DNS provider when the hostname must work from other computers. Forward DNS and reverse DNS are separate settings.
Step 4: Check NetworkManager and hostname automation
Rocky Linux 9 uses NetworkManager for network management, so compare its hostname view with the value you just set:
nmcli general hostname
systemctl is-active NetworkManager
systemctl is-active systemd-hostnamed
The first command should print NEW_HOSTNAME; both service checks should print active on a normally running VPS:
web01.example.com
active
active
NetworkManager’s Rocky Linux 9 role and configuration model are covered in the Rocky Linux 9 network configuration guide. You do not need to restart either service just because hostnamectl succeeded.
Some VPS images also run cloud-init and may restore a provider-supplied name during boot. Check for it before changing configuration:
if command -v cloud-init >/dev/null 2>&1; then
cloud-init status --long
else
echo "cloud-init is not installed"
fi
If the hostname remains correct after a reboot, leave cloud-init unchanged. If it restores the old name, preserve the local setting and apply the name again:
sudo install -d -m 0755 /etc/cloud/cloud.cfg.d
printf '%s\n' 'preserve_hostname: true' | sudo tee /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg
sudo hostnamectl set-hostname NEW_HOSTNAME
Use the same NEW_HOSTNAME value from Step 2. The tee command should echo preserve_hostname: true. If provider metadata continues to overwrite the name, update that provisioning value as well.
Step 5: Refresh SSH and applications
The kernel uses the new name immediately, but an existing shell prompt or long-running application may have read the old name when it started.
Confirm the active name and the persistent file, then open a second SSH session:
hostname
hostnamectl status --static
cat /etc/hostname
All three lines should contain the new hostname:
web01.example.com
web01.example.com
web01.example.com
Reconnect over SSH to refresh the prompt before closing the original session. Restart a specific application only if it caches the hostname at startup:
sudo systemctl restart YOUR_SERVICE
Replace YOUR_SERVICE with the actual unit name. A hostname change alone does not require restarting networking or rebooting the VPS.
Verify the new Rocky Linux 9 hostname
Run this final set to validate the static value, short form, FQDN lookup, and NetworkManager’s view:
hostnamectl status --static
hostname -s
hostname --fqdn
nmcli general hostname
getent hosts NEW_HOSTNAME
For web01.example.com, the first four lines should be the FQDN, web01, the FQDN again, and the FQDN again. The last line should return the address and aliases from /etc/hosts or the expected DNS answer. When those values agree, Rocky Linux 9 is using the new hostname locally.
Troubleshooting
hostnamectl: invalid hostname
The static name contains a character Rocky Linux rejects, usually a space, underscore, or trailing dot. Use lowercase letters, numbers, hyphens, and dots in NEW_HOSTNAME; put spaces only in the separate pretty hostname. Check that every FQDN label starts and ends with a letter or number.
hostname --fqdn returns localhost or the previous name
The operating-system name changed, but /etc/hosts still has a stale machine entry or no mapping for the new FQDN. Keep the localhost records, replace the old server line with SERVER_IP NEW_HOSTNAME NEW_SHORT_NAME, and run getent hosts NEW_HOSTNAME again. If only remote systems see the old result, update public DNS instead of this file.
nmcli general hostname shows a different value
NetworkManager or provisioning metadata is still supplying another hostname. Re-run sudo hostnamectl set-hostname NEW_HOSTNAME, check the output of nmcli general hostname, and inspect cloud-init as shown in Step 4. Do not edit NetworkManager connection files by hand for this change.
The old hostname returns after reboot
Cloud-init, DHCP metadata, or the VPS image’s provisioning settings are applying a name at boot. If cloud-init is responsible, add 99-preserve-hostname.cfg from Step 4 and set the name again; if the provider still wins, change the hostname in its provisioning settings.
Next steps
- Create and test the public A or AAAA record from another network if the name will be used for TLS, mail, monitoring, or a website.
- Request a matching PTR record through the service that manages the VPS address when reverse DNS matters.
- Update SSH inventory, monitoring, backup jobs, certificates, and configuration-management records that still contain the old name.
FAQ
Does changing the hostname change my Rocky Linux VPS IP address?
No. The procedure changes the guest operating system’s identity, not its network interface, route, or assigned address. DNS records still need to point the chosen name at the correct IP address.
Do I need nmtui to rename a Rocky Linux 9 server?
No. hostnamectl is sufficient for a normal SSH-based rename, and nmcli general hostname lets you check NetworkManager’s view. nmtui is an optional text menu for broader NetworkManager configuration, not a requirement for this task.
Can the hostname contain an underscore?
Do not use underscores in the static hostname. Choose DNS-safe labels made from letters, numbers, hyphens, and dots; use the separate pretty hostname if you need spaces or a descriptive label.
Why does my current SSH prompt still show the old name?
Shell prompts are often assembled when the session starts, so an existing terminal can retain the earlier value even though the system has changed. Open a new SSH session, and restart only applications that cache the hostname at launch.