Q. I'm using GNU/Linux with the Internet Systems Consortium DHCP Client. dhclient, provides a means for configuring one or more network interfaces using the Dynamic Host Configuration Protocol. It also updates my /etc/resolv.conf each time my laptop connects to different network. I would like to keep my existing nameservers. How do I skip /etc/resolv.conf update?
A. The DHCP protocol allows a host to contact a central server which maintains a list of IP addresses which may be assigned on one or more subnets. This protocol reduces system administration workload, allowing devices to be added to the network with little or no manual configuration. There are two ways you can get rid of this problem. Use any one of the following method.
WARNING! Many firewalls only allow access to certain nameservers only. So make sure your nameservers are supported. Also, many corporates block snooping name server such as OpenDNS due to privacy issues.Option # 1: Write protecting /etc/resolv.conf file
Write protect your /etc/resolv.conf file with chattr command under Linux ext3 file system:
# chattr +i /etc/resolv.conf
+i attribute write protect etc/resolv.conf file under Linux so that no one can modify it. You can use chflags command under FreeBSD.
Option #2: dhclient-script hooks
The DHCP client network configuration script is invoked from time to time by dhclient. This script is used by the dhcp client to set each interface's initial configuration prior to requesting an address, to test the address once it has been offered, and to set the interface's final configuration once a lease has been acquired.
This script is not meant to be customized by the end user. If local customizations are needed, they should be possible using the enter and exit hooks provided. These hooks will allow the user to override the default behavior of the client in creating a /etc/resolv.conf file.
When it starts, the client script first defines a shell function, make_resolv_conf, which is later used to create the /etc/resolv.conf file. To override the default behavior, redefine this function in the enter hook script.
Create hook to avoid /etc/resolv.conf file update
You need to create /etc/dhcp3/dhclient-enter-hooks.d/nodnsupdate file under Debian / Ubuntu Linux:
# vi /etc/dhcp3/dhclient-enter-hooks.d/nodnsupdate
Append following code:
#!/bin/sh make_resolv_conf(){ : }
Save and close the file. Set permissions:
# chmod +x /etc/dhcp3/dhclient-enter-hooks.d/nodnsupdate
Above script will replace make_resolv_conf() with our own function. This functions does nothing.
A note about RHEL / CentOS / Fedora Linux
Place following code in /etc/dhclient-enter-hooks file:
# vi /etc/dhclient-enter-hooks
Append code:
make_resolv_conf(){ : }
Save and close the file.
Option # 3: Configure dhclient.conf
/etc/dhclient.conf or /etc/dhcp/dhclient.conf file contains configuration information for dhclient. You can turn on or off DNS update and other options for specific interface or all interface using this file. The man pages for DHCLIENT.CONF and DHCP-OPTIONS point out that in dhclient.conf, you should add this:
option domain-name-servers 202.54.1.2, 199.2.3.4, 124.1.5.22
Further readings:
- man pages dhclient-script, dhclient, and dhclient.conf.
Updated for accuracy.
Featured Articles:
- 20 Linux System Monitoring Tools Every SysAdmin Should Know
- 20 Linux Server Hardening Security Tips
- My 10 UNIX Command Line Mistakes
- Linux: 20 Iptables Examples For New SysAdmins

- 25 PHP Security Best Practices For Sys Admins
- The Novice Guide To Buying A Linux Laptop
- 10 Greatest Open Source Software Of 2009
- Top 5 Email Client For Linux, Mac OS X, and Windows Users
- Top 20 OpenSSH Server Best Security Practices
- Top 10 Open Source Web-Based Project Management Software
- Top 5 Linux Video Editor Software
Facebook it - Tweet it - Print it -


