Change hostname on Rocky Linux 10 with hostnamectl, then make the new name resolve locally through /etc/hosts. The update takes about five minutes and becomes active immediately, with no reboot needed for the operating system itself.
Prerequisites
- A VPS running Rocky Linux 10, preferably updated to the latest available 10.x release.
- An SSH session as
root, or a normal account with workingsudoprivileges. - A new short hostname such as
api01, or an FQDN such asapi01.example.com; use lowercase letters, numbers, hyphens, and dots. - The server address shown by its default route, plus access to your DNS provider if other machines must resolve the FQDN.
- A VPSLake Linux VPS if you need a Rocky Linux server with root access.
Step 1: Record the current name and server address
Capture the existing identity before changing it so you can distinguish the saved hostname from a transient value supplied by DHCP or provisioning.
cat /etc/rocky-release
hostnamectl status --static
hostnamectl status --transient
hostnamectl status --pretty
ip -4 route get 1.1.1.1
Rocky Linux 10 should appear in the release output. The static or pretty fields can be empty on a newly provisioned VPS. In the route result, note the value after src; that is the address to use as SERVER_IP in Step 3.
Rocky Linux release 10.2 (Red Quartz)
old-api.example.com
old-api
1.1.1.1 via 203.0.113.1 dev ens3 src 203.0.113.40 uid 0
The example IP is documentation-only. Use the actual address returned by your VPS, not 203.0.113.40.
Step 2: Set the persistent Rocky Linux hostname
Use hostnamectl because it updates the running system and the persistent /etc/hostname value through systemd-hostnamed.
sudo hostnamectl set-hostname NEW_HOSTNAME
hostnamectl status --static
cat /etc/hostname
Replace NEW_HOSTNAME with the same short name or FQDN you selected. Do not include spaces, underscores, or a trailing dot. For an FQDN, keep each label within DNS hostname limits and make sure the complete name is no longer than 64 characters for the system hostname.
The setter normally produces no output. The two checks should print the new value:
api01.example.com
api01.example.com
Rocky’s hostname and networking documentation uses hostnamectl for this change. You do not need to edit /etc/hostname by hand or restart systemd-hostnamed afterward.
If you prefer to confirm the value through NetworkManager, run:
nmcli general hostname
It should return the new static hostname. This command reads the setting; it is not a replacement for the hostnamectl command above.
Step 3: Add a local hosts entry
The static name and name resolution are separate: an /etc/hosts mapping lets local services find the VPS by its new FQDN before public DNS is available.
Open the file without disturbing the existing loopback and IPv6 records:
sudo nano /etc/hosts
Keep lines for localhost and ::1. Replace an old machine-name line if one exists, or add this entry using the routed address from Step 1:
SERVER_IP NEW_HOSTNAME NEW_SHORT_NAME
For api01.example.com with short name api01, the finished line might be:
203.0.113.40 api01.example.com api01
Replace SERVER_IP, NEW_HOSTNAME, and NEW_SHORT_NAME with your values before saving. If you chose only api01, use SERVER_IP api01. Keep one current machine-name line instead of leaving old and new entries together.
Check the local resolver immediately:
getent hosts NEW_HOSTNAME
hostname --fqdn
Successful output should contain the server address and the new FQDN:
203.0.113.40 api01.example.com api01
api01.example.com
This edit affects only the VPS. Create an A record for IPv4 or an AAAA record for IPv6 if the name must resolve from the public internet. Forward DNS and reverse DNS/PTR are separate settings.
Step 4: Check for hostname automation
Some cloud images run cloud-init or provider metadata at boot. Check for that before assuming the local change will survive a reboot.
if command -v cloud-init >/dev/null 2>&1; then
cloud-init status --long
else
echo "cloud-init is not installed"
fi
If cloud-init is absent, continue to Step 5. If it is installed and later restores the old name, create a drop-in that preserves the locally selected hostname, then set 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
The file-writing command should echo:
preserve_hostname: true
The cloud-init module reference documents preserve_hostname. If the VPS image still applies a provider-defined name, change that value in the provisioning or control-panel settings too.
Step 5: Refresh shells and applications that cache the name
The kernel and hostname service use the new name straight away, but an existing SSH prompt or long-running application may have loaded the old value when it started.
Run a final service and file check:
systemctl is-active systemd-hostnamed
hostname
cat /etc/hostname
Expected output is:
active
api01.example.com
api01.example.com
Open a second SSH session to refresh your shell prompt before closing the first one. Restart only a specific application that documents reading the hostname at startup:
sudo systemctl restart YOUR_SERVICE
Replace YOUR_SERVICE with a real unit name. Do not restart NetworkManager just to apply this hostname change, and do not reboot unless another maintenance task requires it.
Verify the new hostname
Use these checks together to validate the static setting, short form, FQDN resolution, and hosts-file entry:
hostnamectl status --static
hostname -s
hostname --fqdn
getent hosts NEW_HOSTNAME
grep -nE '(^|[[:space:]])(OLD_HOSTNAME|NEW_HOSTNAME)([[:space:]]|$)' /etc/hosts
For api01.example.com, the first three lines should be api01.example.com, api01, and api01.example.com. The getent result should show the intended address, and the final search should show the current hosts-file entry rather than an old name. Replace both uppercase hostname placeholders before running the grep command.
Troubleshooting
hostnamectl: invalid hostname
The proposed static name contains an unsupported character, commonly a space, underscore, or trailing dot. Use lowercase letters, numbers, hyphens, and dots for NEW_HOSTNAME; reserve spaces for a separate pretty hostname. Check that every FQDN label starts and ends with a letter or number.
hostname --fqdn returns localhost or the old name
The operating-system name changed, but /etc/hosts still has a stale machine entry or no entry for the new FQDN. Keep the localhost records, replace the old machine-name mapping with SERVER_IP NEW_HOSTNAME NEW_SHORT_NAME, and rerun getent hosts NEW_HOSTNAME. If only remote computers see the old result, update public DNS instead.
The old hostname comes back after reboot
Cloud-init, DHCP-provided metadata, or the VPS image is rewriting the name during startup. Check cloud-init status --long, add preserve_hostname: true when cloud-init is responsible, and update the provider-side hostname if its metadata continues to win.
The SSH prompt still shows the previous name
The prompt was created before the change and may contain the old value even though hostname is current. Start a new SSH session; restart an individual service only if it caches the hostname at launch.
Next steps
- Add and test public A/AAAA records from a separate network when the FQDN is used for TLS, mail, monitoring, or a website.
- Request a matching PTR record through the service that manages the VPS IP when reverse DNS matters.
- Update SSH inventory, monitoring, backups, certificates, and configuration-management records that still identify the server by its former name.
FAQ
Do I need to reboot Rocky Linux 10 after changing the hostname?
No. hostnamectl changes the active name and writes the persistent hostname immediately. Start a new SSH session to refresh the prompt, and restart only applications that read the name once during startup.
Should I use a short hostname or an FQDN?
A short name such as api01 is fine for a standalone VPS or private network. Use an FQDN when TLS, mail, monitoring, or multi-server administration needs a unique identity, and provide matching local or public DNS resolution.
Does changing the hostname rename the VPS in the client area?
No. This procedure changes the Rocky Linux guest operating system. A label in the VPSLake client area or Virtualizor is managed separately and does not automatically change when /etc/hostname changes.
Is editing /etc/hostname directly recommended?
No. Rocky Linux 10 uses systemd and NetworkManager to manage the system identity, so hostnamectl is the safer supported interface. Use /etc/hosts for local name resolution, but leave /etc/hostname to hostnamectl.