How to Permanently Change Hostname in CentOS 5.x Without Reverting After Reboot


2 views

Many CentOS 5.x administrators face this frustrating scenario: you change the hostname using standard methods, but after a reboot, the system reverts to the old configuration. Let's examine why this happens and how to implement a permanent solution.

In CentOS 5.x, the hostname is controlled by multiple configuration files and services:

1. /etc/sysconfig/network (primary configuration)
2. /etc/hosts (local DNS resolution)
3. Various network scripts and services

First, check all relevant files to understand where the old hostname might be hiding:

# Check running hostname
hostname

# Check network configuration
cat /etc/sysconfig/network

# Verify hosts file
cat /etc/hosts

# Check for DHCP overrides
cat /etc/dhclient.conf

Here's the complete sequence to ensure permanent hostname change:

# Step 1: Edit the main network configuration
vi /etc/sysconfig/network
# Ensure this line exists:
HOSTNAME=localhost.localdomain

# Step 2: Update hosts file
vi /etc/hosts
# Should contain:
127.0.0.1 localhost.localdomain localhost

# Step 3: Check for DHCP interference
vi /etc/sysconfig/network-scripts/ifcfg-eth0
# Ensure these lines exist:
DHCP_HOSTNAME=localhost.localdomain
NETBIOS=localhost.localdomain

# Step 4: Set hostname immediately
hostname localhost.localdomain

# Step 5: Prevent NetworkManager from overriding (if installed)
service NetworkManager stop
chkconfig NetworkManager off

Some systems may require these additional steps:

# Check for mail server configuration
vi /etc/mail/sendmail.cf
# Look for Djolddomain.com and change to Djlocalhost.localdomain

# Update postfix configuration if installed
vi /etc/postfix/main.cf
# Modify myhostname parameter

After making changes, test with these commands:

# Verify current hostname
hostname

# Check FQDN
hostname -f

# Test DNS resolution
ping localhost.localdomain

# Restart network to apply changes
service network restart

If issues persist, try these diagnostic steps:

# Check system logs for hostname changes
grep hostname /var/log/messages

# Verify boot sequence
chkconfig --list | grep network

# Check for competing services
ps aux | grep -E 'NetworkManager|dhclient'

When managing CentOS 5.x VPS instances, many administrators encounter hostname persistence issues where changes revert after system reboots. The classic symptoms:

  • Successful temporary change via hostname command
  • Proper configuration in /etc/sysconfig/network and /etc/hosts
  • Automatic reversion to previous hostname upon reboot

The proper way to permanently set hostname in CentOS 5.x requires modifications in three critical locations:

# 1. Edit /etc/sysconfig/network
NETWORKING=yes
HOSTNAME=localhost.localdomain
GATEWAY=xxx.xxx.xxx.xxx

# 2. Update /etc/hosts
127.0.0.1   localhost.localdomain localhost
::1         localhost6.localdomain6 localhost6

# 3. Immediate application (without reboot)
hostname localhost.localdomain
service network restart

Many VPS environments use DHCP which can override static hostname settings. Add this to /etc/sysconfig/network-scripts/ifcfg-eth0:

DHCP_HOSTNAME=localhost.localdomain

After making changes, verify with these commands:

# Check current hostname
hostname
hostname -f

# Verify DNS resolution
getent hosts localhost.localdomain

# Check network service status
service network status

For systems where traditional methods fail, try kernel-level setting:

sysctl kernel.hostname=localhost.localdomain
echo "kernel.hostname=localhost.localdomain" >> /etc/sysctl.conf
  • Check /var/log/messages for hostname-related errors
  • Verify no conflicting entries in /etc/host.conf
  • Ensure no hostname directives in /etc/rc.local
  • Test with chkconfig --list network to verify proper service startup