How to Change the Hostname on Linux: Ubuntu, Debian, CentOS, and RHEL Guide

Changing a Linux hostname is a common administration task when a server is moved to a new role, added to an inventory system, cloned from a template, or prepared for production. A clear hostname helps teams identify systems quickly, reduces monitoring confusion, and makes logs easier to interpret across Ubuntu, Debian, CentOS, and Red Hat Enterprise Linux environments.

TLDR: Most modern Linux distributions use hostnamectl to change the hostname, for example: sudo hostnamectl set-hostname web01.example.com. Administrators should also update /etc/hosts so local name resolution remains consistent. In a 50-server environment, even a 10% rate of unclear hostnames can cause alert-routing mistakes and slower troubleshooting. Older CentOS or RHEL systems may require editing legacy configuration files instead.

What Is a Linux Hostname?

A hostname is the system name used to identify a Linux machine on a network. It may be a simple name such as web01, or a fully qualified domain name, often called an FQDN, such as web01.example.com.

Linux systems commonly use three hostname types:

  • Static hostname: The permanent hostname stored on disk and used after reboot.
  • Transient hostname: A temporary hostname that may be assigned by DHCP or network configuration.
  • Pretty hostname: A human-friendly name that may contain spaces, such as Production Web Server 01.

For servers, the static hostname is usually the most important one to configure correctly.

Check the Current Hostname

Before changing anything, an administrator usually checks the current hostname. On Ubuntu, Debian, CentOS, and RHEL systems with systemd, the following command displays hostname details:

hostnamectl

For a shorter result, the classic command also works:

hostname

To view the FQDN, if configured, the administrator can run:

hostname -f

If this command returns an error, the hostname may not be correctly mapped in /etc/hosts or DNS.

Change the Hostname on Ubuntu

On modern Ubuntu releases, such as Ubuntu 20.04, 22.04, and 24.04, the recommended method is hostnamectl. For example, to rename a server to app01.example.com, the administrator runs:

sudo hostnamectl set-hostname app01.example.com

Next, the local hosts file should be checked:

sudo nano /etc/hosts

A typical entry may look like this:

127.0.0.1       localhost
127.0.1.1       app01.example.com app01

After saving the file, the new hostname can be verified:

hostnamectl
hostname -f

A reboot is usually not required, but some applications, shells, or monitoring agents may need to be restarted to display the new name.

Change the Hostname on Debian

Debian uses nearly the same process as Ubuntu on systemd-based versions such as Debian 10, 11, and 12. The administrator can set the hostname with:

sudo hostnamectl set-hostname db01.example.com

Then /etc/hosts should be updated so the system can resolve its own name locally:

sudo nano /etc/hosts

An example configuration is:

127.0.0.1       localhost
127.0.1.1       db01.example.com db01

Debian systems may also contain the hostname in /etc/hostname. The hostnamectl command normally updates this file automatically, but an administrator can confirm it with:

cat /etc/hostname

If needed, the file can be edited manually, then the system can be rebooted or the hostname can be applied with the hostname command. However, hostnamectl remains the cleaner method on current Debian installations.

Change the Hostname on CentOS

CentOS 7, CentOS 8, and CentOS Stream use systemd, so hostnamectl is the standard approach. To change the hostname to cache01.example.com, the administrator runs:

sudo hostnamectl set-hostname cache01.example.com

The local hosts file should then be edited:

sudo vi /etc/hosts

A suitable entry may be:

127.0.0.1       localhost
192.168.1.25    cache01.example.com cache01

Using the server’s real private IP address is common on CentOS and RHEL servers, especially in data centers or virtual private cloud environments. After the change, verification is simple:

hostnamectl status

On older CentOS 6 systems, hostnamectl is unavailable. In that case, the administrator edits:

sudo vi /etc/sysconfig/network

The file may contain:

NETWORKING=yes
HOSTNAME=cache01.example.com

The administrator should also update /etc/hosts. A reboot is commonly used on these older systems to apply the change persistently.

Change the Hostname on RHEL

Red Hat Enterprise Linux 7, 8, and 9 follow the systemd method. To rename a RHEL server, the administrator can run:

sudo hostnamectl set-hostname rhelapp01.example.com

Then the hosts file should be checked:

sudo vi /etc/hosts

For example:

127.0.0.1       localhost
10.10.5.20      rhelapp01.example.com rhelapp01

RHEL administrators may also use nmcli in environments managed heavily by NetworkManager:

sudo nmcli general hostname rhelapp01.example.com

Afterward, the NetworkManager service can be restarted if required:

sudo systemctl restart NetworkManager

On RHEL 6, the process is similar to CentOS 6. The hostname is configured in /etc/sysconfig/network, and /etc/hosts should be updated as well.

Important Files to Know

Several files and services may influence hostname behavior:

  • /etc/hostname: Stores the static hostname on Ubuntu, Debian, and many systemd-based systems.
  • /etc/hosts: Maps IP addresses to local names and helps the system resolve itself.
  • /etc/sysconfig/network: Used by older CentOS and RHEL releases.
  • DNS records: Required when other machines need to resolve the hostname over the network.
  • Cloud-init: May reset hostnames on cloud servers if hostname preservation is not configured.
data centers

Best Practices for Linux Hostnames

Good hostname conventions make infrastructure easier to support. A company may choose names that include function, number, and environment, such as web01-prod, db02-stage, or backup01-dr.

Administrators should follow these practices:

  • Use lowercase letters, numbers, and hyphens where possible.
  • Avoid spaces in static hostnames.
  • Keep names short but meaningful.
  • Update DNS, monitoring, backup, and configuration management records.
  • Confirm that application licenses or certificates are not tied to the old hostname.

Common Problems After Changing a Hostname

If the command hostname -f fails, the FQDN is often missing from /etc/hosts or DNS. If monitoring still shows the old hostname, the monitoring agent may need a restart or re-registration. If a cloud instance reverts after reboot, cloud-init may be enforcing the original metadata hostname.

For cloud-init systems, an administrator can review:

/etc/cloud/cloud.cfg

The setting preserve_hostname: true may be needed, depending on the distribution and provider.

FAQ

  • Does changing the Linux hostname require a reboot?
    Usually, no. Modern systems apply the change immediately with hostnamectl. However, a reboot or service restart may be useful if applications still display the old name.

  • What is the best command to change a hostname on Ubuntu or Debian?
    The recommended command is sudo hostnamectl set-hostname newname.example.com, followed by checking /etc/hosts.

  • How is CentOS 6 or RHEL 6 different?
    Older CentOS and RHEL systems do not use hostnamectl. Administrators usually edit /etc/sysconfig/network and /etc/hosts, then reboot.

  • Should the hostname be an FQDN?
    For servers, an FQDN is often preferred because it works better with DNS, logs, certificates, and enterprise management systems.

  • Why did the hostname change back after reboot?
    DHCP, cloud-init, or configuration management tools may be overwriting the local hostname. The responsible system should be updated to preserve the desired name.