Ok, this post is about the local DNS server I setup for myself. the motivation behind this was three-fold…
One problem I have is that whilst we have forward and reverse internal DNS setup for all our servers in the data canters (our config management system relies on it), many of the reverse zones are not delegated to the office DNS and as such, reverse lookups fail when records exist. I can point my resolv.conf elsewhere (data-centre DNS servers), but then lookups for office-based stuff fails.
Secondly, we use a Cisco VPN at work, which I have been connecting to via vpnc. Although only traffic destined for servers on the VPN actually travels down the tunnel (with other web traffic avoiding it) … vpnc (unless told not to) overwrites /etc/resolv.conf, which in turn means ALL DNS queries go down the VPN. This is so that DNS lookups of server addresses in our internal TLD (.tmcs) actually get resolved.
Thirdly, I often use my laptop out and about and it doesn’t make me feel hugely comfortable to (in most cases) have my DNS queries hit some LAN resolver which logs queries and suchlike.
So, I decided I would run my own local caching DNS server and configure different DNS server destinations for different domains. Then, when I have finished working from home, forget to close the VPN connection, and then browse to www.a-dodgy-website.com, there’s no trail left of my visit there left in the VPN logs etc. Also, I want to force my system to ALWAYS use 127.0.0.1 for lookups, and configure my DNS resolver to use the OpenDNS DNS servers for their non *.tmcs lookups.
Read on for how it’s done!
OK, so the introduction interested you?! Well, fortunately this is pretty easy to setup. First, let’s install some packages. We’re going to need a DNS server, but instead of installing bind, we’re going to use dnsmasq as it’s easy to setup and more suited to a caching setup rather than a full-blown DNS server.
$ apt-get install dnsmasq
Also, we need some way of making sure resolv.conf retains our setup. The badly-named package resolvconf does this…
$ apt-get install resolvconf
Now we need to configure dnsmasq, so lets start from scratch. Move the (heavily commented) default config file and let’s create a new one…
$ mv /etc/resolv.conf /etc/resolv.conf/default
Now, lets create the config file …
$ vim /etc/dnsmasq.conf
… and start with some basic config options
no-resolv # forces dnsmasq to NOT get it's upstream servers from /etc/resolv.conf
no-poll # more of the same ... do NOT poll /etc/resolv.conf for changes
listen-address=127.0.0.1 # Only listen on the loopback address
no-dhcp-interface=lo # disable dnsmasq's built-in DHCP server
bind-interfaces # Makes sure we only really do listen on 127.0.0.1:53
no-hosts # don't process and therefore use /etc/hosts
Well, that’s the bulk of the options, the rest is your server definitions, which will depend on your own setup. You’ll probably want some specific DNS servers for local/internal DNS and then send everything else to OpenDNS (or some other public DNS service) here are some of my options explained…
server=/.office.tmcs/172.28.10.32server=/.office.tmcs/172.28.11.32
server=/.corp/172.28.10.32server=/.corp/172.28.11.32
Ok, the above sends forward lookups for *.office.tmcs and *.corp to my local office DNS servers
server=/.tmcs/192.168.114.184server=/.tmcs/192.168.114.185
This sends everything all forward lookups under *.tmcs (which don’t get matched above) to the DNS servers in our admin cluster
server=208.67.222.222server=208.67.220.220
And this line sends all other queries to the public OpenDNS servers.
server=/10.in-addr.arpa/192.168.114.184server=/10.in-addr.arpa/192.168.114.185
server=/1-62.56.104.209.in-addr.arpa/192.168.114.184server=/1-62.56.104.209.in-addr.arpa/192.168.114.185
These are for the reverse zones. I got these from the “TargetNetworks” part of my vpnc config file. The first two of these (10.in-addr.arpa) sends all reverse lookups for 10.*.*.*, or 10.0.0.0/8 to our DNS servers in the admin cluster, which can handle those queries. The second two do the same, however they demonstrate how to specify more complex subnets. 1-62.56.104.209.in-addr.arpa relates to IP addresses in the range 209.104.56.1-62 or 209.104.56.0/26
For now, we’re just going to add another config option, temporarily so we can see things working as we expect.
log-queries
This will log ALL queries and where dnsmasq sends them, to /var/log/messages. Save that file and go back to your bash prompt.
Now, we need to add an option to our vpnc config file, so that it doesn’t overwrite our resolv.conf when it sets up the tunnel (your vpnc config filename/location will likely differ)…
$ echo "DNSUpdate no" >> /etc/vpnc/lca-internal.conf
And of course we need to setup /etc/resolv.conf to point locally, so edit that file…
$ vim /etc/resolv.conf
nameserver 127.0.0.1
Ok, save that file. Now we’re going to make sure when /etc/resolv.conf DOES get changed (by various programs such as NetworkManager), that it retains our local resolver. We just need to edit one file for that…
$ vim /etc/resolvconf/resolv.conf.d/base
nameserver 127.0.0.1
Save that one. All we need to do now is start/restart dnsmasq…
$ service dnsmasq restart
… and start our vpn (this might differ for you)
$ vpnc-connect /etc/vpnc/lca-internal.conf
And now we can open a terminal and tail the logs…
$ tail -f /var/log/messages
… Then open another terminal and try some queries, making sure they
- Are hitting your local resolver on 127.0.0.1
- Are being sent to the destinations you configured
- Are being cached (try same query a few times)
And when done, remove the query log, restart dnsmasq, and you’re good to go…
$ vim /etc/dnsmasq.conf
Remove this line
log-queries
And restart
$ service dnsmasq restart
]]>