Configure a DNS server with your Raspberry Pi

This is just a quick recipe so you can have a DNS server on your local network. In my case, I wanted to log every DNS query made from my network into a custom file.

This is useful if you want to know, for example as was my case, where your mobile phone is accessing to when it’s connected to your wireless network after installing an app (among other hacker/spy things that I’m not going even to mention 🙂 ).

First, the most important is to set up your router to add the IP of your Raspberry as the DNS server. This step depends on your router, so I cannot tell that much about it. You’ll have to log in your router admin interface (usually http://192.168.1.1) and look for the DHCP section. Once there you should find the DNS servers field. Then put the IP of your Raspberry Pi as the first DNS entry, and apply the changes.

Well, once the router is configured, let’s start with the Raspberry Pi configuration.

First. install the needed packages. Either as root or with sudo, execute:

Then, you have to edit the /etc/bind/named.conf.options file so that it looks like this:

After that you have to create and set the owner of the logs file, and restart the bind daemon. Again, either as root or using sudo, execute:

And that’s all. Now you can run the following command to see the DNS queries in real time:

You’ll see something similar to this (in my case, the IP of my Raspberry PI is 192.168.1.77, and the connected client is 192.168.1.100):

So far so good. Then I also wanted to add a custom domain 🙂

The /etc/hosts file is not writable in Android without a rooted phone, so this is the way to do this. Please note that the files name and content will vary depending on your network IP range and the domains you want to add to the DNS, but the it will be analogous, so this are pure orientative values:

Then edit the /etc/bind/named.conf.local to add the following blocks:

And finally, the zone files:

/etc/bind/zones/db.whatever.domain.com :

/etc/bind/zones/db.192 :

Then, just a DNS restart and it will be ready:

 

 
Tagged , . Bookmark the permalink.

7 Responses to Configure a DNS server with your Raspberry Pi

  1. Viviana says:

    Hi JuanRa

    Thanks for the info. I’m new in all this raspberry world.. wich model would you recommend to run this script?

    Looking forward for your response. Regards!

    • JuanRa says:

      Hi Viviana,

      The DNS server (bind9) doesn’t require too much resources for a small home network, typically with 2-10 devices. So I think any model would be ok. Nonetheless I used a Model B+ to do this 🙂

      Cheers!

  2. Jack Glazko says:

    Hi JuanRa, great blog entry! Made it easy to set up a bind server on the RPi for some testing.

    I believe I found one issue in following these directions. After configuring as above, syslog was telling me when I tried to start BIND:

    When I looked at the config in named.conf.options, it looks like the channel was set up as “bind.query.log”, but a few lines below it is looking for a category of “dns.query.log”. Once I changed that category line to say

    and restarted bind everything took off and ran fine.

  3. Zac S says:

    I have no experience with networking and DNS but had a need to set up a local dns.  I went through several other tutorials, this was the only one that has worked for me, Thanks you so much.  Can i ask where you learned this? is there a comprehensive book or resource?

    • JuanRa says:

      Hi Zac, I’m glad this was useful for you. About your question, I don’t remember exactly, but I guess it was a combination of other tutorials, reading docs, and a “trial and error” process looking the server logs.

  4. Haroldo says:

    Hi, thanks for sharing this tutorial, I have a question, I need to host a dns server (not local), in order to redirect my domain (example.cl) to my public ip. How can i do that using my raspi and bind? I guess it is a very similar process, but i don’t know how. Can you help me?

     

    Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *