7

This question already has an answer here:

The title nearly says it all. I have a VM in EC2 running CentOS 7. It uses DHCP, which seems to be a EC2 requirement. DHCP keeps overwriting the hostname on reboot, no matter what I try. I won't enumerate it, but I've tried pretty much every suggestion here: Override DHCP hostname on RHEL5/CentOS/Amazon Linux as well as those found on several other sites that google searches turned up. I don't understand if it's the particular OS version or AWS/EC2 environment, but I can't change the hostname and make it stick across reboots. Even brute forcing it via /etc/rc.d/rc.local (adding "/bin/hostnamectl set-hotname foo.bar.baz" did not work. I can't believe other AWS/EC2 users haven't figured this out. Please help.

Update: This also does not work: http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/set-hostname.html I.e., adding a DNS A record for the box in AWS's Route53 (and verifying it works), and setting HOSTNAME=foo.bar.baz in /etc/sysconfig/network (checking after a reboot) is also futile.

Update 2: A similar question was answered for Fedora 20. The accepted solution there also involved reconfiguring cloud-init, though in a different manner. Also, it wasn't clear that that question/solution applies to other distros. Thus, I feel my question should probably remain for others running CentOS and searching for it that way (yes, I know Fedora and CentOS are siblings, but not everyone googles for all synonyms when searching for solutions).

marked as duplicate by Ward, mdpc, Michael Hampton Nov 26 '14 at 3:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • This question was answered for a different distro, and was answered in a slightly different way. I understand why you'd want your answer to be definitive, but I think it's a little unfair to close this question as "duplicate." Maybe "similar" is more accurate. – JJC Nov 26 '14 at 13:53
  • 1
    I tend to agree. I didn't find it when searching for the issue myself (google) a week ago, so others probably aren't, either. – Sirex Nov 26 '14 at 18:41
7

Alright. I spent probably a total of five hours on this problem before arriving at an answer. @Sirex's suggestion to uninstall cloud-init was the clue. Essentially, cloud-init is a service built into most EC2 images (AMIs), including AMI Linux and CentOS. The system does lots of things (google it), and is probably useful for certain provisioning and maintenance foo. Thus, I am hesitant to simply disable it. But, it also appears to be setting the EC2 hostname. So, to override that with your preferred hostname, simply do this:

(1) create a file called something like 99_...cfg (I named mine 99_hostname.cfg), and write the following contents into it:

#cloud-config
hostname: foo
fqdn: foo.bar.baz

replacing "foo" and "foo.bar.baz" with your host name and fully-qualified domain name, of course.

(2) Place this file in /etc/cloud/cloud.cfg.d/99_hostname.cfg

(3) Make sure the file permissions/owner/selinux context (if you have SELinux enforcing) are set properly via these commands:

sudo su -
cd /etc/cloud/cloud.cfg.d/
chown root: 99_hostname.cfg
chmod 644 99_hostname.cfg
chcon system_u:object_r:etc_t:s0 99_hostname.cfg

(4) Reboot your VM, and the new hostname should be set. If you don't trust your bash prompt, you can check this, by, for example, executing either of the following:

hostnamectl

or

cat /etc/hostname
2

uninstall cloud-init.

This drove me mental the first time :-)

Still looking to find time to work out how to make cloud-init set the hostname as expected (from a tag or such) - not got that far myself yet.

  • HA! "mental." Yes, I was livid after spending hours HOURS on this problem. EC2's support hasn't helped yet either, telling me to try hostnamectl and nmcli, neither of which persisted the name on reboot. BUT! Your answer made me dig into how to make could-init override the hostname, and, by golly, IT WORKED! Hallelujah! THANK YOU. I'd accept your answer, but I think there are probably reasons to keep cloud-init installed, so I'm posting what to do. – JJC Nov 26 '14 at 2:51
  • 1
    I know right ? I tried every frikking method of changing a hostname i knew over like an entire day... Yeah post that up, would be good for others to see. Just hunted around and seen the user-data item during instance launch myself. face-palm. ;-/ – Sirex Nov 26 '14 at 2:58
  • :-) Posted the steps I took. Please share the upvote love. :-D – JJC Nov 26 '14 at 3:13
  • Yep, "sudo yum remove cloud-init" and now I am less mental, just wasted 2 hours on this. Thanks! – meme Jun 21 '15 at 4:37

Not the answer you're looking for? Browse other questions tagged or ask your own question.