Change the hostname of an AWS CentOS7 instance

Problem Description:

After upgrading AWS EC2 instances to CentOS 7, we found that changing the hostname is no longer as easy as in previous versions (CentOS 5/6).

After each new instance, modify the host name, restart or clone the machine, the host name will still be initialized.

 

Solution:

Finally found the relevant solution on the AWS official website -  https://aws.amazon.com/premiumsupport/knowledge-center/linux-static-hostname-rhel7-centos7/

It turns out that AWS uses cloud-init to configure the instance. The default instance hostname will be changed according to the instance IP (eg: ip-192-168-0-20). If you want to keep the instance hostname, you need to change the configuration file .

  • Add the reserved hostname configuration at the end of the configuration file:
# vi / etc / cloud / cloud.cfg
preserve_hostname: true
  • Modify hostname
hostnamectl set-hostname --static ec2-server01

In this way, if the instance is restarted or cloned again, the hostname will remain the same.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325638153&siteId=291194637