July 2014
M T W T F S S
« Mar    
 123456
78910111213
14151617181920
21222324252627
28293031  
145

Refs

Categories

Archives

profile for slm on Stack Exchange, a network of free, community-driven Q&A sites

How to Override DHCP Settings on a Fedora/CentOS/RHEL Linux Box

Background

If you maintain your own DNS server and internal domainname then you’re going to want to read this article! I’m going to cover how you can override settings that are coming from your ISPs DHCP sever with your own internal values progmatically.

Little bit more Background

Now before I go too much further, I need to give a little more background. Currently I maintain a single Linux box running CentOS 5.4 which acts as a bridge/gateway system for my entire home network. This box has 2 ethernet cards (NICs) in it, eth0 and eth1. The eth0 device hooks up to my ISPs cable modem, while the other NIC hooks up to my internal network 192.168.0.x.

This article is going to focus on the 1st NIC device, eth0, and the fact that I run my own DNS & DHCP servers. I also maintain my own internal DNS domain called local.home. So all my systems can be accessed using either their short name of say machine1 and also machine1.local.home.

Now since all my systems make use of DHCP they receive their IP addresses, domainname, domainname servers, etc. from my DHCP server. I do this by including lines like the following in my DHCP server’s /etc/dhcpd.conf file:

1
2
option domain-name "local.home.";
option domain-name-servers 192.168.0.5, 8.8.8.8, 8.8.4.4;

The domain-name-servers are as follows: 192.168.0.5 is my DNS server, while the remaining 2 IPs belong to google’s DNS servers.

Through this setup I can control every NIC card that’s on the 192.168.0.x network except for one NIC card. The one connected to my ISPs cable modem, eth0. This NIC card is on a completely different network, i.e. my ISPs, so this NIC gets its IP address from the ISP’s DHCP server. Their DHCP server does a couple of things: it’s pushing their DNS servers IPs in addition to their default domainname suffix, i.e. isp.com. These aren’t really what I want this system to use so I needed a way to override these settings, and this is the gist of this article.

Solution

The solution is actually quite simple once you understand how DHCP works. I’m not going to go into a lot of details but the basics are that when you specify that a NIC is going to receive its IP address via DHCP, under the hood your system is going to run a command like this when the NIC is brought up:

1
/sbin/dhclient -1 -q -lf /var/lib/dhcp/dhclient-eth0.leases -pf /var/run/dhclient-eth0.pid eth0

/sbin/dhclient is called automatically for you, when the NIC device is brought up:

1
2
3
4
5
6
7
8
9
# brings eth0 NIC up
% ifup eth0
 
Determining IP information for eth0... done.
%
 
# brings eth0 NIC down
% ifdown eth0
%

Now with the NIC card up you should see the dhclient app running similarily to the line above. If you peak inside the leases file you’ll see stanzas like the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
lease {
  interface "eth0";
  fixed-address 66.250.123.33;
  option subnet-mask 255.255.255.0;
  option routers 66.250.123.1;
  option dhcp-lease-time 41472;
  option dhcp-message-type 5;
  option domain-name-servers 8.8.8.8,8.8.4.4;
  option dhcp-server-identifier 10.103.13.1;
  option broadcast-address 255.255.255.255;
  option domain-name "isp.com";
  renew 4 2011/6/2 05:44:50;
  rebind 4 2011/6/2 11:20:28;
  expire 4 2011/6/2 12:46:52;
}

These values come from the ISP’s DHCP server. dhclient uses these to generate the file /etc/resolv.conf with the following content:

1
2
3
4
; generated by /sbin/dhclient-script
search isp.com
nameserver 123.213.47.61
nameserver 123.213.47.62

I’d like to override the contents of /etc/resolv.conf so that my domain, local.home, & DNS servers, (192.168.0.5, 8.8.8.8, 8.8.4.4), are the defaults. Turns out there are 2 ways to accomplish this.

Fix #1

The first way is probably the most direct. The app, dhclient can make use of a config. file, /etc/dhclient.conf. To override the contents of the /etc/resolv.conf file simply create the file /etc/dhclient.conf:

1
2
3
4
5
6
7
8
interface "eth0" {
   supersede domain-name "local.home";
   supersede domain-name-servers 192.168.0.5, 8.8.8.8, 8.8.4.4;
   request subnet-mask, broadcast-address, time-offset, routers,
	domain-name, domain-name-servers, host-name;
   require subnet-mask, domain-name-servers;
   script "/sbin/dhclient-script";
}

This will result in the following /etc/resolv.conf file:

1
2
3
4
5
; generated by /sbin/dhclient-script
search local.home
nameserver 192.168.0.5
nameserver 8.8.8.8
nameserver 8.8.4.4

Fix #2

The second approach is to disable dhclient’s ability to create the file /etc/resolv.conf in the first place. This makes use of another dhclient facility, called hooks.Specifically there are 2 hooks, enter & an exit hook. To disable the creation of /etc/resolv.conf, you can create a file called: /etc/dhclient-enter-hooks. The contents should be as follows:

1
2
3
4
5
make_resolv_conf() {
        # We don't want /etc/resolv.conf changed
        # So this is an empty function
        return 0
}

Make the file dhclient-enter-hooks executable:

1
% chmod +x /etc/dhclient-enter-hooks

Additionally you’ll want to manually create a /etc/resolv.conf file, just like the one above. Doing it this way you’ll be creating a static /etc/resolv.conf file that won’t get rewritten each time the DHCP lease is renewed and/or changes in any way.

References

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>