How to Set a Hostname and FQDN on RHEL 7

A properly configured hostname is one of the most fundamental aspects of Linux system administration. On Red Hat Enterprise Linux 7, the hostname identifies your machine on the network and appears in log files, shell prompts, and monitoring tools. Getting it right — including the Fully Qualified Domain Name (FQDN) — is essential for services like Kerberos, email delivery, SSL certificate validation, and directory integration to function correctly. RHEL 7 introduced hostnamectl, a systemd-based utility that replaces the older hostname command for persistent configuration, making hostname management cleaner and more consistent than on earlier releases.

Prerequisites

  • A running RHEL 7 system with root or sudo access
  • A valid hostname planned for your machine (e.g., webserver01)
  • A domain name if you require a FQDN (e.g., example.com)
  • Basic familiarity with the command line and text editing

Step 1: Understand the Three Hostname Types in RHEL 7

RHEL 7 with systemd supports three distinct hostname types, each serving a different purpose:

  • Static hostname: The traditional hostname stored in /etc/hostname. This is what most services and the kernel use. It must follow DNS naming rules (alphanumeric and hyphens only).
  • Pretty hostname: A free-form, human-readable label that can include spaces and special characters. Used by desktop environments and some management tools.
  • Transient hostname: A temporary hostname maintained by the kernel, which may be set by DHCP or mDNS and is lost on reboot. It defaults to the static hostname if one is set.

You can view all three at any time with:

hostnamectl status

Example output:

   Static hostname: webserver01.example.com
   Pretty hostname: Web Server 01
         Icon name: computer-server
           Chassis: server
        Machine ID: a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4
           Boot ID: 9f8e7d6c5b4a3f2e1d0c9b8a7f6e5d4c
  Operating System: Red Hat Enterprise Linux Server 7.9 (Maipo)
       CPE OS Name: cpe:/o:redhat:enterprise_linux:7.9:GA:server
            Kernel: Linux 3.10.0-1160.el7.x86_64
      Architecture: x86-64

Step 2: Set the Static Hostname with hostnamectl

The recommended method for setting a persistent hostname on RHEL 7 is hostnamectl set-hostname. This command writes the value to /etc/hostname and updates the running system immediately — no reboot required.

To set a short hostname:

sudo hostnamectl set-hostname webserver01

To set a FQDN as the static hostname (recommended for servers):

sudo hostnamectl set-hostname webserver01.example.com

To set a pretty hostname alongside the static one:

sudo hostnamectl set-hostname "Web Server 01" --pretty

Verify the change took effect:

hostnamectl status
hostname

Step 3: Verify and Edit /etc/hostname Directly

The hostnamectl command writes to /etc/hostname. You can view or edit this file directly if needed:

cat /etc/hostname

The file should contain a single line with your hostname:

webserver01.example.com

If you need to edit it manually (for example, during a scripted deployment):

echo "webserver01.example.com" | sudo tee /etc/hostname

After a manual edit, load the new hostname into the running system without rebooting:

sudo hostname $(cat /etc/hostname)

Note that the hostnamectl approach is preferred as it handles all three hostname types atomically and integrates with systemd properly.

Step 4: Configure /etc/hosts for FQDN Resolution

Setting the hostname alone is not enough for FQDN resolution. The system must be able to resolve the FQDN to an IP address. In environments without DNS, this is done via /etc/hosts. Even in DNS-managed environments, it is best practice to add a local entry so the machine can resolve its own name without depending on the network.

Open /etc/hosts with a text editor:

sudo vi /etc/hosts

Ensure it contains an entry mapping the server’s primary IP address to both the FQDN and the short hostname:

127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6

192.168.1.50   webserver01.example.com   webserver01

Replace 192.168.1.50 with your server’s actual IP address. The FQDN must come before the short hostname on the same line. Do not use 127.0.0.1 or 127.0.1.1 for the FQDN entry on a server — use the real interface IP so that services binding to the hostname get the correct address.

Step 5: Verify FQDN Resolution

Once /etc/hosts is updated, verify that the FQDN resolves correctly using the hostname command with the --fqdn flag:

hostname --fqdn

Expected output:

webserver01.example.com

If you receive hostname: Name or service not known, the FQDN is not resolvable — check your /etc/hosts entry or DNS configuration. You can also test with getent:

getent hosts webserver01.example.com
getent hosts webserver01

Both should return the IP address and both name forms:

192.168.1.50    webserver01.example.com webserver01

Step 6: Set the Hostname via nmcli (NetworkManager)

In some RHEL 7 configurations, especially on systems managed with NetworkManager, you can also set the system hostname through nmcli. This is useful in scripted provisioning workflows or when NetworkManager is the primary network management layer:

sudo nmcli general hostname webserver01.example.com

This writes to /etc/hostname and tells NetworkManager to apply the change. Verify with:

nmcli general hostname

Note that nmcli general hostname and hostnamectl set-hostname both write to the same underlying file; either method is acceptable, but avoid mixing them carelessly in automation scripts.

Step 7: Confirm the Hostname Persists After Reboot

To confirm that your hostname configuration survives a reboot, schedule a test or simply verify the source files are correct:

# Check the persisted hostname file
cat /etc/hostname

# Check the hosts entry is present
grep webserver01 /etc/hosts

# Check current runtime hostname
hostname
hostname --fqdn
hostnamectl status

If all three commands return the expected values, your configuration is correct and will survive reboots.

Troubleshooting Common Issues

FQDN returns “localhost.localdomain”

This typically means the FQDN entry in /etc/hosts is missing or incorrect. Ensure the real server IP maps to the FQDN and that no entry maps 127.0.0.1 to the hostname.

hostname –fqdn fails after setting hostnamectl

The static hostname is set, but /etc/hosts does not contain a matching FQDN entry. Add the entry as described in Step 4.

Pretty hostname not persisting

The pretty hostname is stored in /etc/machine-info by hostnamectl. Verify it with:

cat /etc/machine-info

It should contain a line like PRETTY_HOSTNAME="Web Server 01".

Properly setting the hostname and FQDN on RHEL 7 ensures that system services, logs, and network peers identify your machine consistently and correctly. Using hostnamectl for the static hostname, pairing it with a well-formed /etc/hosts entry, and verifying with hostname --fqdn gives you a solid, production-ready foundation for any RHEL 7 server deployment.