You'll dig this

Quick post: how to set up local DNS caching under Ubuntu. I sometimes find that there's a long delay when launching applications. Often this is just the system working hard to start them, but sometimes there's no load being generated whilst everything sits there for a couple of seconds.

It occurred to me that this might be due to a network timeout or sluggish response of some kind, and a quick look at the output of tcpdump proved that there was indeed a DNS query going on every time I launched xterm.

Rather than sit and wait for the DNS server to get back to me, I decided to install a local DNS server to cache responses. These instructions are a bit basic, but they should show you what needs to be done. Good luck!

  • Have a look at what DNS you're getting a the moment.
    root@box:~# dig google.com | grep -A 1 Query
    ;; Query time: 146 msec
    ;; SERVER: 10.0.0.254#53(10.0.0.254)          
    
  • Install the package.
    root@box:~# aptitude install dnsmasq
    
  • Edit the server config to enable local serving.
    root@box:~# vi /etc/dnsmasq.conf
      [look for a line starting '#listen-address=']
      [remove that initial '#' and add '127.0.0.1']
      [result should be 'listen-address=127.0.0.1']
      [save file and exit]
    
  • Add 127.0.0.1 to your list of nameservers.
    root@box:~# vi /etc/resolv.conf
      [insert a new first line that reads 'nameserver 127.0.0.1']
      [save file and exit]
    
  • Every time DHCP gets a new lease, resolv.conf will be re-written. Edit the DHCP configuration to always add the local DNS server entry that we added in the previous step. (You might want to check that the last step hasn't been re-written whilst you've been doing this one.)
    root@box:~# vi /etc/dhcp3/dhclient.conf
      [find the line starting '#prepend domain-name-severs']
      [remove the initial '#' character]
      [line should read 'prepend domain-name-servers 127.0.0.1;']
      [save file and exit]
    
  • Launch the server.
    root@box:~# /etc/init.d/dnsmasq start
    
  • Check the local server is being used, and see how much faster it is.
    root@box:~# dig google.com | grep -A 1 Query
    ;; Query time: 0 msec
    ;; SERVER: 127.0.0.1#53(127.0.0.1)