RDP Monster

Change Hostname Linux: hostnamectl, /etc/hosts and Reverse DNS

Change Hostname Linux: hostnamectl, /etc/hosts and Reverse DNS

Change the hostname with hostnamectl (Ubuntu, Debian, RHEL)

On any distribution that runs systemd (Ubuntu, Debian, RHEL, Rocky Linux, AlmaLinux, Fedora, openSUSE, Arch), the supported way to change the hostname is hostnamectl. It talks to systemd-hostnamed, which updates the running kernel value and writes /etc/hostname in one step, so the change applies immediately and survives a reboot. You need root or a sudo-capable account; if privilege escalation on your box is unclear, how sudo and su work on Ubuntu covers it.

sudo hostnamectl set-hostname server1.example.com
hostnamectl

systemd tracks three hostnames:

  • Static: stored in /etc/hostname, applied at boot. This is the one that matters.
  • Transient: the kernel's current value. DHCP or NetworkManager can override it at runtime, and the plain hostname NAME command only changes this one, so it is lost at reboot.
  • Pretty: a free-form label in /etc/machine-info, never used by the network.

Without flags, hostnamectl set-hostname sets all three; pass --static, --transient or --pretty to change only one. systemd 249 and later also accept the shorter hostnamectl hostname NAME.

Stick to lowercase letters, digits and hyphens, with each dot-separated label at most 63 characters; the kernel caps the whole name at 64 characters and hostnamectl rejects invalid characters for the static name. Use a fully qualified name such as server1.example.com when the machine will send mail or expose services. Your prompt keeps the old name until you open a new session, because bash reads the hostname once at startup.

Update /etc/hostname and /etc/hosts by hand

If you prefer editing files, or the system has no systemd, /etc/hostname is a single line holding the static name. Write the new name there, then apply it to the running kernel without rebooting:

echo "server1.example.com" | sudo tee /etc/hostname
sudo hostname -F /etc/hostname

The step people skip is /etc/hosts. Debian and Ubuntu installers add a line mapping 127.0.1.1 to the hostname chosen at install time; RHEL-family images usually add nothing. Either way, the new name must resolve locally, because sudo, hostname -f and several daemons look it up. Replace the old entry, FQDN first and short alias second (hostname -f reports the first name on the matching line); mapping the FQDN to the public IP instead of 127.0.1.1 is equally valid:

127.0.0.1     localhost
127.0.1.1     server1.example.com server1

Skip this and the symptom shows up on the very next command: sudo: unable to resolve host server1: Name or service not known on Ubuntu and Debian, and hostname: Name or service not known from hostname -f. Postfix derives its own identity from the hostname and logs warnings when it does not resolve.

On RHEL, Rocky and AlmaLinux with NetworkManager, sudo nmcli general hostname server1.example.com is an equivalent that goes through the same systemd-hostnamed service.

Stop cloud-init from reverting the hostname on reboot

This is the classic VPS problem: the hostname changes fine, then the next reboot puts the old one back. The cause is cloud-init, which most provider images boot with. Its set_hostname module runs once per instance, but update_hostname runs on every boot and rewrites /etc/hostname from the instance metadata (typically the label you gave the server when you ordered it). Confirm cloud-init is present with cloud-init status, then tell it to leave the hostname alone:

echo 'preserve_hostname: true' | sudo tee /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg

A drop-in under /etc/cloud/cloud.cfg.d/ is cleaner than editing /etc/cloud/cloud.cfg directly, because package upgrades will not overwrite it; flipping preserve_hostname: false to true in the main file works the same. Alternatively, rename the server in your provider's panel so the metadata matches. Creating /etc/cloud/cloud-init.disabled also works but switches off every other module, so prefer preserve_hostname.

Verify the new hostname

Check every place the name is read from, because they can disagree:

hostname                        # transient (kernel) name
hostname -f                     # FQDN via /etc/hosts or DNS
hostnamectl status              # static, transient, pretty
cat /etc/hostname               # what boots next time

If hostname and cat /etc/hostname differ, something set the transient name at runtime, usually a DHCP client or cloud-init. If hostname -f errors out, the problem is /etc/hosts.

