Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question
    
@I’m a newbie with Linux OS’s. If more details are required, please let me know. –  Thomas May 8 '11 at 13:25

2 Answers 2

up vote 22 down vote accepted

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.

share|improve this answer
    
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 May 12 '11 at 7:17

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.