When you first create an EC2 instance, set the hostname and reboot it you may notice the hostname and shell prompt are reset to ip-xx-xx-xx-xx.ec2.internal. In my test I used a red hat machine on the free tier. The machine is only using a private ip, with no public ip assigned.
- Get your private ip
- curl http://169.254.169.254/latest/meta-data/local-ipv4
- or get if from DNS host
- grep nameserver /etc/resolv.conf
- Edit your /etc/hosts file and add a line:
- xx-xx-xx-xx (your ip) your-name.us-east-1.net (your hostname)
- check the link above or man page for formatting
- Update the /etc/sysconfig/network file
-
HOSTNAME=your-name.us-east-1..net
NOZEROCONF=yes
NETWORKING=yes
-
- Update the /etc/cloud/cloud.cfg
- add the following line – “preserve_hostname: true”
- reboot your machine and confirm your hostname persists
- run hostname [-f]