Wondering how to assign a static hostname to an Amazon EC2 instance on Centos 7? We can help you!
Often our customers report to us that even after changing hostname when they reboot or stop and then start the instance, the hostname changes back.
Here at Bobcares, we handle requests from our customers to fix this issue as a part of our Server Management Services.
Today let’s see how our Support Engineers assign a static hostname for our customers with EC2 instances.
Steps to Assign a Static Hostname to an Amazon EC2 instance on Centos 7
We must add the hostname to the appropriate configuration files on the instance so that the hostname persists even after rebooting or stopping and starting our EC2 instance.
Following are the steps to do this:
1. Firstly, we have to run the hostnamectl command from the shell to update the hostname of RHEL 7 or CentOS 7 Linux instance. We can do this using the following command:
sudo hostnamectl set-hostname --static persistent-hostname
Note: Changing the hostname by using the hostnamectl command doesn’t update the /etc/hosts file. However, if necessary, we can manually edit this file to add the new hostname.
2. Next we will use any text editor to update the /etc/cloud/cloud.cfg file.
Note: For instances running RHEL 7.4 and above, skip this step.
sudo vi /etc/cloud/cloud.cfg
3. Then we will add the following line to the bottom of the /etc/cloud/cloud.cfg file.
4. After that we can save the changes and exit the conf.
5. Now, we can run the reboot command to reboot the EC2 instance using the following command:
5. Finally, we can run the Linux hostname command without any parameters to verify.
We will be able to see that the hostname persists even after the reboot.
[Need assistance? We can help you]
To conclude, we saw the steps that our Support Techs follow to assign a static hostname to an Amazon EC2 instance on Centos 7.