ec2でテスト環境を作成していると、よく/etc/hostsを書き換えて自分のドメインのIPを変えることをおこないます。
EC2ではIPアドレスが起動ごとに変わるため、毎回変更する必要が出てきますが、手作業でやるとメンドイのでスクリプト化します。
vim /root/scripts/update_hosts.sh
---
#!/bin/sh
PUBLICIP=`curl http://169.254.169.254/latest/meta-data/public-ipv4`
cp -f /root/template/hosts /etc/hosts
sed -i "s/___public_ip___/${PUBLICIP}/g" /etc/hosts
---
vim /root/template/hosts
---
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
___public_ip___ {domain}
---
vim /etc/rc.local --- ・ ・ ・ /bin/sh /root/scripts/update_hosts.sh ---


