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

22. August 2016 AWS, CentOS 0
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.

Leave a Reply

Your email address will not be published.