Permanently Set HOSTNAME in an AWS EC2 instance

This is more when you want to create an AMI from an EC2 image and you want it to have a persistent hostname. This was needed in our AWS based Jenkins process and was for our Centos/RHEL images, the Ubuntu/Debian process is very similar apart from stage 3 as this file does not exist:

1) Replace the “/etc/cloud/cloud.cfg” with:

#cloud-config
hostname: [yourhostname]
fqdn: [your fully qualified domain name]
manage_etc_hosts: true
# vim:syntax=yaml

3) Edit “/etc/sysconfig/network”:

NETWORKING=yes
HOSTNAME=[your hostname]

4) Edit “/etc/hosts”:

127.0.0.1   [your fully qualified domain name] [yourhostname] localhost.localdomain localhost localhost4.localdomain4 localhost4

5) Stop the Instance

6) Edit the Instance’s User Data:

#cloud-config
hostname: [yourhostname]

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.