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.


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


/bin/hostname HOSTNAME




  • 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 '10 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 '11 at 21:14
  • yup, not a good solution, on re-leasing this will go to the toilet.
    – lukash
    Nov 3 '16 at 16:25

Try this.. Edit


Then add


See if that clears it up.

  • 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 '10 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 '10 at 21:47

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.

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

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.

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

Just add


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

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

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!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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