What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

On a systemd-based Linux VPS, set the persistent name with hostnamectl, then make sure the same name resolves locally through the appropriate /etc/hosts entry. This changes the server’s local identity immediately and across reboots, but it does not create a public DNS record. The procedure below covers naming, file formats, verification, manual fallback, and the common “sudo: unable to resolve host” error.

What you need to change

A Linux VPS hostname has two related parts: the persistent local name stored in /etc/hostname, and local name-to-address mappings in /etc/hosts. On distributions that use systemd, the supported interface is hostnamectl. It writes the static hostname to /etc/hostname and applies it to the running system, so a reboot is normally unnecessary.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Choose a DNS-compatible name before editing anything. A short label such as web-01 is the conservative value for /etc/hostname; a fully qualified domain name (FQDN) such as web-01.example.com can also be used when your software and naming policy expect one. Systemd documents DNS-style characters and a Linux maximum of 64 characters for static and transient hostnames. Avoid spaces and arbitrary punctuation.

Choose a short hostname or an FQDN

Both forms can be valid, but they solve slightly different operational needs. Decide before changing the machine so every local file, automation rule, and DNS record uses the same convention.

Static value Advantages Check before choosing
web-01 One DNS-style label; compatible with software that expects a simple host name; easy to read in prompts and logs. Make sure the FQDN you intend to use is supplied through /etc/hosts and, when needed, public DNS.
web-01.example.com Expresses the server’s complete domain identity and is convenient for applications that expect an FQDN. Confirm that your local hosts entry and any public A or AAAA record use this exact domain and short-name alias.

Provider automation is another constraint. Some VPS images or initialization systems expect a particular naming convention. If the name returns after a reboot, check the image’s cloud-init or provider initialization policy rather than repeatedly editing the files by hand.

Inspect the current hostname first

Run these commands with an account that can read the local configuration. Use sudo only where it is required:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
hostnamectl status
hostname
cat /etc/hostname
getent hosts "$(hostname)"

These checks show the hostname systemd considers active, the kernel’s current short value, the persistent file contents, and whether the current name resolves through the system’s configured name-service path. Save the output if you are changing a production server; it makes it easier to identify a provider overwrite or an incorrect hosts mapping later.

Set the hostname with hostnamectl

1. Apply the persistent and running value

Replace the example with your selected name:

sudo hostnamectl set-hostname web-01.example.com

hostnamectl manages static, transient, and pretty hostnames. This command sets the static hostname, persists it in /etc/hostname, and updates the running hostname through systemd. A valid static value normally takes precedence over a transient value supplied by DHCP or another network component.

2. Confirm the file format

Open the file as root if you need to inspect it:

sudo cat /etc/hostname

It should contain one newline-terminated string: the intended hostname and nothing else. Do not put an address, aliases, comments, or multiple lines in this file. Systemd reads this file during boot and uses it to call the kernel’s sethostname(2) operation.

3. Align /etc/hosts with the new name

/etc/hosts is a local name-resolution database. It does not publish a record to the Internet. Edit it with your preferred root-capable editor, for example:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
sudo nano /etc/hosts

For a VPS with a known static IPv4 address, put the FQDN first and the short name second on the line for that address:

203.0.113.10 web-01.example.com web-01

Replace 203.0.113.10 with the VPS’s actual local address. Keep the existing localhost entries, including the IPv4 line beginning with 127.0.0.1 and the IPv6 localhost entries. Do not remove unrelated mappings that your image or applications deliberately installed.

Debian-style systems using DHCP may instead map the machine name to loopback address 127.0.1.1:

127.0.1.1 web-01.example.com web-01

Use the convention already documented or established by your distribution image. The important relationship is address, FQDN, then short name, with both names on the same local-address line.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Understand what each file does

/etc/hostname: the persistent local identity

The /etc/hostname file configures the name of the local system. It is read during boot on systemd-based distributions, and its value is the static hostname. Because it is a single value, aliases do not belong there; aliases are handled by name-resolution sources such as /etc/hosts or DNS.

/etc/hosts: local resolution, not public DNS

The hosts file lets programs on the VPS translate a name into an address without querying a public DNS server. Putting the FQDN and short name on the same line lets commands and software resolve either spelling locally. It is especially useful for the system’s own name, which is why a mismatch can affect sudo, mail software, monitoring agents, and service startup even when Internet DNS is working.

Static, transient, and pretty hostnames

Systemd distinguishes three hostname types:

  • Static: the durable value stored in /etc/hostname; set this for a VPS identity that must survive reboot.
  • Transient: a value received from network configuration such as DHCP; it can change when networking is reconfigured.
  • Pretty: a descriptive human-facing label that may contain characters unsuitable for a DNS name; it is not a substitute for a valid static hostname.

A valid static hostname normally wins over a transient value. However, cloud-init, NetworkManager, DHCP hooks, or provider-specific image scripts can rewrite settings. Persistence must therefore be checked after the next planned reboot.

Verify the result without rebooting

Run the complete verification set after editing both files:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
hostnamectl status
hostname
hostname -f
cat /etc/hostname
getent hosts web-01.example.com

Interpret the results as follows:

  • hostnamectl status should show the intended static hostname.
  • hostname should print the running short value or the value your distribution presents as the current hostname.
  • hostname -f should return the FQDN when the local mapping and resolver configuration support it.
  • cat /etc/hostname should show exactly one intended name.
  • getent hosts web-01.example.com should return the address from your local mapping or another configured resolver source.

