19

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.

1
  • @I’m a newbie with Linux OS’s. If more details are required, please let me know.
    – Thomas
    Commented May 8, 2011 at 13:25

3 Answers 3

28

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.

3
  • Thanks for the location of the files. I really don't know anything about linux or using terminal so I needed detailed steps to use a text editor in terminal. Hence my answer posted below. But thanks for the location of the files I needed to edit.
    – Thomas
    Commented May 12, 2011 at 7:17
  • 1
    Note that this only applies to versions prior to CentOS 7, released some years after this answer was written, when the (different) systemd mechanism for the "static hostname" replaced /etc/sysconfig/network.
    – JdeBP
    Commented Oct 7, 2020 at 2:00
  • Unfortunately this doesn't work on CentoOS7 as /etc/sysconfig/network is now a directory. Using hostnamectl works momentarily but - like the OP - as soon as I reboot the hostname reverts back. Commented Feb 16, 2023 at 12:27
4

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
1
  • ... hostnamectl is only available on RHEL 7 & CentOS 7. Commented Aug 10, 2017 at 17:01
2

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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .