How to set static DNS with dhclient while resolv.conf keeps getting overwritten?

8,595

Solution 1

Use the following workaround to prevent the dhcp client from updating your carefully crafted /etc/resolv.conf:

# chattr +i /etc/resolv.conf

Solution 2

I think that your DHCP server sends unsolicited responses, so your resolv.conf was overwritten also if you set the supersede parameter. Adding the +i attr may be a solution but requires that you manually unset and re set it if you have to do some changes.

You can, indeed, write some scripts on the client side (see man 8 dhclient-script).

This topic is also explained in debian wiki:

Another approach makes use of dhclient-script's hook scripts. According to dhclient-script(8):

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 behaviour, redefine this function in the enter hook script. Therefore, we can stop dhclient from overwriting resolv.conf by doing the following:

echo 'make_resolv_conf() { :; }' > /etc/dhcp/dhclient-enter-hooks.d/leave_my_resolv_conf_alone
chmod 755 /etc/dhcp/dhclient-enter-hooks.d/leave_my_resolv_conf_alone

The execute bit is required because dhclient-script uses run-parts(8) to decide which files to read. For that same reason, the filename must not contain anything but letters, digits, underscores and hyphens.

Share:
8,595

Related videos on Youtube

lscstu22
Author by

lscstu22

Updated on September 18, 2022

Comments

  • lscstu22
    lscstu22 over 1 year

    Following the Debian NetworkConfiguration wiki, I've been trying to setup static OpenNIC DNS with no success. The first thing I tried was directly editing /etc/resolv.conf, but it keeps getting overwritten. The wiki page lists three possible sources of the overwrite:

    1. The resolvconf program
    2. The network-manager daemon
    3. DHCP clients
    $ apt-cache policy resolvconf
    resolvconf:
      Installed: (none)
      Candidate: 1.76.1
      Version table:
         1.76.1 0
            500 http://ftp.us.debian.org/debian/ jessie/main amd64 Packages
    $ apt-cache policy network-manager
    network-manager:
      Installed: (none)
      Candidate: 0.9.10.0-7
      Version table:
         0.9.10.0-7 0
            500 http://ftp.us.debian.org/debian/ jessie/main amd64 Packages
    

    Considering I don't have resolvconf or network-manager installed, we can assume the source is a DHCP client. I'm using wicd as an alternative to network-manager, but setting up static dns in the wicd-gtk properties doesn't work. Thus, I edited /etc/dhcp/dhclient.conf by adding supersede domain-name-servers 50.116.40.226;, but my /etc/resolv.conf is still:

    $ cat /etc/resolv.conf
    nameserver 2001:558:feed::2
    nameserver 2001:558:feed::1
    

    What is the hell is going on here? Should I install resolvconf to see if it will work? Should I give up on wicd and install network-manager?

    • ctrl-d
      ctrl-d over 8 years
      It's probably a bug in dhclient, which i also ran into. It ignores the supersede. I ended up making /etc/resolv.conf immutable with chattr. But i'm interested in a permanent solution too.
    • lscstu22
      lscstu22 over 8 years
      @ctrl-d Bravo, sudo sh -c 'printf "name server 169.57.14.220\nnameserver 190.10.8.128\n" > /etc/resolv.conf' && sudo chattr +i /etc/resolv.conf works! Post an answer along the same lines, and I'll accept it
    • ctrl-d
      ctrl-d over 8 years
      You're welcome. Good that it works for you too.
    • lscstu22
      lscstu22 over 8 years
      @roaima sorry for the late reply, but no it wasn't: -rw-r--r-- 1 root root 49 Dec 14 18:19 resolv.conf
    • Patrick Mevzek
      Patrick Mevzek about 6 years
      What version of Debian do you use? If this is a recent one, you have systemd on it, and it overwrites /etc/resolv.conf on networl changes.
  • lscstu22
    lscstu22 over 8 years
    I'd like to add I had to combine the editing and chattr commands into one with && for this to work since /etc/resolv.conf was being overwritten so quickly. Check out the question comments above.
  • ctrl-d
    ctrl-d over 8 years
    I just put in: nameserver 127.0.0.1 and chattr-ed it.