News Technical
Logotipo de CentOS

How to Change the Hostname in CentOS/RHEL 7 Without Rebooting the Server

Changing the hostname of your CentOS 7 or RHEL 7 server is a common system administration task that can be done without needing to reboot the system. Follow these three simple and effective steps to permanently update your server’s name.

Before You Start: Check the Current Name

Before making changes, verify your current hostname with this command:

bash
hostname

Or for more detailed information:

bash
hostnamectl

Step-by-Step Procedure to Change the Hostname

Step 1: Configure the New Name in /etc/hostname

This file contains the hostname to be used after a system reboot.

bash
sudo nano /etc/hostname

Actions to perform:

  • Delete any existing name in the file

  • Write only the new hostname (without additional domains)

  • Example: If you want server1.mydomain.com, write only server1

Save changes: Ctrl + X, then Y and Enter

Step 2: Update the /etc/hosts File

This file maps IP addresses to hostnames on the local system.

bash
sudo nano /etc/hosts

Find the line containing your server’s IP address (usually 127.0.0.1 or the private IP) and update it:

Correct configuration:

text
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.1.8 server1.mydomain.com server1

Important note: Make sure to include both the FQDN (full qualified domain name) and the short hostname.

Step 3: Apply Changes Without Rebooting

To make changes take effect immediately without server restart:

bash
sudo hostnamectl set-hostname "new-hostname"

Practical example:

bash
sudo hostnamectl set-hostname "server1"

Immediate verification:

bash
hostname
hostnamectl status

Verification and Additional Useful Commands

Verification Commands

bash
# Check current hostname
hostname

# Detailed hostname information
hostnamectl

# Check network configuration
cat /etc/hostname
cat /etc/hosts

Troubleshooting Common Issues

ProblemSolution
Hostname doesn’t change after step 3Execute: sudo systemctl restart systemd-hostnamed
Error “hostname: you must be root”Make sure to use sudo before the command
Change doesn’t persist after rebootVerify /etc/hostname contains only the name, without domain

Temporary Change (Current Session Only)

If you need a temporary change that disappears on reboot:

bash
sudo hostname "temporary-name"