Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question

5 Answers 5

up vote 2 down vote accepted

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

:)

share|improve this answer
    
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
    
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

Try this.. Edit

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

Then add

DHCP_HOSTNAME=host.domain.com

See if that clears it up.

share|improve this answer
    
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.

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

Just add

PEERDNS="no"

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

share|improve this answer

For the new CentOS 7 AMI, the only way I could get a static hostname set was 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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