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:
hostnameOr for more detailed information:
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.
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 onlyserver1
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.
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:
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:
sudo hostnamectl set-hostname "new-hostname"
Practical example:
sudo hostnamectl set-hostname "server1"
Immediate verification:
hostname
hostnamectl statusVerification and Additional Useful Commands
Verification Commands
# Check current hostname hostname # Detailed hostname information hostnamectl # Check network configuration cat /etc/hostname cat /etc/hosts
Troubleshooting Common Issues
| Problem | Solution |
|---|---|
| Hostname doesn’t change after step 3 | Execute: sudo systemctl restart systemd-hostnamed |
| Error “hostname: you must be root” | Make sure to use sudo before the command |
| Change doesn’t persist after reboot | Verify /etc/hostname contains only the name, without domain |
Temporary Change (Current Session Only)
If you need a temporary change that disappears on reboot:
sudo hostname "temporary-name"
