RHEL Hostname – How to Change the Hostname of a RHEL-Based Distro

hostname

I logged in for the first time, opened terminal, and typed in ‘hostname’. It returned ‘localhost.localdomain.com’. Then I logged as the root user in terminal using the command, ‘su –‘, provided the password for the root user and used the command ‘hostname etest’ where etest is the hostname I’d like my machine to have. To test if I got my hostname changed correctly, I typed ‘hostname’ again at terminal and it returned etest.

However, when I restart my machine, the hostname reverts back to ‘localhost.localdomain.com’.

Here are the entire series of commands I used in terminal.

    [thomasm@localhost ~]$ hostname  
    localhost.localdomain  
    [thomasm@localhost ~]$ su -  
    Password:   
    [root@localhost ~]# hostname etest  
    [root@localhost ~]# hostname  
    etest

I had run into the same problem when I set up RHEL and Ubuntu OS’s with VMPlayer.

Best Answer

On RHEL and derivatives like CentOS, you need to edit two files to change the hostname.

The system sets its hostname at bootup based on the HOSTNAME line in /etc/sysconfig/network. The nano text editor is installed by default on RHEL and its derivatives, and its usage is self-evident:

# nano /etc/sysconfig/network

You also have to change the name in the /etc/hosts file. If you do not, certain commands will suddenly start taking longer to run. They are trying to find the local host IP from the hostname, and without an entry in /etc/hosts, it has to go through the full network name lookup process before it can move on. Depending on your DNS setup, this can mean delays of a minute or so!

Having changed those two files, you can either run the hostname command to change the run-time copy of the hostname (which again, was set from /etc/sysconfig/network) or just reboot.

Ubuntu differs in that the static copy of the hostname is stored in /etc/hostname. For that matter, many aspects of network configuration are stored in different places and with different file formats on Ubuntu as compared to RHEL.

Related Question