How do I assign a static hostname to a private Amazon EC2 instance running RHEL 7 or Centos 7?

As a Linux System admin, we always love to work on CentOS and RHEL. In my case i had to launch a CentOS7 instance from the AWS Marketplace again. When we set the hostname with hostnamectl and reboot, yes the Instance comes with the default hostname again. So i have thought of documenting it here, so that it may be helpful for you as well !!

Issue

  • You have launched a CentOS 7 Instance from AWS Marketplace.
  • You have ran hostnamectl and rebooted the instance, but the hostname didnt became persistent.

Solution

  • Set hostname with hostnamectl command
    hostnamectl set-hostname server1.example.com
  • Assign the hostname in /etc/sysconfig/network file
    vim /etc/sysconfig/network
    NETWORKING=yes
    NOZEROCONF=yes
    HOSTNAME=server1.example.com
  • Update the cloud.cfg file and set preserve_hostname to true
    vi /etc/cloud/cloud.cfg
    preserve_hostname: true
  • Now reboot the server
    reboot
  • The server will bootup with the new persistent hostname. Please comment, if you have faced any issues with this.
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Scroll to Top