How to Change Hostname in RHEL / CentOS 7.0
You have probably needed to change the hostname of your CentOS 7 install for one reason or the other. Changing the hostname in CentOS 7 is now much simpler than ever.
If you are ready open up your terminal and follow through this tutorial, and in less than 10 minutes you will have changed your hostname!
To check the hostname open your terminal and type the following command:
By default the hostname is localhost.localdomain. To change this you can follow the following steps
Edit /etc/hostname File
The easiest way to change the hostname is to edit the /etc/hostname file. Open your terminal and with your favourite text editor delete localhost.localdomain and change to whatever name you like.
$ sudo vi /etc/hostname
Save changes and then confirm your hostname by using hostname command. If it has not changed then you need to restart the CentOS 7 server.
Hostnamectl is a tool that is used control the Linux system hostname. You can also use this tool to change the hostname in a few easy steps.
First and foremost confirm the hostname by using hostnamectl as follows:
$ hostnamectl status
To change the hostname type the command:
$ hostnamectl set-hostname grace
It may be necessary to restart the systemd-hostnamed daemon so that as to reflect the change in Static hostname.
$ sudo systemctl restart systemd-hostnamed
Note: Hostnamectl recognizes three types of hostnames: pretty, static and transient. Pretty is stored in/etc/machine-info and is a human readable format while static hostname is stored in /etc/hostname. Transient hostname is more of a temporary hostname which may take up the static hostname when network connectivity is lost.
To change the hostname you can also use the NetworkManager text interface tool (nmtui). This is also another very easy method and fairly straight forward.
From the command line invoke nmtui:
$ sudo nmtui
This will present to you a text user interface like this. Using the arrow keys select Set system hostname and use tab to select OK.
Edit the hostname which by default is localhost.localdomain and change it to whatever you prefer.
After changing the hostname, choose okay, then the following confirmation message will appear. If the hostname is as you desire you can choose OK. You can confirm changes using the hostname command.
Nmcli is a command line tool for controlling the NetworkManager and can also be used to change the hostname.
To check the hostname with nmcli, type in your terminal:
$ nmcli general hostname
This will print out the hostname to your terminal. By default in CentOS 7 it should be localhost.localdomain.
To change this use the command:
$ nmcli general hostname grace
This will request for your password via a GUI interface. If you are running this command remotely remember to use root or sudo:
$ sudo nmcli general hostname grace
You can then check the hostname with the command hostname or nmcli general hostname.