Note: This article is outdated as of Ubuntu 12.04. Please see this article if you're using Ubuntu 12.04 or later.
Dynamic Host Configuration Protocol (DHCP) is a commonly used method of obtaining IP and DNS information automatically from the network. In some cases, you may wish to statically define the DNS servers instead of using the ones provided by the DHCP server. For example if your ISP commonly experiences DNS outages, you might want to use the DNS servers provided by OpenDNS instead of the ones provided by your ISP.
When using a static IP configuration on Linux, you normally add the DNS servers to the /etc/resolv.conf
. However, if you try to add a DNS server to /etc/resolv.conf
under a DHCP configuration, you'll notice that your static entry disappears as soon as the DHCP client runs (usually on boot). To prevent this, you need to tell the DHCP client to prepend the static DNS server(s) to /etc/resolv.conf
before adding the ones provided from the DHCP server (if any).
The configuration file you'll need to edit is the same on both Debian and Ubuntu, however depending on your setup the location of the file may vary. Here are the two common places I've found the file:
Debian: /etc/dhclient.conf
Ubuntu: /etc/dhcp3/dhclient.conf
Open the file in your favorite editor and add one of two lines at the top, separating multiple DNS servers with a comma and ending the entry with a semi-colon:
If you simply want to add static DNS servers to be used in addition to the ones provided by DHCP, use a prepend entry:
prepend domain-name-servers 208.67.222.222, 208.67.220.220;
If you want to override the DNS servers provided by DHCP entirely and force the system to use the ones you provide, use the supersede entry:
supersede domain-name-servers 208.67.222.222, 208.67.220.220;
Before these static DNS servers will to be appended to your /etc/resolv.conf
file, you'll need to re-run the DHCP client. The easiest way to do this is by running /etc/init.d/networking restart
(sudo required) or you can try running the dhclient
command.
After re-running the DHCP client, check your /etc/resolv.conf
file to confirm the static DNS servers have been added.
nice, thanks
Glad I could help!
Thanks! Works great. I’ve also changed the request from:
request subnet-mask, broadcast-address, time-offset, routers,
domain-name, domain-name-servers, domain-search, host-name,
netbios-name-servers, netbios-scope, interface-mtu,
rfc3442-classless-static-routes;
to:
request subnet-mask, broadcast-address, time-offset, routers,
netbios-name-servers, netbios-scope, interface-mtu,
rfc3442-classless-static-routes;
That way it doesn’t even ask for DNS, hostename and so on.
cu
Roman
Awesome! Thanks for the tip, Roman! 🙂
cool Raam. I wanted to know how I can configure the dns servers to be over-riden by DHCP. I mean, which file and what change in that file to be made 🙂
Pavan, this article explains exactly that!
I configured a pdnsd server for my team. But the DNS server got overwritten by company global dncp service, by using you team, just works. Thanks.
You’re welcome, Feng! 🙂
Thanks Raam and Roman, I’ve been searching for an answer to this for weeks now – you just saved me a whole world of pain!
Thanks again!
Brian
Hertford, UK
Glad to help, Brian! 🙂
I’m running Mint 11 and the file is in
/etc/dhcp/dhclient.conf
It works and opendns is WAY better than my current provider dns for some reason.
In /etc/resolv.conf, why does it “search domain”? I’m still looking for that answer. Anyway, glad you posted this for us who are so tired of bad dn servers! 🙂
Regards
Thanks for the tip! 🙂 I find that speed really depends on your ISP. Sometimes the OpenDNS servers are slower than the ISP DNS.
Merci Raam !
very helpfull when connected thru openvpn, my internet provider DNS are not accessible via foreign IP adress
Nicolas
You’re most welcome, Nicolas! Glad to help. 🙂
Thank you! worked perfectly.
You’re welcome! I’m glad to hear it helped. 🙂
Thanks a lot for posting this! Btw, on my system (Debian sid) the file is /etc/dhcp/dhclient.conf
You’re welcome, Zed! Thanks for the tip about Debian sid. 🙂
Thanks a lot! Your post was so usefull 😀
My pleasure! Glad it helped. 🙂
outdated from ubuntu 12.04:Â http://www.stgraber.org/2012/02/24/dns-in-ubuntu-12-04/
Thank you for the heads up! I’ve added a note and link to the top of this article to indicate it is outdated.
Great. This is exactly what i was looking for.
Great! I’m glad this helped. 🙂
Hi there! 🙂
This quick refference was very helpful.
I’m glad it helped, Ivaylo! 🙂
Hi there. 🙂
In Debian 7 for static DNS, you need write in:
/etc/dhcp/dhclient.conf
supersede domain-name-servers staticServerIP
Thank you for the tip! 🙂
Thanks mate, it helped me a lot.
You’re welcome! 🙂
This is one solution to the problem…however if you want to permanently edit /etc/resolv.conf with your custom DNS nameservers on Debian-based system, then install resolvconf package using apt-get manager and manually populate resolvconf base file with your custom DNS entries and options. Use this post as a guide http://www.bytelinux.com/make-permanent-changes-to-resolv-conf-file-on-ubuntu/ in order to get the job done!
Thank you for the tip! 🙂
If I get the DNS servers from the server (DHCP), they are set correctly, but the file resolv.conf is not updated with the values.
Should etc/resolv.conf be updated?
Exact what I need right now – thank you.
on debian jessie the conf file:
/etc/dhcp/dhclient.conf
Thanks for the update, Aysad! 🙂