These commands let you detect an inconsistency immediately. A reboot is not required merely to activate a hostname set through hostnamectl; reboot only when it is part of your normal change procedure or when you need to test boot-time and provider behavior.

Manual fallback when hostnamectl is unavailable

Minimal images, non-systemd systems, or recovery environments may not provide hostnamectl. In that case, edit the persistent file as root so it contains exactly the intended name, update the corresponding /etc/hosts line, and apply the running value with the legacy command if it exists:

sudo hostname NEWNAME

For example, the file can contain web-01.example.com, while the hosts entry contains the address followed by web-01.example.com web-01. The exact command and boot behavior vary with the init system. If a usable hostname command is not available, rebooting allows systemd (where present) to read /etc/hostname. Treat this as a fallback; the documented systemd path remains hostnamectl.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Fix “sudo: unable to resolve host” after a rename

This warning usually means the current hostname and the local hosts database disagree. The hostname change itself may have succeeded; sudo is failing while trying to resolve the machine’s own name.

Check the active and persistent names

hostname
hostnamectl status
cat /etc/hostname

Compare the names character for character, including hyphens and domain components. Decide whether the static value should be the short name or FQDN, then use that decision consistently.

Correct the local-address line

Ensure the line for the VPS’s local address lists the FQDN first and short name second. For example:

203.0.113.10 web-01.example.com web-01

On a Debian DHCP-style layout, the equivalent may be:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
127.0.1.1 web-01.example.com web-01

Leave 127.0.0.1 localhost and IPv6 localhost lines intact. Save the file and test resolution:

getent hosts web-01.example.com
getent hosts web-01

Once either spelling resolves to the intended local address, start a new sudo command and confirm that the warning is gone. If resolution still fails, inspect the image’s resolver configuration and any automation that rewrites /etc/hosts.

Troubleshoot common hostname problems

The name changes back after reboot

First verify that /etc/hostname contains the desired value. If it does, an initialization system may be applying a transient or provider-defined name during boot. Review the VPS image’s cloud-init and provider policy for hostname management, then change the authoritative setting there or disable the overwrite according to that image’s documentation. A static value normally has priority, but an image policy can rewrite files or settings after startup.

hostname -f returns the wrong value or fails

The command depends on successful name resolution. Check that the FQDN appears on the correct local-address line in /etc/hosts, that the short alias is present when needed, and that there is only one conflicting local mapping for the machine. Then rerun getent hosts for both forms before trying hostname -f again.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

The hostname contains invalid characters

Replace spaces, underscores, and arbitrary punctuation with DNS-style letters, digits, and hyphens. Keep the complete static or transient name at or below 64 characters. If an application needs a friendly descriptive label, use a pretty hostname separately rather than placing spaces in the static value.

Other machines cannot resolve the new FQDN

Local hosts-file edits affect this VPS only. A hostname change does not create or modify a public DNS record. For Internet resolution, create or update the appropriate DNS A record for IPv4 and/or AAAA record for IPv6 with your DNS provider. Continue to keep the local /etc/hosts mapping correct even after public DNS is available, because local services may resolve the name before querying external DNS.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Short name versus FQDN: a practical decision framework

Use these questions when the choice is not obvious:

  1. Does software require one DNS label? If yes, a short static value such as web-01 is the safer compatibility choice.
  2. Will local and public DNS provide the FQDN? If the server is identified primarily by a domain name and you control the matching records, using web-01.example.com as the static value can make logs and service configuration clearer.
  3. Does the provider expect a naming convention? Check the VPS image and automation policy before selecting either form; provider scripts can rewrite the hostname.
  4. Can you maintain both local aliases? Whichever static value you choose, put the FQDN and short name together on the correct /etc/hosts line when both spellings are used by your software.

Editing safely on a remote VPS

A hostname change rarely interrupts networking, but an inconsistent hosts file can complicate administration. Use a controlled sequence:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  1. Keep an existing SSH session open while making the change so you have a recovery path.
  2. Record the current output of hostnamectl status, hostname, and cat /etc/hostname.
  3. Choose one valid name no longer than 64 characters and decide whether it is short or fully qualified.
  4. Run hostnamectl set-hostname with sudo on systemd distributions.
  5. Edit /etc/hosts so the local address maps to FQDN then short name, while preserving localhost entries.
  6. Run the verification commands and test both the FQDN and short alias with getent hosts.
  7. After the next planned reboot, repeat the checks to detect cloud-init or provider rewrites.

Quick reference

Task Command or file Expected result
View the current systemd hostname state hostnamectl status Shows static, transient, and pretty values when present.
Set a persistent hostname on systemd sudo hostnamectl set-hostname web-01.example.com Updates the running name and writes the static value to /etc/hostname.
Inspect the persistent value cat /etc/hostname One newline-terminated hostname string.
Map names locally /etc/hosts Address followed by FQDN and short-name aliases.
Test local resolution getent hosts web-01.example.com Returns the address selected by the system resolver.
Check the canonical name hostname -f Returns the FQDN when local or DNS resolution is correctly configured.

Final checklist

  • The static name uses DNS-compatible characters and is no longer than 64 characters.
  • /etc/hostname contains exactly one intended value.
  • /etc/hosts maps the local address to FQDN first and short name second.
  • 127.0.0.1 localhost and IPv6 localhost entries remain present.
  • hostnamectl status, hostname, and hostname -f show coherent values.
  • getent hosts resolves both the FQDN and short name as required.
  • Any public A or AAAA record has been updated separately.
  • A post-reboot check confirms that cloud-init, DHCP, NetworkManager, or provider automation has not overwritten the setting.

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.