How to override the DHCP-provided nameserver?

36,019

Solution 1

If you want to override or append to the name servers suggested by the DHCP server, you can configure this in /etc/dhcp/dhclient.conf, eg:

interface "eth0" {
    prepend domain-name-servers 10.0.0.2;
}

You can also do this from the "Edit Connections" control in the network indicator in Unity or GNOME.

Solution 2

A resolution that will resolve your problem is to configure your resolvconf package. This will give precedence to your preferred nameservers as well as any other desired settings resolv.conf settings such as search and domain preferences.

Edit the file: /etc/resolvconf/resolv.conf.d/head

Place your desired nameservers and any other configurations you want to take preference when there is a network change there. When any changes to the network happen, the configuration there will always take precedence.

An example of the /etc/resolvconf/resolv.conf.d/head file:

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN

nameserver 8.8.8.8
search mydomain.com

Look at the answer to a similar question:
override dns nameserver

Note:
Be sure to have a linefeed after the last entry. Most editors will provide a linefeed automatically when saving the file.

Share:
36,019

Related videos on Youtube

David Parks
Author by

David Parks

Updated on September 18, 2022

Comments

  • David Parks
    David Parks over 1 year

    I (think) that I've configured a static IP address in /etc/network/interfaces:

    # The primary network interface 
    auto eth0 
    iface eth0 inet static
        address 10.1.1.2
        netmask 255.255.255.0
        network 10.1.1.0
        broadcast 10.1.1.255
        gateway 10.1.1.1
    

    And I change the file /etc/resolv.conf to include the desired nameservers.

    Then I restart networking sudo /etc/init.d/networking restart

    But after some hours resolv.conf always reverts back to the DHCP nameserver.


    I should also say that this is Ubuntu Server with only l0 and eth0 interfaces.

    • h3.
      h3. over 12 years
      Is the resolvconf package installed?
    • David Parks
      David Parks over 12 years
      There is only eth0 and lo0, the IP I manually set is the same as the DHCP IP I will get (the DHCP server will always serve the same static IP). But I don't want to use the nameserver given by the DHCP server, so I thought I would set the IP myself and there would not be any DHCP, but I don't understand why the resolv.conf keeps being updated.
    • David Parks
      David Parks over 12 years
      Gilles - no, I ran: dpkg -L resolvconf, and get: Package `resolvconf' is not installed.
  • David Parks
    David Parks over 12 years
    This was the best solution, I posted another option, but realized that setting a static IP was not a good idea in an AWS EC2 environment. :)
  • poolie
    poolie over 10 years
    How is this relevant, and why do you think it's a good general rule? He doesn't want to stop using DHCP?
  • Grammargeek
    Grammargeek over 8 years
    @EugenevanderMerwe It's the prepend command. The poster said "This was the best solution, I posted another option, but realized that setting a static IP was not a good idea in an AWS EC2 environment. :) "
  • blueFast
    blueFast about 6 years
    What if you want to prepend several domain name servers?
  • blueFast
    blueFast about 6 years
    Prepending domain name server 127.0.0.1 seems to discard the name server supplied by dhcp (this does not happen prepending other name servers)
  • Sqerstet
    Sqerstet about 5 years
    Replace prepend with supersede to overwrite DHCP value completely.
  • ctrl-alt-delor
    ctrl-alt-delor about 4 years
    Thanks. Can I also say to add a DNS server for all interfaces (maybe with exceptions)? Or a list of interfaces?
  • poolie
    poolie about 4 years
    @ctrl-alt-delor yes, you can just put the prepend or supersede at the top level, with no interface block