{ 16 comments… read them below or add one }
in Debian & *ubuntus resolv.conf is handled by a package called resolvconf. You should modify resolv.conf through it.
resolvconf will append whatever’s in your resolvconf files (head, base, tail), but I haven’t figured out a way to stop it from including the DHCP stuff as well, which I don’t want. I will try write-protecting /etc/resolv.conf to see if that works.
Note: You are making a big assumption, that being that the user is using ext3. Yes RH and derivatives force this on you. Other linux distro’s recognize the other files systems where chattr is not going to be available.
Take a look at the man page for dhclient.conf (used by dhclient3 btw)
********snip*********
The supersede statement
supersede [ option declaration ] ;
If for some option the client should always use a locally-configured value or values rather than
whatever is supplied by the server, these values can be defined in the supersede statement.
*******end snip ***********
James,
Thanks for your feedback. The faq has been updated.
Thanks so much for this. It’s been driving me crazy trying to keep resolv.conf from being changed.
if you want local values to supersede server values add in /etc/dhclient3/dhclient3.conf
supersede domain-name “google.com”
At least on the BSD variants, the problem occurs when /sbin/dhclient-script is called by /sbin/dhclient. You could conceivably edit this script to ensure that /etc/resolv.conf does not get overwritten, but I find using the supersede directive works well enough. You would edit /etc/dhclient.conf as follows:
supersede host-name “myhost”;
supersede domain-name “my.domain”;
supersede domain-name-servers 127.0.0.1,192.168.1.1,192.168.1.2;
Which will give generate the following /etc/resolv.conf for you every time:
search mydomain
nameserver 127.0.0.1
nameserver 192.168.1.1
nameserver 192.168.1.2
There really should be a more obvious way to stop this often unwanted behavior.
Thanks for the information – you helped solve this irritating little problem for me.
My OS is kubuntu 9.10 x64. I have followed the procedure described in the 2nd Option, but it didn’t work.. resolv.conf is still being set to its original state everytime I reboot my system. Any of you can help me about it?
when you run dhclient eth0 chek which file is the correct configuration file
/etc/dhclient.conf
/etc/dhcp3/dhclient.conf
put supersede command in the correct configuration file
Follow option # 3, this should fix it.
Thanks for the help, that did not fix the problem but then I tried using supersede command instead of option, and now it works
For RHEL/CentOS the right way to solve this problem is to update /etc/sysconfig/network-scripts/ifcfg-eth0 or whatever interface you have, with:
PEERDNS=no
PEERNTP=no # if you have also problem with overwritten /etc/ntp.conf
check documentation at /usr/share/doc/initscripts-*/sysconfig.txt
or see whats inside script causing all this /sbin/dhclient-script , search for PEER
In Debian Lenny (5.0) you should do this:
# nano /etc/default/dhcpcd
(…)
# Uncomment this to allow dhcpcd to set the DNS servers in /etc/resolv.conf
# If you are using resolvconf then you can leave this commented out.
SET_DNS=’no’
# Uncomment this to allow dhcpcd to set hostname of the host to the
# hostname option supplied by DHCP server.
SET_HOSTNAME=’no’
(…)
Hi all,
When i use a vpn to my work i want the dns servers from my work to be used for queries to the local domain.
Is there a way to point the client to a specific dns server for domain
“mywork.local” ? When i connect the vpn resolv.conf does not get rewritten with the mywork dns servers…
I use ubuntu 10.10 desktop edition
Option # 3 needs correction I think.
In my case there was not dhclient.conf file and I created an empty one with the line you proposed:
option domain-name-servers a.b.c.d
Nothing changed in behaviour and there was an error in logs when dhclient started:
…dhclient: /etc/dhclient.conf line 2: expecting “code” keyword.
Finally this worked for me (choose one of those):
supersede domain-name-servers 8.8.8.8;
or
prepend domain-name-servers a.b.c.d;
‘prepend’ does not replace DNS servers obtained by DHCP, but this makes your name server to be the first one in resolv.conf
‘supersede’ totally replaces the option with your variant.
I hope this will help someone.
Michael.
The stub resolver (built into libc) can’t handle different directions for multiple domains, but you could always point it at a localhost resolver that does.
For that you will need to learn about BIND or LWres or Unbound or some other DNS server, none of which is within the scope of this question.
Having done that you’ll need to be a bit careful what you put in the “search” path; if you put VPN-only zones in it then there may be delays when you don’t have the VPN connected — not to mention, numerous entries in /var/log/named (or syslog) complaining about the unreachability of the DNS server at the other end of the VPN.