Laptop DNS forwarding to DHCP DNS

I run bind9, a DNS server, on my Ubuntu laptop so that I can use more advanced local dns resolution than what /etc/hosts can provide, such as wildcard domain names.

The problem is that in order for me to be able to use that DNS server whilst being able to resolve internet domains at the same time used to require me to do two things:

  • Edit /etc/resolv.conf, and change nameserver to
  • Edit /etc/bind/named.conf.options, and add a forwarder to the nameserver IP that was previously in /etc/resolv.conf

Obviously this is too annoying to have to do every time the dhcp renews, wiping my /etc/resolv.conf changes. Also, if I am frequently changing networks, as I do on my laptop. I have to go and update the bind forwarders again.

In came the solution I wrote in Ubuntu 8.04 (Hardy), using the dhcp-script hook directory in /etc/dhcp3/dhclient-exit-hooks.d/, which scripts inside get called when dhcp renews, after the /etc/resolv.conf, I was able to write a script that did the above automatically.


echo $new_domain_name_servers | sed -e '
iforwarders {\n
s/\([^ ]\+\)\s*/ \1;\n/g
"$NAMED_INITD" restart
sed -e "
\$anameserver $BIND_IP
/^\s*nameserver/i# moved following nameserver to bind forwarders
s/^\s*nameserver/# nameserver/" -i "$RESOLV_CONF"

This will read the dhcp announced nameserver from the parameters dhclient sends to it, create a /etc/bind/named.conf.forwarders configuration file with the dhcp nameserver in, and rewrite the /etc/resolv.conf nameserver to, and restart bind.

Now you just need to include the /etc/bind/named.conf.forwarders in your /etc/bind/named.conf.options file:

options {
        directory "/var/cache/bind";
        include "/etc/bind/named.conf.forwarders";
        auth-nxdomain no;    # conform to RFC1035

As I mentioned in the post Workaround so NetworkManager runs dhclient hooks, when I upgraded to Ubuntu 8.10 (Intrepid), this script no longer ran. Please read this article to make the script work again.