Long-running daemons cache the hostname when they start: syslog writes the old name into every line and mail software announces it in HELO until restarted. Restart what matters, for example sudo systemctl restart rsyslog postfix; if you are not sure what is running on the box, listing services with systemctl shows every active unit. Finally, reboot once and run hostname again; on a cloud image that is the only proof the change is permanent.

Where the hostname lives on each distribution

The mechanics are nearly identical across modern distributions, with a few traps:

DistributionPersistent changeFiles involvedWatch out for
Ubuntu 16.04+, Debian 8+hostnamectl set-hostname/etc/hostname, /etc/hosts (127.0.1.1 line)cloud-init on VPS images; stale /etc/hosts breaks sudo
RHEL, Rocky, AlmaLinux 7+, Fedorahostnamectl or nmcli general hostname/etc/hostnameNetworkManager may take the transient name from DHCP if no static name is set
Alpine (OpenRC)Edit /etc/hostname, then rc-service hostname restart/etc/hostname, /etc/hostsNo hostnamectl
CentOS 6 / RHEL 6 (end of life)HOSTNAME= in /etc/sysconfig/network/etc/sysconfig/network, /etc/hostsNeeds hostname NAME or a reboot to apply

Whatever the distribution, the local name only matters to the machine itself. Once the server sends mail, the name that counts is the one attached to its IP in DNS.

What reverse DNS (PTR) is and why a VPS needs it

Forward DNS maps a name to an address with A and AAAA records. Reverse DNS does the opposite: a PTR record maps an address back to a name. For IPv4 it lives in the in-addr.arpa zone with the octets reversed, so 203.0.113.10 is looked up as 10.113.0.203.in-addr.arpa; IPv6 uses ip6.arpa. The reverse zone is delegated to whoever holds the IP block, and for a VPS that is your hosting provider. This is why you cannot add a PTR at your registrar or in a Cloudflare zone: those control example.com, not the address space.

Mail is where PTR records bite. Receiving servers check that the connecting IP has a PTR, and that the name in the PTR resolves forward to the same IP (forward-confirmed reverse DNS, or FCrDNS). Google's sender guidelines require a valid PTR that points back to the sending IP, Microsoft applies the same logic, and SpamAssassin ships rules such as RDNS_NONE and RDNS_DYNAMIC that add score when the PTR is missing or looks auto-generated. A VPS with no reverse DNS, or with the provider's default 203-0-113-10.static.provider.example, gets rejected outright or lands in spam regardless of SPF and DKIM. Outside mail, a proper PTR makes traceroute and third-party logs readable and signals a managed host under a real domain.

Set the PTR for your VPS and match it forward

1. Find the public IP

You need the exact address the PTR will be attached to. On the server, ip -4 addr show lists interface addresses, though on a VPS behind NAT the public IP may differ; checking your IP address on Linux covers both cases, including the external check with curl.

2. Create the forward record first

In your domain's DNS, add an A record such as mail.example.com → 203.0.113.10, plus an AAAA record if the server will connect over IPv6. Some providers refuse a PTR whose forward record does not exist yet, and the FCrDNS check needs it anyway, so publish the A record and wait until dig +short A mail.example.com returns the address.

3. Set the PTR from the provider panel

Open the server's network or IP section in your provider's client area and look for a field labelled reverse DNS, rDNS or PTR. Enter the FQDN exactly as it appears in the forward record: mail.example.com, no trailing dot, no typos. If the server will send mail over IPv6, set that PTR too, or restrict Postfix to IPv4 with inet_protocols = ipv4; Gmail enforces reverse DNS on IPv6 just as strictly.

4. Make the server announce the same name

Set the machine's hostname to the PTR name with sudo hostnamectl set-hostname mail.example.com, add it to /etc/hosts, and make sure the mail software uses it in HELO/EHLO. For Postfix that is a single setting:

sudo postconf -e 'myhostname = mail.example.com'
sudo systemctl reload postfix

Hostname, PTR, HELO name and A record now all agree on one FQDN and one IP, so every check a receiving server can run resolves to the same answer.

Check reverse DNS with dig -x and host

dig and host come from the dnsutils package on Debian and Ubuntu, and bind-utils on RHEL-family systems. Query the reverse record, then confirm it resolves forward to the original IP:

$ dig -x 203.0.113.10 +short
mail.example.com.

