Change hostname in Rocky Linux 8 with hostnamectl, then make the new name resolve locally through /etc/hosts. The active name changes in seconds, and you can finish the configuration in about five minutes without rebooting the VPS.
Prerequisites
- A VPS running Rocky Linux 8.
- An SSH connection as
root, or a normal account with workingsudoprivileges. - A short hostname such as
web01, or an FQDN such asweb01.example.com; use lowercase letters, numbers, hyphens, and dots. - The VPS’s primary IP address and access to your DNS provider if other computers must resolve the FQDN.
- No special RAM or disk allocation is required for a hostname change.
- A VPSLake Linux VPS if you need a Rocky Linux server with root access.
Step 1: Record Rocky Linux 8’s current name
Check the different hostname types first so you can distinguish the saved system name from a temporary value supplied by DHCP or provisioning.
cat /etc/rocky-release
hostnamectl status --static
hostnamectl status --pretty
hostnamectl status --transient
nmcli general hostname
The release command should identify Rocky Linux 8. A blank pretty or transient value is normal on some minimal images. Example output might look like this:
Rocky Linux release 8.x (Green Obsidian)
old-web.example.com
old-web.example.com
old-web.example.com
The static hostname is the durable machine identity. The pretty hostname is only an optional human-readable label, while the transient value can come from the network.
Step 2: Set the persistent hostname
Use hostnamectl so systemd updates the active kernel name and Rocky Linux’s /etc/hostname together.
sudo hostnamectl set-hostname NEW_HOSTNAME
hostnamectl status --static
cat /etc/hostname
Replace NEW_HOSTNAME with a short name such as web01 or an FQDN such as web01.example.com. Do not use spaces, underscores, or a trailing dot. The command normally returns no success message; both checks should print the new name:
web01.example.com
web01.example.com
If you want a descriptive label for administrators, set a pretty hostname separately. It may contain spaces and does not replace the operational name:
sudo hostnamectl set-hostname "NEW_PRETTY_NAME" --pretty
hostnamectl status --pretty
Replace NEW_PRETTY_NAME with a label such as Production web server. The hostnamectl procedure in the RHEL 8 networking documentation applies to Rocky Linux 8’s compatible systemd and NetworkManager stack.
Step 3: Make the new name resolve on the VPS
Changing the system name does not create DNS records, so add a local hosts entry when the server must resolve its own FQDN before public DNS is ready or whenever local-only resolution is sufficient.
Find the IPv4 address used by the default route:
ip -4 route get 1.1.1.1
Use the address shown after src as SERVER_IP. Then open the hosts file:
sudo nano /etc/hosts
Keep the existing localhost and IPv6 entries. Add one line for the server, replacing the uppercase placeholders:
SERVER_IP NEW_HOSTNAME NEW_SHORT_NAME
For web01.example.com, NEW_SHORT_NAME is web01. If you chose only a short hostname, use SERVER_IP web01 instead. Save the file, then test the local resolver:
getent hosts NEW_HOSTNAME
hostname --fqdn
hostname -s
Successful output for an FQDN resembles:
203.0.113.40 web01.example.com web01
web01.example.com
web01
The hosts file changes this VPS only. Create an A or AAAA record with your DNS provider if the name must work from other machines. A forward DNS record also does not automatically create reverse DNS.
Step 4: Apply the name through NetworkManager when needed
Rocky Linux 8 commonly uses NetworkManager, so nmcli gives you a second supported way to inspect or set the system hostname and helps reveal a provisioning conflict.
Display the current NetworkManager value and set it if it differs from the value in Step 2:
nmcli general hostname
sudo nmcli general hostname NEW_HOSTNAME
nmcli general hostname
Replace NEW_HOSTNAME with the same static name used earlier. The final command should return it:
web01.example.com
For a menu-driven terminal interface, install the optional package and open it:
sudo dnf install -y NetworkManager-tui
sudo nmtui
Choose Set system hostname, enter the new value, select OK, and choose Quit. nmtui changes the system hostname but does not edit /etc/hosts or publish DNS, so keep the local mapping from Step 3 when you need it.
Step 5: Prevent an image from restoring the old name
Some VPS images use cloud-init or provider metadata at boot. Check for cloud-init before changing its configuration; installing it is not necessary for an ordinary hostname update.
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 survives a reboot, no change is needed. If cloud-init restores the previous name, create a drop-in that preserves the local value and set the name again:
sudo install -d -m 0755 /etc/cloud/cloud.cfg.d
sudo tee /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg >/dev/null <<'EOF'
preserve_hostname: true
EOF
sudo hostnamectl set-hostname NEW_HOSTNAME
Replace NEW_HOSTNAME with the desired name. If the provider continues to apply a value from instance metadata, change that provisioning value as well; otherwise it can overwrite the local configuration on a later boot.
Verify the new Rocky Linux 8 hostname
Run the checks below together. They confirm the static value, NetworkManager’s view, the persistent file, and the resolver result.
hostnamectl status --static
nmcli general hostname
cat /etc/hostname
hostname --fqdn
getent hosts NEW_HOSTNAME
For an FQDN, the first three lines should show the same name, hostname --fqdn should return that FQDN, and getent should return the expected server address. If the pretty hostname was set, check it separately:
hostnamectl status --pretty
systemctl is-active systemd-hostnamed
Expected output is similar to:
Production web server
active
The change is active immediately. Reconnect over SSH to refresh a prompt that still displays the old name, and restart only an application that reads the hostname once during startup.
Troubleshooting
hostnamectl: invalid hostname
The static value contains a space, underscore, trailing dot, or another unsupported character. Use lowercase letters, digits, hyphens, and dots for NEW_HOSTNAME; put spaces only in the separate --pretty value. Check that each FQDN label starts and ends with a letter or number.
hostname --fqdn returns localhost or the old name
The system hostname changed, but /etc/hosts still has a stale mapping or no entry for the new name. Keep the existing 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 value, update public DNS rather than editing this VPS’s hosts file.
The old hostname comes back after reboot
Cloud-init, DHCP, or provider metadata is applying a name during boot. Check cloud-init status --long, add 99-preserve-hostname.cfg from Step 5 if cloud-init is responsible, and update the hostname in the VPS provisioning settings if metadata still wins.
nmcli: command not found or nmtui: command not found
hostnamectl is sufficient and is normally already installed. On a minimal Rocky Linux image, install the missing NetworkManager tools with sudo dnf install -y NetworkManager NetworkManager-tui, then retry the command.
Next steps
- Add the public A or AAAA record and test it from a different network if the FQDN will be used for websites, certificates, mail, or monitoring.
- Request a matching PTR record through the system that manages the VPS address when reverse DNS matters.
- Update SSH inventory, monitoring, backups, certificates, and configuration-management records that still contain the old name.
FAQ
Do I need to reboot Rocky Linux 8 after changing the hostname?
No. hostnamectl updates the active hostname and /etc/hostname immediately. Reconnect to refresh your shell prompt, and restart only a service that caches the name at startup.
Is nmcli better than hostnamectl on Rocky Linux 8?
Both are supported on Rocky Linux 8. hostnamectl is the simplest choice for a one-time rename, while nmcli general hostname is useful for checking what NetworkManager currently stores on a server managed through NetworkManager.
Can I change the hostname without changing the server’s IP address?
Yes. A hostname change does not alter network interfaces, routes, or IP addresses. If the new name is an FQDN, create its DNS record separately and make sure local resolution matches it.
What is the difference between a static, transient, and pretty hostname?
The static hostname is the persistent machine name stored in /etc/hostname. A transient name may be supplied by the network, while a pretty hostname is an optional descriptive label that can contain spaces and is not used as the server’s DNS identity.