This site is now 100% read-only, and retired.

XML logo

dns caching with pdnsd
Posted by mar on Tue 25 Aug 2009 at 11:58
Tags: , ,

Intro

Being a long time user of pdnsd (small and simple DNS caching daemon), I have always struggled a bit when using it with dhclient.

I have always used some custom hook scripts (pre-up, post-up, ...) in /etc/network/interfaces when I was configuring a device with dhcp to supply the just generated /etc/resolv.conf to pdnsd and generate a new one with nameserver 127.0.0.1 line in it to enable local caching. It got a bit more complicated with shorewall. Anyway, it has been a somewhat working setup, even with mapping stanzas in /etc/network/interfaces.

Before going on, I should mention I just hate the resolvconf debian package, which never seems to be doing what I want. (You may give it a try yourself.)

I was reading through the dhclient man page in hope there are some hooks or something and to my surprise, I learned they are! So finally, the much improved "receipt" for happy networking with laptop with several virtual machines, local DNS zones, local DNS caching and more ... ;-).

So much for the intro. Now the thing.

Setup

  • pdnsd should be able to use the info provided by dhclient, thankfully, it has a feature for this. Apart from other standard server stanzas, it should contain this server section. It points to file with standard resolv.conf syntax and it could be for example resolv.conf file generated by dhclient.

server {
    label = "resolv.conf.from.dhclient";
    file = "/etc/resolv.conf.from.dhclient";
}
  • Debian has another nice feature -- /etc/dhcp3/dhclient-enter-hooks.d/ and /etc/dhcp3/dhclient-exit-hooks.d/ directories, that can contain various scripts that are run before and after the dhclient obtains its info from server (and writes down the new /etc/resolv.conf). All i have to do is to prepare 2 simple custom scripts for pdnsd. This scripts are sourced ('.') by shell during startup/rebound/stop/... actions of dhclient.

/etc/dhcp3/dhclient-enter-hooks.d/dhclient-pdnsd-enter-hook This script just uses predefined variables by /sbin/dhclient-script and modifies them to its needs -- particullary the $new_domain_name_servers var:


# place a link to this file in /etc/dhcp3/dhclient-enter-hooks.d

echo "I: prepending 127.0.0.1 as local caching DNS server"
new_domain_name_servers="127.0.0.1 $new_domain_name_servers"
echo "I: new_domain_name_servers=$new_domain_name_servers"

and

/etc/dhcp3/dhclient-exit-hooks.d/dhclient-pdnsd-exit-hook This script generates the file mentioned in the server section of pdnsd above and notifies pdnsd that the config changed and it should re-read the DNS info.


# place a link to this file in /etc/dhcp3/dhclient-exit-hooks.d

pdnsd_resolv_conf="/etc/resolve.conf.from.dhclient"
echo "I: creating $pdnsd_resolv_conf"
cat /etc/resolv.conf | fgrep --invert-match -e "127.0.0.1" > $pdnsd_resolv_conf
echo "I: notifying pdnsd"
/usr/sbin/pdnsd-ctl config

Outro

With a setup like this, which integrates nicely into debian ways I can use pdnsd, which i like, for a long time, without worrying that future DHCP REBOUND will overwrite my /etc/resolv.conf leaving out my custom changes to this file -- ie. my local cache and local zones for virtual machines defined in pdnsd.

This set up also integrates with /etc/network/interfaces in a completely transparent way -- on a laptop, when you may be connected via bluetooth, wlan, eth, ppp -- this always ensures, that once DHCP is used, local DNS set up will nicely prevail no matter what interface is in use.

I would like to hear from you, how you handle DNS on your laptop, what tools you use and why. Until then, another happy day with debian, i must do ifdown wlan0 just to see it working again :).

 

Comments on this Entry

Re: dns caching with pdnsd
Posted by Anonymous (97.65.xx.xx) on Tue 25 Aug 2009 at 16:59
I use dnsmasq for my DNS caching, though it's possibly overkill compared to pdnsd.

Dealing with dhclient is fairly easy. Edit the following file /etc/dhcp3/dhclient.conf

above the "request" line, tack in:
prepend domain-name-servers 127.0.0.1;

Then whenever dhcp kicks in 127.0.0.1 will be above the domain-name-servers in resolv.conf that your DHCP server provides you :)

[ Parent ]

Re: dns caching with pdnsd
Posted by mar (90.183.xx.xx) on Wed 26 Aug 2009 at 07:40
[ View Weblogs ]

great tip, thanks, much more simple.

[ Parent ]

Re: dns caching with pdnsd
Posted by mar (193.85.xx.xx) on Wed 26 Aug 2009 at 08:14
[ View Weblogs ]

anyway, i do believe, that for the nameserver 127.0.0.1 should be the only one asked, and it itself should forward any DNS request -- in order to be able to cache them, so this tip just removes the script number one from my setup, correct?

[ Parent ]