8

I'm running Amazon Linux (based on RHEL5) on EC2.

I have my own domain name pointing to the instance's elastic IP. Amazon auto-assigns the instance a generic (and not visible on the Internet) hostname via DHCP.

This internal-only hostname breaks sending email since the SMTP server wants to see a real (and public) hostname. I can fix email by manually running the "hostname" command to set the real public hostname.

I have set HOSTNAME in /etc/sysconfig/network but the DHCP hostname seems to override this.

Is there a good/correct way to set my hostname and always ignore what DHCP has to say about it, while still using DHCP otherwise?

I can think of lots of kludgy stuff to do (run a script that undoes what DHCP does, or whatever) but wondering if there's an actual config setting somewhere to force the hostname.

6 Answers 6

2

Mike's answer sounds better and will likely work, but if it gives you trouble, you can always use a sledgehammer. :)

Add

/bin/hostname HOSTNAME

To

/etc/rc.local

:)

3
  • 1
    Went with the sledge for now. Was hoping to avoid it and do things the "right way" but the "working way" is always plan B ;-)
    – Havoc P
    Dec 12, 2010 at 16:53
  • 1
    Hmm. Over time it turns out that the sledge fails, because on re-dhcp (which EC2 servers appear to do occasionally), the hostname gets lost again.
    – Havoc P
    Apr 18, 2011 at 21:14
  • yup, not a good solution, on re-leasing this will go to the toilet.
    – lukash
    Nov 3, 2016 at 16:25
8

Try this.. Edit

/etc/sysconfig/network-scripts/ifcfg-eth0

Then add

DHCP_HOSTNAME=host.domain.com

See if that clears it up.

2
  • Exactly what I was looking for, but it doesn't seem to work on the Amazon Linux image (maybe it was added post-RHEL5?)
    – Havoc P
    Dec 12, 2010 at 16:52
  • you know, this may work. I was doing something super stupid (I have a whole setup to apply config patches to a pristine unmodified AMI ... but it applies them after the network is up).
    – Havoc P
    Dec 12, 2010 at 21:47
5

For Centos 6 and therefore Redhat 6 you will continually lose whatever hostname you want your host to be known as locally, each time you get a new IP or reboot, while using DHCP. (Redhat 5 / Centos 5 info also below)

(which can be a pain if you script backups or similar, relying on a static hostname)

If you use dhclient and continually have your desired localhostname overwritten (forgotten) by the DHCP provided hostname, read on.

To force a local hostname (and still let DHCP give you whatever IP it wants) you need to modify (or create) the file /etc/dhcp/dhclient-eth0.conf.

(replace the string eth0 with whatever network interface you are getting your hostname overwritten by, eth0 works for most)

For my setup, this simple filecontent works:

interface "eth0"  
   {    
     supersede host-name "myworkbox";  
   }

and I save to /etc/dhcp/dhclient-eth0.conf.

If you have Redhat 5 / Centos 5, try doing the same, but file location should be /etc/dhclient-eth0.conf.

You will see why this occurs by viewing the section that references dhclient-${DEVICE}.conf in the file /etc/sysconfig/network-scripts/ifup

If you need to get a particular ip address, you need to review the manpage for dhclient.

1
  • Unfortunately, this does not seem to work on CentOS7 (on AWS, with SELinux enabled, even with correct contexts). :-(
    – JJC
    Nov 18, 2014 at 2:21
4

For the CentOS 7 AMI, set a static hostname by commenting out the lines:

- set_hostname
- update_hostname

in /etc/cloud/cloud.cfg. The comment character for YAML is #.

This is in addition to setting the hostname normally.

1
  • 4
    You can also add preserve_hostname: true in /etc/cloud/cloud.cfg. Dec 31, 2014 at 3:04
1

Just add

PEERDNS="no"

in /etc/sysconfig/network-scripts/ifcfg-ethX (tested on CentOS 6)

1
  • Peer dns is to enable(default) or disable /etc/resolv.conf update.
    – Jayan
    Sep 15, 2015 at 12:23
0

The /etc/dhcp/dhclient-eth0.conf addition (for AWS AMI (CentOS)) with contents:

interface "eth0"  
{    
  supersede host-name "myworkbox";  
}

seemed to work just fine (if using dhcp, of course). Thanks!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .