2011-06-01

How to force a local DNS resolver to be used using resolvconf

I know it has been a while, but after reading a blog post by Anand Kumria over at planet.debian.org I decided to have a quick look at one of the problems he described.

Basically, Anand wants to force the local resolver to be used for each and every network connection, may that connection be established manually or via NetworkManager. He wrote that fixing this configuration for every new connection manually is tedious, and I fully agree on that. So here is a solution to do this all automatically, using resolvconf:

After installing the resolvconf package every time /etc/resolv.conf is to be updated resolvconf takes care of that. Using the files in /etc/resolvconf this process can be controlled and the resulting file modified to fit one own's needs.

So at first we would like the local resolver to be used for every connection. This works by simply adding the "nameserver 127.0.0.1" directive to the /etc/resolvconf/resolv.conf.d/head file. Simple as that. Every time /etc/resolv.conf gets generated the contents of the head file are actually used as /etc/resolv.conf's header.

Using this method the local resolver is used for every connection. But Anand wanted to use only the local resolver and discard any resolvers possibly obtained via DHCP for example. Guess what, this is also possible using resolvconf.

Adding TRUNCATE_NAMESERVER_LIST_AFTER_127="yes" to /etc/default/resolvconf does exactly that. Now every nameserver directive after the 127.0.0.1 one is ignored and will not make it into /etc/resolv.conf. You can of course add more nameservers to the head file above the 127.0.0.1 directive.

Problem fixed I guess.
Don't forget to re-connect to the network or manually force re-creation of /etc/resolv.conf so the changes you made get populated. I really hope this is of use to some of you facing similar problems.

3 comments:

  1. Adding TRUNCATE_NAMESERVER_LIST_AFTER_127="yes" to /etc/default/resolvconf

    This suggests that this will only work if the nameserver you wish to always exist is "127.0.0.1". What happens if you wish to use some other DNS server? Does similar functionality exist, or is the option poorly named, and not requiring 127.0.0.1?

    ReplyDelete
  2. I haven't checked yet to be honest, but I guess it only allows 127.0.0.1.

    If you want to use other servers you could still put them above the 127.0.0.1 entry in the head file. Now if you have a bogus entry, like the one for localhost which is not running a nameserver, the resolver should just skip it, from my understanding at least. Correct me if I'm wrong there.

    And yes, this is a hack, but it should work.

    ReplyDelete
  3. If you want to use the local resolver, Just remove /etc/resolv.conf

    This is explained in resolv.conf's man page.

    ReplyDelete