How can I add additional search domains to the resolv.conf created by dhclient in CentOS

170,222

Solution 1

None of those worked, but the last one was the closest. For Red Hat 6, Use DOMAIN instead of SEARCH as in the example above and the file location is different.

I modified the file /etc/sysconfig/network-scripts/ifcfg-eth0

and changed

DOMAIN=domain.com

to

DOMAIN="domain.com sub.domain.com"

and it all worked.

Solution 2

Also you can add string to /etc/dhcp3/dhclient.conf like this

prepend domain-search "domain1.com", "domain2.com";

Note, that this method works with Debian Lenny and Squeeze, too.

Solution 3

I managed to work this out in the end. I added a line like the following to /etc/dhclient-eth0.conf

append domain-name "example.com";

Solution 4

This is mostly a note for RHEL7 to reduce trial and error. Dean's answer of using DOMAIN="domain1.exmaple.com domain2.example.com" in /etc/sysconfig/network-scripts/ifcfg-device.conf works. An interesting note is the host's domain that the connection gets from DHCP is always prepended to the search path, even if you leave it out of DOMAIN= or put it later in a list for DOMAIN=. It looks like /sbin/dhclient-script has a bunch of logic related to this.

In my testing, I found that Philip's suggestion of using /etc/dhcp/dhclient-device.conf also works, although there is some strange behavior with that, most likely due to that same logic in /sbin/dhclient-script that tries to move things around. For instance, neither supercede or prepend work as expected, the host's domain will be first. As a side note on this method, /var/lib/NetworkManager/dhclient-device.conf is the generated NetworkManager file and is used by the client. If you have a file in /etc/dhcp/ that gets read in, you'll see it pasted at the top of the file and a few extra options added below.

Solution 5

On CentOS 6, I'm using the following file to add my preferred DNS search domain:

# cat /etc/dhcp/dhclient-eth0.conf 
interface "eth0" {
    supersede domain-search "dns1.example.com";
}
# getenforce 
Enforcing
# ls -lZ /etc/dhcp/dhclient-eth0.conf 
-rw-r--r--. root root system_u:object_r:bin_t:s0   /etc/dhcp/dhclient-eth0.conf
#

This file is the first that's checked for in /etc/sysconfig/network-scripts/ifup-eth:

if [ -s /etc/dhcp/dhclient-${DEVICE}.conf ]; then
   DHCLIENTCONF="-cf /etc/dhcp/dhclient-${DEVICE}.conf";

See also: redhat - Configuring DHCP on RHEL 6 - Server Fault

Share:
170,222

Related videos on Youtube

Martin Hilton
Author by

Martin Hilton

Updated on September 17, 2022

Comments

  • Martin Hilton
    Martin Hilton almost 2 years

    When my CentOS virtual machine boots it uses DHCP to get an IP address. It also overwrites resolv.conf with the DNS settings provided by the DHCP server. The DHCP server doesn't supply any search domains so I would like to get dhclient to put in a list of search domains when it writes it. How can I configure dhclient to do this?

  • Patrick
    Patrick about 11 years
    One note to add - if you're specifying multiple search domains to dhclient by using (option|supersede|*) domain-search ..., make sure you follow the dhcp-options(5) man page spec for quoting individual domains: option domain-search "example.com", "sales.example.com", "eng.example.com"; Many older distributions of dhclient allowed the format "example.com sales.example.com eng.example.com"; but this compatibility has been removed in the past few years due to bug fixes bugs.launchpad.net/ubuntu/+source/isc-dhcp/+bug/777785
  • Dr David C Crooke
    Dr David C Crooke almost 11 years
    Adding SEARCH to ifcfg-eth0/etc/sysconfig/network-scripts/ifcfg-eth0 also works on the Amazon version of Fedora. In older versions of Red Hat it apparently was DOMAIN. I wpould recommend this over the above.
  • Radek Simko
    Radek Simko over 10 years
    You should be able to use both DOMAIN and SEARCH github.com/mcr/isc-dhcp/blob/master/common/resolv.c#L68
  • PolyTekPatrick
    PolyTekPatrick almost 9 years
    This technique of setting DOMAIN also worked for me on CentOS 6.5, even though my resolve.conf is generated by NetworkManager rather than dhclient. The generated resolve.conf file combined my entries with those from the DHCP server.
  • radtek
    radtek over 5 years
    AWS Linux (RHEL), DOMAIN="your.domain" didn't work for me, reset after reboot
  • radtek
    radtek over 5 years
    does not work for aws linux (rhel)
  • puravidaso
    puravidaso about 3 years
    This works, however "append" works the same as "prepend" on CentOS 7, is this a known bug? How do I append?
  • user2066657
    user2066657 over 2 years
    Note that DOMAIN or SEARCH apparently must be added to the ifcfg-eth0 file, and adding it to just sysconfig/network is NOT sufficient -- DESPITE the ifup-post properly sourcing that file. NetworkManager, that fridge-art of pointless D-K wheel reinvention, will go behind the ifup-post invocation and munge the resolv.conf AFTER ifup-post is done, laying down ONLY the fqdn as SEARCH . It's obviously ignoring sysconfig/network because, well #boring or something. \sigh. Gold star, kids.