I am using GNU/Linux with the Internet Systems Consortium DHCP Client. The dhclient, provides a means for configuring one or more network interfaces using the Dynamic Host Configuration Protocol. It also updates my /etc/resolv.conf file each time my laptop connects to the different network. I would like to keep my existing nameservers. How do I skip /etc/resolv.conf update on a Linux based system? How do I stop DHCP client from changing resolv.conf on Linux?
The DHCP protocol allows a host to contact a central server which maintains a list of IP addresses which one can assign to one or more subnets. This protocol reduces system administration workload, allowing devices to be added to the network with little or no manual configuration.
WARNING! Many firewalls only allow access to specific nameservers only. So make sure your nameservers are supported. Also, many corporates block snooping name server such as OpenDNS due to privacy issues.
Make Sure /etc/resolv.conf never get updated By DHCP client
Information regarding DNS servers are stored in /etc/resolv.conf file. One can see it with cat command:$ cat /etc/resolv.conf
You can set or change DNS server by editing the /etc/resolv.conf file. However, this file might get updated by dhcp client on Linux. There are various methods to fix this issue. Use any one of the following methods. Let us see how to stop DHCP from changing resolv.conf file on Linux.
Write protecting /etc/resolv.conf file
Method 1: I am going to write protect your /etc/resolv.conf file using the chattr command on a Linux bases system. The syntax is:# chattr +i /etc/resolv.conf
The +i option (attribute) write protects /etc/resolv.conf file on Linux so that no one can modify it including root user. You can use chflags command on FreeBSD based system.
Creating dhclient-script hooks
Method 2: 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/dhcp/dhclient-enter-hooks.d/nodnsupdate file under a Debian / Ubuntu Linux:# vi /etc/dhcp/dhclient-enter-hooks.d/nodnsupdate
Append following code:
#!/bin/sh make_resolv_conf(){ : } |
Save and close the file. Set permissions using the chmod command:# chmod +x /etc/dhcp/dhclient-enter-hooks.d/nodnsupdate
The script will replace make_resolv_conf() with our own function. This functions does nothing and so no IP address will get added to /etc/resolv.conf file.
A note about resolvconf program on a Debian or Ubuntu based system
If the resolvconf program is installed, you should not edit the resolv.conf configuration file manually on a Debian or Ubuntu based system as it will be dynamically changed by programs in the system. If you need to manually define the nameservers (as with a static interface), add a line something like the following to the interfaces configuration file at /etc/network/interfaces file:
##Place the line indented within an iface stanza, e.g., right after the gateway line.## dns-nameservers 8.8.8.8 127.0.0.1
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. Another option is to modify your interface configuration file such as /etc/sysconfig/network-scripts/ifcfg-eth0 file and append any one of the following option:
# do not overwrite /etc/resolv.conf ## PEERDNS=no
OR
## use the following nameservers in /etc/resolv.conf ## PEERDNS=no DNS1=8.8.8.8 DNS2=1.2.3.4
Save and close the file. Where,
- PEERDNS=yes|no – Modify /etc/resolv.conf if peer uses msdns extension (PPP only) or DNS{1,2} are set, or if using dhclient. default to “yes”.
- DNS{1,2}=<ip address> – Provide DNS addresses that are dropped into the resolv.conf file if PEERDNS is not set to “no”.
Configure dhclient.conf file
Method 3: The /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:
supersede domain-name-servers 202.54.1.2, 199.2.3.4;
OR
prepend domain-name-servers 1.2.3.4, 1.2.3.5;
Here is a sample config for you:
timeout 60; retry 60; reboot 10; select-timeout 5; initial-interval 2; reject 192.33.137.209; interface "eth0" { send host-name "laptop-area51.nixcraft.net.in.home"; send dhcp-client-identifier 00:30:48:33:BC:32; send dhcp-lease-time 3600; supersede domain-search "net.in.home", "cyberciti.biz", "vpx.nixcraft.net.in"; prepend domain-name-servers 8.8.8.8, 127.0.0.1; request subnet-mask, broadcast-address, time-offset, routers, domain-search, domain-name, domain-name-servers, host-name; require subnet-mask, domain-name-servers; }
How to stop network-manger updating /etc/resolv.conf on Linux
Method 4: Edit the /etc/NetworkManager/NetworkManager.conf file using a text editor such as vim command or nano command:$ sudo vi /etc/NetworkManager/NetworkManager.conf
Add/edit/append as follows in [main]dns=none
Here is a full config:
[main] plugins=ifupdown,keyfile dns=none [ifupdown] managed=false [device] wifi.scan-rand-mac-address=no |
Save and close the file in vim text editor. Next time you reboot the Linux box your /etc/resolv.conf file will not get updated when using nm (network-manager) on Linux.