How to change the hostname of a RHEL-based distro?

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.


You can use hostnamectl.

1. Set hostname with:

# hostnamectl set-hostname etest

or

# hostnamectl set-hostname etest.something

2. Logout and back in.

3. (Optional) To see more options:

man hostnamectl

Here are the steps to change the hostname of a CentOS machine (or any other RHEL derivative) using terminal.

Open Terminal. Type su -, to login as the root. After providing the password, type vi /etc/sysconfig/network. This will open the 'network' file using the vi text editor in command mode. Type 'i' to go into insert mode of vi. Navigate to the hostname section of the file and replace localhost.localdomain with etest (or the required hostname). Press 'Esc' to exit insert mode and get back to the command mode. In command mode type :wq to save changes and exit vi.

Incase you want to exit without saving, type :q! in command mode.

Similar steps can be followed to edit the /etc/hosts file if required.

Tags:

Hostname