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.
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="add9c5c2c0ccdec0edc1c2ceccc1c5c2ded9">[email protected]</a> ~]$ hostname
localhost.localdomain
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="592d313634382a341935363a383531362a2d">[email protected]</a> ~]$ su -
Password:
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="0f7d60607b4f63606c6e6367607c7b">[email protected]</a> ~]# hostname etest
[<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="f0829f9f84b09c9f93919c989f8384">[email protected]</a> ~]# hostname
etest
I had run into the same problem when I set up RHEL and Ubuntu OS’s with VMPlayer.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
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.
Method 2
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
Method 3
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.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0