Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Change Hostname in CentOS RHEL: Step-by-Step

· 5 min read · Home Tech

In this article
  1. Why the Hostname Matters
  2. How Hostname Resolution Works on CentOS/RHEL
  3. How to Change the Hostname (Modern Method)
  4. Changing Hostname on Older CentOS/RHEL (6 and Earlier)
  5. Choosing a Good Hostname
  6. Common Issues and How to Fix Them
  7. Verifying the Change
  8. Frequently Asked Questions

To change the hostname on CentOS or RHEL, use the hostnamectl set-hostname command, which works on all modern versions (7 and later). This guide covers the command, how to verify the change, and how to handle older systems that rely on different configuration files.

Why the Hostname Matters

The hostname is the network name of your server. It appears in your shell prompt, in log files, and in commands like hostname and hostname -f (fully qualified domain name). It is also used by other systems on your network to identify your machine. A clear, consistent hostname makes it easier to manage multiple servers, especially in environments with dozens or hundreds of machines. For example, a web server might be named web01.example.com, while a database server might be db01.example.com.

How Hostname Resolution Works on CentOS/RHEL

On CentOS and RHEL, the hostname is stored in the kernel and set at boot time. The hostnamectl tool (part of systemd) manages the static hostname, which is written to /etc/hostname. The file simply contains the hostname on the first line. When you run hostnamectl set-hostname, it updates both the kernel's current hostname and the /etc/hostname file, so the change persists across reboots.

There are actually three hostname types in systemd:

  • Static: stored in /etc/hostname, used at boot.
  • Transient: set by DHCP or mDNS, can change at runtime.
  • Pretty: a free-form description like "Web Server 1" — not used for network identification.

Most of the time, you only need to change the static hostname.

How to Change the Hostname (Modern Method)

On CentOS 7 and later, and RHEL 7 and later, use hostnamectl. The basic command is:

sudo hostnamectl set-hostname new-hostname

For example, to set the hostname to web01.example.com:

sudo hostnamectl set-hostname web01.example.com

After running the command, verify the change with:

hostnamectl

or simply:

hostname

The change takes effect immediately for new sessions. Your current shell prompt may not update until you log out and back in, or you can force it by running exec bash (or exec $SHELL).

Changing Hostname on Older CentOS/RHEL (6 and Earlier)

CentOS 6 and RHEL 6 do not have hostnamectl. Instead, you edit the /etc/sysconfig/network file. Open it with a text editor:

sudo vi /etc/sysconfig/network

Find the line that starts with HOSTNAME= and change it to your desired hostname, for example:

HOSTNAME=web01.example.com

Save the file, then run the hostname command to set it immediately:

sudo hostname web01.example.com

This change is temporary until you reboot, but it applies right away. The /etc/sysconfig/network file ensures the hostname is set on the next boot.

Choosing a Good Hostname

Hostnames can be simple (like server1) or fully qualified (like server1.example.com). The fully qualified domain name (FQDN) is the one that includes a domain. When you set a hostname with hostnamectl, you can use either, but it is common to use the FQDN on servers that are part of a domain.

Here are a few guidelines:

  • Use lowercase letters, numbers, and hyphens. Underscores are not recommended.
  • Keep it short enough to be readable in a prompt, but descriptive enough to identify the server's role.
  • Avoid spaces and special characters.
  • Make sure the hostname resolves correctly in your DNS or /etc/hosts file if other systems need to reach it.

Common Issues and How to Fix Them

After changing the hostname, you may notice that the shell prompt still shows the old name. This is normal — the prompt is set by the shell, and it reads the hostname at startup. Log out and log back in, or run exec bash to refresh it.

Another common issue is that the hostname reverts after a reboot. This usually happens if you edited /etc/sysconfig/network on an older system but did not run the hostname command, or if you used hostname without editing the file. On modern systems, hostnamectl should persist, but if it doesn't, check that /etc/hostname contains the correct name and that no other service (like a DHCP client) is overwriting it.

Also, if you have a line in /etc/hosts that maps your old hostname to 127.0.0.1, some commands may still display the old name. Update /etc/hosts to include the new hostname:

127.0.0.1   localhost localhost.localdomain
127.0.0.1   web01.example.com web01

Verifying the Change

To confirm that the hostname is set correctly, use the following commands:

  • hostname — shows the short hostname.
  • hostname -f — shows the FQDN (if set).
  • hostnamectl — shows detailed status, including static and transient hostnames.
  • cat /etc/hostname — shows the static hostname file content.

If you changed the hostname on a system that is part of a cluster or uses Kerberos, you may need to update the /etc/krb5.conf or the local /etc/hosts to reflect the new name, otherwise authentication may fail.

Frequently Asked Questions

Do I need to reboot after changing the hostname?

No, a reboot is not required. The hostnamectl set-hostname command takes effect immediately. However, some services that read the hostname at startup (like your shell prompt or certain daemons) may need to be restarted or you may need to log out and back in to see the change.

Can I change the hostname without sudo?

No, changing the system hostname requires root privileges. Use sudo or log in as root. The hostname command can be run as a regular user to view the hostname, but not to change it.

What is the difference between a short hostname and an FQDN?

A short hostname is just the machine name, like web01. An FQDN includes the domain, like web01.example.com. When you set a hostname with hostnamectl, you can use either. If you set the FQDN, the short name is automatically derived from it. Some services, like email or web servers, require the FQDN to work correctly.

Why does my hostname show as localhost?

If your hostname is localhost or localhost.localdomain, it usually means the hostname was never set, or the /etc/hostname file is empty or contains localhost. Use hostnamectl set-hostname to set a proper name.

How do I change the hostname on CentOS 8 or RHEL 8?

The process is identical to CentOS 7 and RHEL 7. Use hostnamectl set-hostname. CentOS 8 and RHEL 8 still use systemd, so the same commands work.

Can I use the hostname command to change the hostname permanently?

No, the hostname command alone only changes the current runtime hostname. To make it permanent, you must also edit /etc/hostname (modern) or /etc/sysconfig/network (older). The hostnamectl command does both at once.