Configuring Static DNS with DHCP on Debian/Ubuntu

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.

Write a Comment

Comment

36 Comments

  1. 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

  2. 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 🙂

  3. 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.

  4. 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

  5. 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.

  6. Merci Raam !
    very helpfull when connected thru openvpn, my internet provider DNS are not accessible via foreign IP adress

    Nicolas

  7. 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?