On this short tutorial, we will learn how to change hostname on CentOS 7 operating system.
Introduction
As a sysadmin, we are sometimes asked by our user to reset the hostname of a server. It doesn’t matter whether the system we are going to change the hostname for is in the cloud, virtual machine or on premise. On this article, we will discuss how to change the hostname on the CentOS 7 operating system.
The steps to change the hostname are as follows :
1. Checking Current Hostname
On this stage, we will check current hostname This task can be performed by submitting command line hostname atau hostnamectl.
$ hostnamectl
Output :
[ramansah@localhost /]$ hostnamectl Static hostname: localhost.localdomain Icon name: computer-vm Chassis: vm Machine ID: 0b99206d488246ab9454bb64a579545d Boot ID: 6813bffc38614b4dbbb667a7488fa097 Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-1160.el7.x86_64 Architecture: x86-64
2. Set New Static Hostname
On this stage, we will set a new static hostname. CentOS 7 only allows Fully Qualified Domain Names (FQDNs), for this purpose will do the following steps :
$ sudo hostnamectl set-hostname <new_hostname>
On this tutorial, we will use dev01.bckinf.com as new hostname. Outpu of this command is shown below.
[ramansah@localhost /]$ sudo hostnamectl set-hostname dev01.bckinfo.com
3. Verify the Hostname
Then we will verify again, the last hostname after changing, by submitting command line hostnamectl.
[ramansah@localhost /]$ hostnamectl Static hostname: dev01.bckinfo.com Icon name: computer-vm Chassis: vm Machine ID: 0b99206d488246ab9454bb64a579545d Boot ID: 6813bffc38614b4dbbb667a7488fa097 Virtualization: vmware Operating System: CentOS Linux 7 (Core) CPE OS Name: cpe:/o:centos:centos:7 Kernel: Linux 3.10.0-1160.el7.x86_64 Architecture: x86-64
[ramansah@localhost /]$ hostname dev01.bckinfo.com
4. Edit the /etc/hosts File
On this stage, we will update /etc/hosts file to change hostname persistently.
[ramansah@localhost /]$ sudo vi /etc/hosts
In the text editor, look for the line that begins with 127.0.0.1 (the IP address that refers to the system you are working on). It should read:
127.0.0.1 localhost localhost.localdomain localhost 4 localhost4.localdomain4 old.hostname
In this step we have to change the entry old.hostname
to my.new-hostname.server
– and spell it the same as in Step 2.
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
to be
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 127.0.0.1 dev01 dev01.bckinfo.com ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
After all are set, then we reboot the system, and check again by using command line hostnam or hostnamectl.
Conclusion
On this short article, we have learned how to change hostname on CentOS 7 operating system. I hope this guidance will be helpful.