Hostname updating problem with CentOS 7 RHEL 7 in AWS EC2
September 25, 2015
In CentOS 7 and RHEL 7 some times , we have faced hostname updating issue, i had got same issue in AWS EC2 where DHCP configures on CentOS 7, when i attempted a few time in different ways to set hostname with hostname file (/etc/hostname ) and through hostnamectl command , but after reboot hostname get same as earlier because DHCP overwrite the hostname on reboot .
I got solution after spending some of hours and the cloud-init was the reason behind this issue. cloud-init is an organization consolidated with most EC2 AMIs , including AMI Linux and CentOS .
I have found two method , which have solved this issue.
SOLUTION 1 :
You need to edit “/etc/cloud/cloud.cfg ” and comment out modules that set the hostname .The accompanying modules can be remarked out utilizing #.
# - set_hostname # - update_hostname
Now change your hostname thorough hostnamectl or edit the hostname file /etc/hostname like below.
#hostnamectl set-hostname server1.example.com #hostnamectl
After that reboot the server.
# systemctl reboot
You can override hostname name with your preferred hostname with below steps.
(1) Create a file in like 99_anyname.cfg in “/etc/cloud/cloud.cfg.d/” and write like below content.
#cloud-config hostname: server1 fqdn : server1.example.com
P.S : Here server1 and server1.example.com are hostname and fqdn.
(2) Set the permission and if selinux is enabled or in enforcing then set selinix context like below.
#chown root: /etc/cloud/cloud.cfg.d/99_anyname.cfg #chmod 644 /etc/cloud/cloud.cfg.d/99_anyname.cfg
If selinux in enforcing mode then execute below command and check correct file name.
#chcon system_u:object_r:etc_t:s0 /etc/cloud/cloud.cfg.d/99_anyname.cfg
(3) Reboot the server and after reboot you check now hostname .