Intermediate Standard

How to set a static hostname on Linux

By Angus Published 14 May 2026 5 min read

Your Linux server’s hostname identifies it on a network and appears in shell prompts, logs and system tools. Without a persistent hostname, any name you set is lost after a reboot, which causes confusion in multi-server environments and breaks tools that rely on a consistent identity.

This article covers how to view your current hostname, set a new one permanently using hostnamectl, and update /etc/hosts so local name resolution reflects the change.

Before you begin

  • You need SSH access to your Linux server. See our guide on how to connect via SSH if you have not done this before.
  • Commands in this guide require root or sudo privileges.
  • This guide uses Ubuntu Server. The hostnamectl command works on any systemd-based distribution, including AlmaLinux and Debian.

Check your current hostname

Before making changes, confirm what hostname your server is currently using. The hostnamectl command displays the static hostname alongside other system identity information, so you can verify the starting state.

Run the following command over SSH:

hostnamectl

The output includes a Static hostname line showing the name currently assigned to the server. Note this down before proceeding.

Terminal output of the hostnamectl command showing the current static hostname and system details
The hostnamectl command displays the current static hostname and system information.

Set a persistent static hostname

Setting the hostname with hostnamectl writes the name to /etc/hostname and applies it immediately without requiring a reboot. This makes the change persistent across restarts, unlike the older hostname command which only affects the current session.

  1. Set the new hostname.
    Replace your-new-hostname with the name you want to assign. For a server that will be identified by a fully qualified domain name, use the format server1.example.com.
sudo hostnamectl set-hostname your-new-hostname
  1. Confirm the change.
    Run hostnamectl again to verify the Static hostname field now shows your new name.
hostnamectl

The new hostname is now active and will persist after a reboot. The next step updates /etc/hosts so the system can resolve the new name locally.

Update /etc/hosts

The /etc/hosts file maps hostnames to IP addresses locally. If your new hostname is not listed here, some applications and services may fail to resolve it, even though the system name has changed. Entries in /etc/hosts take precedence over DNS by default, as controlled by the Name Service Switch configuration in /etc/nsswitch.conf.

  1. Open /etc/hosts in a text editor.
    Use nano or your preferred editor.
sudo nano /etc/hosts
  1. Locate the line for your server’s IP address.
    You will see a line beginning with 127.0.1.1 followed by the old hostname. Update it to reflect the new name. If you are using a static LAN or VPS IP address, add or update a line for that address too.

A correctly formatted /etc/hosts file looks like this, where each line maps an IP address to a hostname, optional aliases and an FQDN:

127.0.0.1   localhost
127.0.1.1   your-new-hostname
10.0.0.11   server1 server1.example.com
  1. Save and close the file.
    In nano, press Ctrl+O to save, then Ctrl+X to exit.
  2. Verify local resolution.
    Confirm the system resolves the new hostname correctly by running the following, replacing your-new-hostname with the name you set.
getent hosts your-new-hostname

The command returns the IP address mapped to that name in /etc/hosts, confirming local resolution is working.

The /etc/hosts file open in nano showing updated hostname entries
Update the hostname entry in /etc/hosts to match the name set with hostnamectl.

Troubleshooting

Hostname reverts after reboot

If your hostname changes back after restarting, the name was likely set with the older hostname command rather than hostnamectl. The hostname command only applies to the current session and does not write to /etc/hostname.

  • Confirm the contents of /etc/hostname with cat /etc/hostname. It should contain only your new hostname.
  • Re-run sudo hostnamectl set-hostname your-new-hostname to write the name persistently.
  • On some cloud instances, a cloud-init configuration may override the hostname on boot. Check for a preserve_hostname setting in /etc/cloud/cloud.cfg and set it to true.

Applications cannot resolve the hostname

Some services fail to start or log errors if they cannot resolve the server’s own hostname. This usually means /etc/hosts has not been updated to include the new name.

  • Run getent hosts your-new-hostname to test local resolution. No output means the name is not in /etc/hosts.
  • Add the missing entry to /etc/hosts as shown in the step above.
  • Check /etc/nsswitch.conf and confirm the hosts: line includes files before dns, so local entries are checked first.

Wrapping up

You have set a persistent static hostname on your Linux server using hostnamectl and updated /etc/hosts so the system resolves the name locally. The hostname will now remain consistent across reboots.

From here, you may want to review how your server handles DNS and network identity more broadly. Our guides on how to find your server’s hostname, what an FQDN is and securing your VPS cover related topics worth reviewing after this change.

If you are running your own server and need more control over your environment, take a look at our VPS hosting plans.

Need more power?

Get scalable resources with our VPS hosting with root access and optional software.

Get VPS Hosting

Starting something new?

Perfect for websites and small businesses unlimited bandwidth with cPanel hosting.

Get cPanel Hosting