On a systemd-based Linux server, run sudo hostnamectl set-hostname NEW-NAME, update the server’s 127.0.1.1 entry in /etc/hosts, then verify with hostname -f. The command changes the running kernel name, the persistent /etc/hostname file, and systemd’s hostname state without requiring a reboot. On a cloud virtual machine, configure cloud-init as well, or the provider’s name can replace yours at the next boot.
Antihost offers Linux VPS plans with full root access, unlimited traffic on a 1 Gbps port and a 14-day free trial.
What changes when you rename a Linux server?
A Linux host can have several related names. The name most administrators mean is the static hostname: the persistent DNS-style name stored in /etc/hostname. On a systemd distribution, hostnamectl set-hostname also updates the kernel hostname (the value returned by uname -n) and systemd-hostnamed’s state. The change is immediate; the systemd procedure does not require a reboot or a service restart.
As an Amazon Associate I earn from qualifying purchases.
The hostname is separate from your server’s IP address, DNS records, SSH keys, users and application configuration. Renaming the host does not automatically create a public DNS record or change a cloud provider’s inventory label. It changes the local name by which the operating system identifies itself.
Check the name you want to use first
Choose a DNS-style name rather than a display phrase. Systemd limits static and transient hostnames to 64 characters. Avoid spaces and other free-form wording; a name such as game-node-01 is appropriate, while Production Game Server is not a static hostname.
#1 Best Overall
- Use one intended spelling everywhere. Hostnames are case-sensitive in some tools and are commonly normalized to lowercase for DNS use.
- Decide whether the name is short or fully qualified. A short name such as
game-node-01can be mapped to a fully qualified domain name in/etc/hosts. - Keep the localhost records intact. You will edit only the host-specific entry, not the
localhostmappings. - Check whether cloud-init controls the machine. A cloud VM may receive a provider-supplied local-hostname during boot, which can override a local change unless you preserve the hostname in cloud-init.
Standard systemd procedure
1. Set the persistent hostname
Run this as a user with administrative privileges:
sudo hostnamectl set-hostname NEW-NAME
Replace NEW-NAME with the exact name you selected, for example:
sudo hostnamectl set-hostname game-node-01
Guides also document the equivalent form sudo hostnamectl hostname NEW-NAME. The ordinary set-hostname form is clearer because it states that you are assigning a new value. With no type flag, systemd changes the static, transient and pretty hostname values together when applicable. For a server, the important result is the new static name in /etc/hostname.
Free tools Windows power users keep installed
One-click scans. No signup required.
2. Correct the local host mapping
hostnamectl does not edit /etc/hosts. Open that file with an editor available on your distribution, for example:
sudo nano /etc/hosts
Find the line that maps the local machine, commonly the 127.0.1.1 line, and replace only the old host name. Leave the localhost records in place. A typical result is:
127.0.0.1 localhost
127.0.1.1 game-node-01
::1 localhost ip6-localhost ip6-loopback
Some installations put a fully qualified name and its short alias on the same line, such as 127.0.1.1 game-node-01.example.net game-node-01. Preserve that arrangement if it is already used and change both names consistently. Do not remove the 127.0.0.1 or ::1 localhost entries.
This mapping matters for local name resolution. If it still contains the old name, commands that resolve the machine’s own hostname can fail or print a warning such as sudo: unable to resolve host. That warning is a symptom of inconsistent local naming; it is not fixed by rerunning hostnamectl alone.
3. Verify every relevant view of the name
Run the following checks after saving both files:
hostname
hostnamectl --static
cat /etc/hostname
hostname -f
hostnameshould print the short name you selected.hostnamectl --staticshould print the persistent static value.cat /etc/hostnameconfirms what will be read as the persistent name at boot.hostname -ftests the fully qualified result through local name resolution. It can report an error when/etc/hostsdoes not map the new name correctly.
For a final kernel-level check, uname -n should match the running hostname. No reboot is needed for these values to update on a systemd host.
Hostname types in systemd
Systemd distinguishes three hostname types. Knowing which one changed prevents confusion when a network service appears to restore a different name.
| Type | Where it comes from | Persistence and purpose |
|---|---|---|
| Static | /etc/hostname |
Persistent administrator-assigned name. It has higher priority than a transient name and is the normal choice for a server. |
| Transient | Network configuration such as DHCP or mDNS | May change when networking starts. It has lower priority than the static name. |
| Pretty | /etc/machine-info, alongside chassis and icon information |
A free-form display label for user interfaces, not the DNS-style identity used by server tools. |
Running hostnamectl set-hostname NAME without a selector changes all applicable hostname types. To change only one type, use the corresponding option: hostnamectl --static set-hostname NAME, hostnamectl --transient set-hostname NAME or hostnamectl --pretty set-hostname NAME. Static and transient values must be DNS-style names no longer than 64 characters; a pretty name is intended for display and can contain spaces.
The priority order is static, then transient, then a fallback name. Therefore, setting a static hostname normally prevents a DHCP-provided transient value from becoming the effective name. If the static value is correct but a tool still displays another label, inspect whether that tool is showing the pretty name or a provider-specific inventory name.
Cloud virtual machines: prevent a reset on reboot
Cloud-init commonly manages hostnames on virtual machines. Its documented default is preserve_hostname: false. In that mode, cloud-init can update /etc/hostname at each boot from the cloud-provided local-hostname. A name set with hostnamectl can therefore be replaced when the VM restarts unless cloud-init is configured to preserve it.
Set the preservation option
Add this setting to the cloud-init configuration or user-data mechanism used by your image:
Rank #4
#cloud-config
preserve_hostname: true
The exact configuration location is image- and provider-dependent, so confirm where your cloud platform reads cloud-init settings. The important value is true; it tells cloud-init not to change the hostname during its normal boot processing.
Apply and test in the right order
- Set
preserve_hostname: truethrough the VM’s supported cloud-init configuration path. - Run
hostnamectl set-hostname NEW-NAME. - Update the
127.0.1.1mapping in/etc/hosts. - Run the verification commands and record the expected output.
- Reboot the VM deliberately, then run the same checks again. If the name changes back, cloud-init or another image-specific startup component is still authoritative.
Cloud-init also has a create_hostname_file option. Setting it to false stops cloud-init from creating /etc/hostname on an image that lacks that file; in many cloud environments the hostname then comes from DHCP. That option is not normally needed when you are renaming an existing installation with an /etc/hostname file, and it is not a substitute for preserve_hostname: true when you need to retain an administrator-selected name.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →Cloud images differ. Some documentation reports that manually editing /etc/hostname after boot can stop cloud-init from changing it, while other images continue to apply provider metadata. Treat that behavior as image-specific: the supported preservation setting and a reboot test are the reliable way to confirm persistence.
Troubleshooting common failures
hostnamectl is not available
hostnamectl is a systemd utility. On a distribution or minimal image that does not use systemd, use that distribution’s hostname procedure or edit the persistent hostname file according to its documentation. Do not assume that a systemd command will exist merely because the kernel is Linux. The procedure in this guide applies to systemd-based distributions.
Best Value
The command runs but the old name remains
Check the three independent views: hostname for the running kernel name, hostnamectl --static for systemd’s static value and cat /etc/hostname for the file that will persist. If only one differs, you are looking at a different hostname type or a file changed by another startup service. Also check that you used administrative privileges and that the command completed without an error.
hostname -f fails
This usually points to local name resolution rather than a failed hostname assignment. Reopen /etc/hosts, make sure the 127.0.1.1 line contains the new short name (and the intended fully qualified name, if used), and keep the localhost entries intact. Run hostname -f again after saving. A short hostname can still be valid even when no fully qualified mapping has been configured, so distinguish an FQDN lookup problem from the static name itself.
Windows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallCrashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutesudo reports “unable to resolve host”
Compare hostname with the name on the host-specific line in /etc/hosts. Correct the mismatch; changing /etc/hostname alone does not update /etc/hosts. Because the warning is emitted while resolving the local machine, external DNS changes are not the first place to look.
The name returns after a reboot
On a cloud VM, inspect cloud-init first. Its default policy allows the provider’s local-hostname to update /etc/hostname during boot. Set preserve_hostname: true, apply the setting through the image’s supported configuration path and test another reboot. On a self-managed system, compare cat /etc/hostname before and after reboot to determine whether the file itself changed or only the running/transient value did.
The new name is rejected
Static and transient names must be DNS-style and no more than 64 characters. Replace spaces or display text with a DNS-style server name and try again. If you need a human-readable label containing spaces, set a pretty hostname separately rather than using it as the static identity.
Editing /etc/hostname directly
Directly editing /etc/hostname can establish a persistent name, and tutorials document it as an alternative. It is less comprehensive than hostnamectl: the preferred systemd command updates the running kernel value, the file and systemd-hostnamed state in one operation, while a file-only edit does not itself demonstrate that all three views agree. If you must edit the file directly on a minimal or non-systemd installation, use the distribution’s own command or startup procedure to apply the running hostname, then verify with hostname, cat /etc/hostname and hostname -f.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Scan for outdated or missing drivers - takes under a minute3Clear out junk files and repair common Windows errorsQuick Recap
A compact checklist for production changes
- Choose a DNS-style static name of 64 characters or fewer.
- Confirm whether the server is systemd-based and whether cloud-init manages its hostname.
- Run
sudo hostnamectl set-hostname NEW-NAME. - Change the host-specific
127.0.1.1entry in/etc/hosts; preserve localhost mappings. - Verify
hostname,hostnamectl --static,cat /etc/hostnameandhostname -f. - On a cloud VM, set
preserve_hostname: truebefore testing a reboot. - After the reboot test, investigate any difference by checking which hostname type or startup manager supplied the unexpected value.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




