Every Linux system has a hostname – a label that identifies the machine on a network. You need to change it when provisioning new servers, renaming machines to match your naming convention, or fixing a default hostname assigned by a cloud provider. Linux gives you several ways to do this, from the recommended hostnamectl command to nmcli and direct file edits.
This guide covers all methods to change the hostname on Linux systems including RHEL 10, Rocky Linux 10, AlmaLinux 10, Ubuntu 24.04, Debian 13, and Fedora 42. Each method works on any modern Linux distribution running systemd, which covers nearly every server and desktop distribution in active use today.
Prerequisites
- A Linux server or workstation (RHEL 10, Rocky Linux 10, AlmaLinux 10, Ubuntu 24.04, Debian 13, or Fedora 42)
- Root or sudo access
- SSH access if configuring a remote server
Step 1: Check the Current Hostname
Before making any changes, check what hostname your system currently uses. The hostnamectl command shows all hostname types along with system details.
hostnamectl
The output displays the static hostname, icon name, chassis type, and OS information:
Static hostname: localhost.localdomain
Icon name: computer-vm
Chassis: vm
Machine ID: 3f47d43533514bb6bcc03d51b7468459
Boot ID: a8b2c1d9ef124567890abcdef1234567
Virtualization: kvm
Operating System: Rocky Linux 10.0 (Smoky Quartz)
CPE OS Name: cpe:/o:rocky:rocky:10
Kernel: Linux 6.12.0-55.el10.x86_64
Architecture: x86-64
You can also use the hostname command for a quick one-line check:
hostname
This returns just the hostname without any extra system details:
localhost.localdomain
Step 2: Change Hostname with hostnamectl (Recommended)
The hostnamectl command is the standard way to set the hostname on any Linux system running systemd. It updates the static hostname in /etc/hostname and notifies all running services of the change – no reboot required.
sudo hostnamectl set-hostname server01.example.com
Confirm the change took effect immediately:
hostnamectl
The static hostname line should now show your new hostname:
Static hostname: server01.example.com
Icon name: computer-vm
Chassis: vm
Linux actually maintains three types of hostnames. The hostnamectl set-hostname command sets all three at once, but you can target each one individually:
- Static hostname – stored in
/etc/hostname, persists across reboots. This is the one you usually care about - Transient hostname – assigned at runtime by DHCP or mDNS, resets on reboot
- Pretty hostname – a free-form UTF-8 label for display purposes (can include spaces and special characters)
To set each type separately:
sudo hostnamectl set-hostname server01.example.com --static
sudo hostnamectl set-hostname server01.example.com --transient
sudo hostnamectl set-hostname "Production Web Server 01" --pretty
After setting the hostname, update /etc/hosts to match so that local name resolution works correctly. If you have static IP configured on your server, use that IP. Otherwise use the loopback address.
sudo vi /etc/hosts
Add or update the line that maps your IP to the new hostname:
127.0.0.1 localhost
127.0.1.1 server01.example.com server01
::1 localhost ip6-localhost ip6-loopback
Step 3: Change Hostname with nmcli
NetworkManager’s nmcli command line tool can also set the system hostname. This method is useful on systems where hostnamectl doesn’t persist the hostname across reboots – something that happens on Red Hat CoreOS (RHCOS) and Fedora CoreOS (FCOS) systems where the /etc/hostname file gets overwritten during updates.
Set the hostname with nmcli:
sudo nmcli general hostname server01.example.com
Verify the hostname was set by querying NetworkManager:
nmcli general hostname
The command should return your new hostname:
server01.example.com
After changing the hostname with nmcli, restart the systemd-hostnamed service so other tools pick up the change:
sudo systemctl restart systemd-hostnamed
This method writes the hostname to /etc/hostname through NetworkManager’s D-Bus interface, which makes it persistent across reboots. On RHCOS and FCOS systems, nmcli is the preferred method because it integrates with the immutable OS configuration.
Step 4: Change Hostname via /etc/hostname and /etc/hosts
On older systems without systemd or when you need to script hostname changes without installing extra tools, you can edit the configuration files directly. This method works on every Linux distribution.
Open the hostname file:
sudo vi /etc/hostname
Replace the entire contents with your new hostname – the file should contain a single line:
server01.example.com
Next, update /etc/hosts to map the hostname to your server’s IP address:
sudo vi /etc/hosts
Ensure your hostname appears on the 127.0.1.1 line (or your server’s static IP):
127.0.0.1 localhost
127.0.1.1 server01.example.com server01
::1 localhost ip6-localhost ip6-loopback
Apply the change without rebooting by running:
sudo hostname server01.example.com
The hostname command sets the transient hostname for the current session. Combined with the /etc/hostname edit, the change is both immediate and persistent.
Step 5: Change Hostname Temporarily
Sometimes you need a temporary hostname change that reverts on reboot – for testing, debugging, or short-lived environments. The hostname command sets only the transient hostname without touching /etc/hostname.
sudo hostname temp-debug-server
Verify the temporary change:
hostname
The output shows the temporary hostname:
temp-debug-server
You can also use hostnamectl with the --transient flag for the same effect:
sudo hostnamectl set-hostname temp-debug-server --transient
After the next reboot, the system reverts to the static hostname stored in /etc/hostname.
Step 6: Change Hostname on Cloud Instances (AWS, Azure, GCP)
Cloud instances have an extra layer of hostname management. Cloud providers assign hostnames through metadata services, and cloud-init can overwrite your changes on reboot. Here is how to make hostname changes stick on each major platform.
AWS EC2
On AWS EC2 instances, cloud-init resets the hostname on every boot by default. Set the hostname first, then tell cloud-init to preserve it.
sudo hostnamectl set-hostname web-prod-01.example.com
Edit the cloud-init configuration to prevent hostname resets:
sudo vi /etc/cloud/cloud.cfg
Find the preserve_hostname setting and change it to true:
preserve_hostname: true
This prevents cloud-init from overwriting your /etc/hostname on subsequent boots. If you also set a Name tag on the EC2 instance through the AWS console, the hostname and tag will stay consistent.
Azure
Azure Linux VMs use the waagent (Azure Linux Agent) alongside cloud-init. Set the hostname and update the cloud-init config the same way:
sudo hostnamectl set-hostname app-server-01.example.com
Then disable cloud-init hostname management:
sudo vi /etc/cloud/cloud.cfg
Set the preserve_hostname directive:
preserve_hostname: true
Google Cloud Platform (GCP)
GCP instances use Google’s guest agent to manage hostnames. The process is the same – set the hostname and disable cloud-init’s hostname module:
sudo hostnamectl set-hostname db-primary-01.example.com
Prevent cloud-init from resetting it:
sudo vi /etc/cloud/cloud.cfg
Change the setting:
preserve_hostname: true
For a detailed walkthrough on managing hostnames in cloud environments, see the guide on changing server hostname in EC2, OpenStack, and Azure instances.
Step 7: Hostname Naming Conventions
Choosing a good hostname matters for server management at scale. A clear naming scheme makes it easy to identify machines in logs, monitoring dashboards, and SSH sessions.
Follow these rules for valid and practical hostnames:
- Allowed characters – lowercase letters (a-z), digits (0-9), and hyphens (-). No underscores, spaces, or special characters
- Length – each label (between dots) can be up to 63 characters. The full FQDN must be under 253 characters
- No leading or trailing hyphens –
-web-01andweb-01-are invalid - Use FQDNs for servers –
web-prod-01.example.comis better than justweb-prod-01for DNS resolution and SSL certificates
A practical naming pattern for servers is role-environment-number.domain:
| Hostname | Purpose |
|---|---|
| web-prod-01.example.com | Production web server |
| db-staging-02.example.com | Staging database server |
| k8s-worker-03.example.com | Kubernetes worker node |
| mon-prod-01.example.com | Production monitoring server |
Avoid creative names like “thor” or “gandalf” for servers – they are fun at first but impossible to manage at scale. When you have 50 servers, you need to know what each one does from the hostname alone. If you run a CentOS, RHEL, Rocky, or Alma Linux system, the same naming rules apply.
Step 8: Verify the Hostname Change
After changing the hostname with any method, run through these verification steps to confirm everything is consistent.
Check the static hostname:
hostnamectl --static
This should return your new static hostname:
server01.example.com
Check the transient hostname to make sure it matches:
hostnamectl --transient
Verify the hostname resolves correctly through /etc/hosts:
getent hosts server01.example.com
The output should show your server’s IP mapped to the hostname:
127.0.1.1 server01.example.com
Check that the /etc/hostname file contains the correct value:
cat /etc/hostname
The file should contain a single line with your new hostname:
server01.example.com
Open a new terminal session or SSH connection to see the updated hostname in your shell prompt. The current session may still show the old hostname in the prompt until you log out and back in. If you use NetworkManager with nmcli on Ubuntu and Debian, verify with nmcli general hostname as well.
Conclusion
You now know how to change the hostname on Linux using hostnamectl, nmcli, and direct file edits. The hostnamectl method is recommended for most systems since it updates all hostname types at once and persists across reboots without extra steps. For cloud instances, always set preserve_hostname: true in cloud-init to prevent your changes from being overwritten.
After changing the hostname, remember to update DNS records, monitoring systems, and any configuration management tools (Ansible, Puppet) that reference the old hostname. For servers in production, plan hostname changes during a maintenance window since some services cache the hostname at startup and need a restart to pick up the new value.