44

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?

Journeyman Geek
  • 133,878

10 Answers10

36

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.

slhck
  • 235,242
17

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.

Jawa
  • 3,679
8

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";
6

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.

Pablo A
  • 1,733
3

The /etc/dhclient-eth0.conf answer didn't work for me. I don't have an /etc/dhcp3 directory so I didn't think that was likely to work either.

After examining the /sbin/dhclient-script file (which creates /etc/resolv.conf on my Centos 5.6 system), I added the SEARCH line below to /etc/sysconfig/networking/devices/ifcfg-eth0:

DEVICE=eth0 
BOOTPROTO=dhcp
HWADDR=08:00:24:61:17:AC 
ONBOOT=yes
TYPE=Ethernet
SEARCH="example.com sub1.example.com sub2.example.com"

Then:

# ifdown eth0
# ifup eth0
#  cat /etc/resolv.conf
; generated by /sbin/dhclient-script
search example.com sub1.example.com sub2.example.com
nameserver 10.1.0.11
slm
  • 10,859
3

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

Pablo A
  • 1,733
2

For anyone going through Fedora / Red Hat's rather opaque pile of scripts, the answer, at least on Amazon's latest AMI, it is /etc/dhclient-eth0.conf (and not the decoy empty folder at /etc/dhcp/ ). The file is not present and will need to be created

1

Add to the DHCP server conf (ISC dhcpd):

option domain-name "domain1.com domain2.net domain3.org";

Where domain1.com, etc.. are the domain suffixes you want added to the resolv.conf file on each client.

slm
  • 10,859
LinuxUser
  • 169
1

In Fedora 19 add next lines to /etc/dhcp/dhclient.conf

# /etc/dhcp/dhclient.conf

interface "p2p1"
{
    supersede domain-name-servers 8.8.8.8, 8.8.4.4;
    append domain-name " mydomain.net example.com";
}

Work fine with NetworkManager. Details see: # man dhclient.conf

1

Since I don't see this answer and it worked for me (while the others didn't), here it is: edit /etc/resolvconf/resolv.conf.d/base the same way as you would /etc/resolv.conf. You'll need resolvconf installed.

appas
  • 113