Override DHCP hostname on RHEL5/CentOS/Amazon Linux
Solution 1
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
:)
Solution 2
Try this.. Edit
/etc/sysconfig/network-scripts/ifcfg-eth0
Then add
DHCP_HOSTNAME=host.domain.com
See if that clears it up.
Solution 3
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.
Solution 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.
Solution 5
Just add
PEERDNS="no"
in /etc/sysconfig/network-scripts/ifcfg-ethX (tested on CentOS 6)
Related videos on Youtube
Havoc P
Updated on September 17, 2022Comments
-
Havoc P almost 2 years
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.
-
Havoc P over 13 yearsExactly 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 over 13 yearsWent 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 over 13 yearsyou 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).
-
JJC over 9 yearsUnfortunately, this does not seem to work on CentOS7 (on AWS, with SELinux enabled, even with correct contexts). :-(
-
Jayan almost 9 yearsPeer dns is to enable(default) or disable /etc/resolv.conf update.
-
lukash over 7 yearsyup, not a good solution, on re-leasing this will go to the toilet.