$ host 203.0.113.10
10.113.0.203.in-addr.arpa domain name pointer mail.example.com.

$ dig +short A mail.example.com
203.0.113.10

$ dig -x 2001:db8::10 +short
mail.example.com.

The PTR returns a name, the name returns the IP you started with: FCrDNS passes. If dig -x prints nothing or the status is NXDOMAIN, the record is either not published yet or your resolver still holds the old answer. Bypass caches with dig -x 203.0.113.10 @1.1.1.1, or use dig -x 203.0.113.10 +trace to see what the provider's authoritative server currently serves. Run the check from a machine other than the VPS when you can.

The mismatch that catches people most often is pointing the PTR at the bare domain. example.com usually resolves to a web server or a CDN, not to the VPS, so the reverse lookup succeeds but the forward confirmation fails. Use a dedicated hostname under your domain and keep its A record pointing at the mail server alone.

All of this assumes a server where you hold root and where the IP is yours to name. A Linux VPS with reverse DNS control from rdp.monster ships with its own static, dedicated IPv4 whose PTR you set from the client area, full root access, dedicated CPU and RAM, unlimited (fair-use) bandwidth, no KYC, and crypto payment accepted, from $8.99/month. Delivery takes about 10 seconds after payment confirms, so hostnamectl and the PTR field are the first two things you touch on a fresh box.

Frequently Asked Questions

Do I need to reboot after changing the hostname on Linux?

No, hostnamectl applies the new name immediately, but one reboot is the only real test that it sticks.
hostnamectl set-hostname updates the running kernel value and /etc/hostname in the same call, so the new name is live at once. What does not update automatically is anything that read the hostname earlier: open shells keep the old prompt until you log in again, and daemons such as rsyslog or Postfix keep announcing the old name until restarted. Reboot once anyway, especially on a VPS image with cloud-init, because that is the only way to prove the change is persistent.

What is the difference between the hostname command and hostnamectl?

hostname changes only the kernel's transient name; hostnamectl also writes the static name that survives reboots.
hostname NAME sets the kernel's current hostname and nothing else. It is gone at the next boot, and DHCP or NetworkManager can overwrite it sooner. hostnamectl set-hostname NAME talks to systemd-hostnamed, which sets the transient name and writes the static name to /etc/hostname, plus the pretty name in /etc/machine-info. On a systemd distribution use hostnamectl; reserve the plain hostname command for reading the value or for non-systemd systems such as Alpine.

Can I set reverse DNS for my VPS at my registrar or in Cloudflare?

No, PTR records live in the in-addr.arpa zone, which belongs to the owner of the IP block, i.e. your VPS provider.
Your registrar and Cloudflare manage the zone for example.com, which holds the forward records. A PTR record belongs to the reverse zone for the address, such as 113.0.203.in-addr.arpa, and that zone is delegated by the regional registry to whoever was allocated the block. For a VPS that is the hosting provider, so the PTR is set in the provider's panel or through its API. The only exception is when you own the IP range yourself or the provider delegates the reverse zone to your nameservers.

How long does a reverse DNS change take to propagate?

The provider usually publishes it within minutes; resolvers may keep the old answer until the record's TTL expires.
There is no global propagation step. Once the provider's authoritative server serves the new PTR, anyone who asks it directly gets the new value straight away; dig -x IP +trace shows exactly what it answers. Recursive resolvers that already cached the old record, or a negative answer, keep it until the TTL runs out, which can be anywhere from a few minutes to a day depending on the zone. If dig -x from your laptop still shows the old name, query @1.1.1.1 or the authoritative server instead of waiting.

Adrien Roche, Infrastructure & Hosting Editor

Systems engineer with 10+ years operating Windows Server and Linux fleets. Adrien runs the rdp.monster infrastructure documentation and writes our guides on RDP, VPS hosting, server administration, networking and privacy tooling.

Register to our reseller program

Your information

If you have any question, contact us by clicking here !
Name(Required)
Enter your email address, you must have an account on dashboard.rdp.monster !

Your company

Enter your website address if you have one
Quickly explain how you're going to sell services to your customers. For example, talk to people on forums.

Even monsters love cookies!

We use cookies to enhance your browsing experience, serve personalized ads or content, and analyze our traffic. By clicking "Accept", you consent to our use of cookies.