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.
hostnamectl command works on any systemd-based distribution, including AlmaLinux and Debian.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.

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.
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
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.
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.
nano or your preferred editor.sudo nano /etc/hosts
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
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.

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.
/etc/hostname with cat /etc/hostname. It should contain only your new hostname.sudo hostnamectl set-hostname your-new-hostname to write the name persistently.preserve_hostname setting in /etc/cloud/cloud.cfg and set it to true.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.
getent hosts your-new-hostname to test local resolution. No output means the name is not in /etc/hosts./etc/hosts as shown in the step above./etc/nsswitch.conf and confirm the hosts: line includes files before dns, so local entries are checked first.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.
Get scalable resources with our VPS hosting with root access and optional software.
Get VPS HostingPerfect for websites and small businesses unlimited bandwidth with cPanel hosting.
Get cPanel Hosting