Quick local DNS caching for your workstation in Ubuntu

The latest Ubuntu releases makes it real easy to set up a local DNS cache for your workstation using dnscache from the well-known djbdns software by D. J. Bernstein.

For those who have historically installed djbdns/ucspi-tcp/daemontools from source because of distribution restrictions, things changed really for the better after DJB placed all these software in the public domain in 2007. You can now setup all this in about one minute! (depending on your Internet connection though. :-P )

To setup a local dns cache in Ubuntu (specifically, 10.04 Lucid Lynx on which I tried this), right now you need to do:

  1. Install the necessary packages:

    $ sudo apt-get install daemontools daemontools-run djbdns \
                         dnscache-run ucspi-tcp
     
    

    This installs all the necessary packages using Debian's filesystem layout (not DJB's). So all services are symlinked in `/etc/service` (and not in `/service`). dnscache files are supposed to be accessed in `/etc/dnscache`, and are already configured to use the root servers.

  2. Start daemontools: The daemontools scripts (actually just svscan) are setup in Ubuntu to be run using upstart and not /etc/inittab. So all you need to do now is run:
    sudo start svscan
    
  3. Making the computer use the local cache permanently. The install scripts actually replace your dns servers in /etc/resolv.conf with the loopback address on which dnscache runs. So you would have already started using the dns cache already. However, if you are using DHCP with Network Manager, these changes will be overwritten the next time NetworkManager modifies resolv.conf, e.g. when the lease is renewed or you plug-in a DHCP interface again. To make this permanent, edit /etc/dhcp3/dhclient.conf, and uncomment/add a line saying prepend domain-name-servers 127.0.0.1;. Disconnect and connect to your DHCP interface again, and see the loopback address (127.0.0.1) being added at the top of the name servers in /etc/resolv.conf .

That is it.

As usual, here are the other commands that you would be doing to manage dnscache. Read the docs for more details.

tech
The magical moment that made me a FOSS guy forever The constant conflict between the Maker's Schedule and Manager's Schedule