Tell A Friend

Tell a Friend

How to Configure a Raspberry Pi as a DNS Server for the Home Network

This article is the continuation of the series “Using the Raspberry Pi as a Server for the Home Network” and describes a quick procedure to install and configure a DNS server on the RPi.

Remember that for best results you should first install the RPi server as described in my previous articles. This achieves the goal of long life of the server, thanks to the usage of a real hard disk in place of the majority of the micro SD card used by the RPi (which tends to die faster than a real HD when many read/write cycles are done on it).

First , from any computer in the home network, ssh to the RPi server and login as root.
If you have a linux computer, you can just use the ssh command to do so.
If you have a MS Windows computer, you can still install a third party free application, like PuTTY, and use that to ssh to the RPi.

Once you have a window logged in as root into the RPi, you can go through the procedure I’m about to describe.

  1. install all the packages necessary for handling the DNS server by executing in sequence the following commands:
    apt-get update
    apt-get upgrade
    apt-get install bind9 dnsutils bind9-doc
  2. the above sequence will install the DNS service and make it start right away. You can check that the service is running by issuing the following command:
    service bind9 status

    Here is an example of its output:

    root@rasp1:~# service bind9 status
    [ ok ] bind9 is running.
  3. A more detailed status of the service can also be obtained with the rndc command:
    root@rasp1:~# rndc status
    version: 9.8.4-rpz2+rl005.12-P1
    CPUs found: 4
    worker threads: 4
    number of zones: 20
    debug level: 0
    xfers running: 0
    xfers deferred: 0
    soa queries in progress: 0
    query logging is OFF
    recursive clients: 0/0/1000
    tcp clients: 0/100
    server is up and running
  4. In order to make the bind9 service start every time the RPi reboots, run the following command:
    update-rc.d bind9 defaults
  5. Now you need to stop the service to adjust its configuration the way you need it:
     service bind9 stop
  6. Make the appropriate changes to file named.conf.local, located in the directory /etc/bind. As an example, here are the contents I used in my home network:
    // Do any local configuration here
    // Consider adding the 1918 zones here, if they are not used in your
    // organization
    //include "/etc/bind/zones.rfc1918";
    zone "" {
             type master;
             file "/etc/bind/";
    zone "" {
             type master;
             file "/etc/bind/";

    Note that there are 2 zones defined in my configuration file.
    The first zone,, represents the domain name of my home network. You can choose whatever you like for your local network, as long as it is isolated from the external network by the router (possibly using NAT service), and as long as it is in the dotted form ““.
    The second zone represents the domain name of my home WEB server, that is used only inside the home network by the whole family.
    You will have to declare each zone as “master“, and will have to specify the file where the details for that zone are defined.

  7. Create the configuration files for the zones you have defined in the previous file. In my case they are and, located in the same directory /etc/bind.
    Here is an example of the file contents:

    $TTL 24H
    @ IN SOA   (
                                          2015073001        ; serial
                                          86400             ; refresh
                                          3600              ; retry
                                          604800            ; expire
                                          10800             ; minimum
    ; Name servers
               IN        NS
    ;Public servers
    ns1         IN        A
    bind        IN        A
    squid       IN        A
    router      IN        A
    ap          IN        A
    ; Private clients on the LAN
    einstein    IN        A
    dragon      IN        A
    rasp1       IN        A
    rasp2       IN        A
    laserjet    IN        A

    The first section of the file is a header that contains a number of information. You can leave everything as it is and it will work for most of the case. Otherwise, please read the bind documentation to learn how to make changes on those parameters.However, you will have to change one of the parameters in the header every time you make a change to this file. That is the serial number of the file, which is the first of the numeric fields. Every time you make a change to this file, do not forget to increment the serial number, otherwise the change will not be applied. You can use whatever rule to define the number. To easily keep track of the time when I make the changes, I use to set the serial number as the date of the day followed by a 2 digit progressive number, in case I make more than one change the same day. In this case I set the date putting first the year, then the month and lastly the day, so that the number always keeps increasing.The Name servers section is actually the name of the dns server itself, comprehensive of the domain name.The public servers section lists the name of all the other services in the network, which should have a fixed IP address. Only the names are listed, without the domain.The private clients section lists all the other elements in the network (laptops, tablets, phones, servers, …) that have a fixed IP address. Elements that do not have a fixed IP address cannot be listed in the file.Remember that all the spaces you see in the file are actually TABs. Make sure you use those and not the spaces. Also make sure that there are no tabs or spaces after the last character on each line.

  8. Modify the file /etc/resolv.conf. Here is the content of my file:

    Note that the first line describes the local domain of the home network. The second line represents the internal IP address of the server. Leave it exactly the same.

  9. Modify the file named.conf.options in the directory /etc/bind to set the DNS cache to the directory /etc/bind/cache, which should be on the hard drive, based on the installation instructions provided in the past articles. The directory information should be right at the beginning of the file. Do not modify anything else in it, unless you really know what you are doing.
  10. Copy the contents of /var/cache/bind (residing on the SD card) into /etc/bind/cache (residing on the HD).
  11. Now reboot you RPi. This will automatically start the DNS server with the configuration information you have provided.

At this point the DNS service should be happily running with your changes. You can test if it is working correctly by querying some of the IP addresses in your home network through the command line. For example, to find the IP address of the computer dragon in the home network, type:

nslookup dragon

and you should see a response similar to the following:



The address provided in the last line is the actual IP address of dragon.

Let me know if this procedure works for you. Was it useful? Clear enough to follow?

Next time we will talk about proxy servers, what they are and how they are used. Then we will learn how to install one on the Raspberry Pi.

A DNS Server for the Home Network

What is a DNS server and why would someone use it in a home network?

DNS stands for Domain Name Server. It is a server capable of translating the name of a computer or a WEB site (the URL) with its corresponding numeric IP address, which is the equivalent of the phone number of a person or a business office.

Continuing with the same analogy, The Domain Name is kind of the last name of a person identifying the whole family members, or the name of a company identifying all the locations of the company itself. Within that family or company, it is possible to address one specific person or site. In other words, the DNS is like a phone book, a directory that holds the IP addresses of several computers in the network.

In this analogy, the people are the computers and the companies are the networks were the computers are located.

Now, there are two main types of DNS servers: authoritative and caching. The authoritative servers are sometimes also known as primary and secondary servers, or master and slave servers, depending on the context and the way they are actually configured.

Authoritative servers are those that hold the addresses of a whole network domain, like

Caching servers are those that do not know about any address, but know about where to find them. When a caching server is interrogated, it launches a query to the authoritative server that it thinks has the answer, gets the information from there, and replies to the interrogation. In addition to that, it also stores the same information locally, for a certain amount of time, so that, if the same request comes in later on, it can find it in its own memory (the cache) and does not need to query the authoritative server. Once the time expires, the information is removed from the cache, and needs to be acquired again when it is needed. This guarantees that the address is periodically refreshed, in case it changes.

In addition to that, sometimes DNS servers can work simultaneously as authoritative for certain domains and as caching for others.

If you followed me so far, you may have already realized that we use DNS servers all the time, for example when we access a WEB site with a browser. Whenever we type the URL of a web site and hit ENTER, the browser contacts a DNS server to retrieve the actual numerical IP address of the site. Once the IP address is retrieved, the WEB browser can use it to send a WEB page request to the WEB site itself (well, actually to the WEB server powering that WEB site).

Normally, we use the DNS server of our own service provider, which is usually configured in the modem/router we use to connect to the internet. However, it is possible to modify the configuration of the modem/router and change the IP address of the service provider DNS server with another one, even our own.

OK, but why use our own DNS server in a home network? Well, two reasons:

  1. Having a local caching DNS server usually speeds up a little bit the queries to retrieve the IP addresses. You may benefit from a caching DNS server when you browse the WEB, because it becomes a little faster to retrieve from the cache the IP addresses of the WEB sites you visit (when available) rather than from the service provider DNS server which is located far away from your place.
  2. Some people (I am one of those) have a local network at home with a number of computers/servers. Maybe a file server to store pictures and movies, a network printer, a smart TV, or one of those consoles that allow TVs to connect to the internet, and so forth. Possibly, you may want to give names to all these network elements, and you want to address them by name rather than by their IP addresses. In that case, instead of letting your home router assign randomly chosen IP addresses (using a type of service named DHCP), you want to assign fixed IP addresses to each element and use a DNS server to translate the names in the corresponding IP addresses. It is much simpler to recall names than a bunch of numbers.

Are you intrigued with these possibilities? If so, don’t forget to read my next article, where I will explain how to create a DNS server in the home network using a Raspberry Pi as a server. If you have not done so already, go ahead and read my previous posts on the subject. Then, order an RPi and configure it as described. That will be the base for building the DNS server as I will illustrate next time.

Using the Raspberry Pi as a Server for the Home Network – Part 3

Let’s dig right away into the procedure for installing Raspbian on the RPi. I will go very quickly on the general details, as they are very well explained in several other sites. In order to execute the procedure, you will need another computer that you will be using to prepare the SD card and to pre-partition the hard drive you will be using with the RPi.

If you never installed Raspbian before, I suggest you to search for the procedure on the internet, and try to do it at least once, before attempting to do what I am about to describe. A relatively good familiarity with the tool gparted would also be nice. The tool is usually available on any Linux distribution. If you don’t find it, just use you favorite package installer to put in on your system. Gparted is also available under Windows.

Here is what you’ll need:

  1. A Raspbery Pi 2 version B. I strongly suggest to use this RPi, or a newer and more powerful version, if it becomes available when you read this article. You may want to also add a couple of heat sinks, if you plan to keep the RPi server running 24/7. They can be found easily on the internet. Here is a picture of the RPi 2B with heat sinks:rpi2_heatsinks
  2. A power supply for the RPi. Any cell phone charger with micro USB will work, as long as it provides an output current of at least 1.5 A.
  3. An external USB portable hard disk drive. I used a 1 Tb Passport from Western Digital, but any other similar device will work. You can even use an internal hard disk, provided that you buy one of those adapters to go from SATA or PATA to USB. The RPi has a USB 2.0 interface. However, if your HD has USB 3.0, it will still work, although you won’t be able to use the full speed of that interface.
  4. A power supply for the hard disk drive. Do not think to power the HD from the USB port itself, it will not work, because the RPi ports do not provide enough juice to power the HD. In my case, I connected my Passport to the RPi through one of those powered USB hubs. As a bonus, I got a few extra USB ports available on the Pi, in case I need them for future applications.
  5. A computer you can use to prepare the MicroSD card and to partition and format the hard disk drive. I am a Linux guy, so I used my main Linux computer, which runs the Fedora distro. But also Windows works fine, although you’ll have to go through a few more steps to install the tools you need to write on the MicroSD card and to partition the HD.

Once you have everything ready, you can start the installation procedure as follows.

  1. Copy on the MicroSD card the latest version of NOOBS.
  2. Connect the HD to your PC, run gparted (or whatever partitioning tool you are comfortable with), and partition the disk as in the following picture. You can adjust the size of each partition according to your needs. In my case, I even left some unallocated space for future use. Note that all the partitions created on this HD are meant to replace partitions available on the MicroSD card. The reason for the replacement is a matter of both size and reliability. The SD card does not have enough space for the needs of a server. Also, those partitions where the system writes very often will quickly make the SD card inoperable, if we keep using them over and over (see my previous article to know why). Replacing those partitions, with the ones on the HD, will greatly extend the lifetime of the RPi server.
  3. rasp1_gparted_HDExecute the procedure to install Raspbian on the RPi using the NOOBS menu. You will have to connect the RPi to the TV and to a keyboard and mouse to do so. It is also useful to connect the Pi to the network. This will allow for installing the most recent updates to the Raspbian distribution.
  4. Toward the end of the installation, the raspi-config utility will be launched. Use it to do the following:
    • Make sure that the file system is already expanded to the max size of the SD card.
    • Adjust the memory split giving the GPU only 16Mb. Since you will use the RPi as a server, you will mainly work on it through the command line to install and configure the server itself. You will not use the graphical interface, therefore you don’t need much memory assigned to the GPU.
    • Enable ssh, so you will be able to connect remotely to the RPi from another PC in the network.
    • Change the host name to something more meaningful to you. In my case, since I planned to have a number of RPi in my home network, I named this one ‘rasp1’.
    • Shutdown the Raspberry Pi and power it down.
  5. Connect the HD to one of the 4 USB ports of the RPi, attach the network cable and power up the disk, wait a 10-15 seconds, then power up the RPi. This is to make sure that when the RPi completes the initialization, the disk is already spinning and available, otherwise it could not be recognized.
  6. Figure out the IP address currently assigned to the Raspberry Pi by your router. This could be done logging into the router and look for the newest device assigned IP address. Another way would be to scan the IP addresses of your network from a PC, until you find the one you are looking for.
  7. Now, if you have a Linux PC, just fire up a terminal window and ssh to the RPi as follows:
    ssh -X pi@<ip_address_of _the_PI> 
    then enter the password raspberry.
  8. Activate the X forwarding on the PI:
    • sudo vi /etc/ssh/sshd_config
    • add the following line to the end of the file:
      X11Forwarding yes
    • save and close the file
    • restart the ssh deamon:
      sudo service ssh restart
  9. Set the time zone:
    sudo dpkg-reconfigure tzdata
    and follow the appropriate prompts.
  10. It is now time to make the changes for automounting the partitions of the external disk:
    • execute the command
      sudo blkid
      and take a note of the following information for each partition of the external disk. The external disk partitions will most probably be identified by names like /dev/sda1, /dev/sda2, and so forth. Here is an example of the command output: RPi_blkid_screenshot
    • Before mounting the /home partition, we need to save the current contents of /home/pi in some other place so, after mounting the /home partition, we can restore the pi directory in it:
      sudo cp -rp /home/pi /var/.
    • Edit file /etc/fstab (using sudo) and add one line for each of the partitions that we want to automount the next time the system is started at the following mountpoints: /var/www, /home, /tmp, /etc/bind. Each line needs to follow the following syntax (substitute the appropriate values for <UUID>, <mount_point>, and <TYPE>:
      UUID=<UUID>   <mount_point>  <TYPE>  defaults,nofail  0  2
      Here is how they look like in my case:fstab_rasp1_screenshot_1
    • Create the following mount points (the others are there already):
      • sudo mkdir /var/www ; sudo chmod 755 /var/www
      • sudo mkdir /etc/bind ; sudo chmod 755 /etc/bind
    • Reboot the PI:
      sudo reboot
    • Once the PI comes back, make sure all the four partitions are mounted, using the command df:rasp1_df_screenshot_1
    • Now restore the saved contents of the ‘pi’ directory:
      cp -rp /var/pi /home/.
  11. Since I am a Fedora person, I like to have access to the root login, so I can run long sequences of commands without the need to use sudo in front of each one of them. This is your preference, everything will work just fine even if you don’t do it. However, from now on, I will assume I am working from the root login when making changes to the system, so I will omit the prefix sudo in all the commands. If you don’t use the root login, don’t forget to use sudo. In order to add root login, just run the command
    sudo passwd
    and follow the prompts. Once that is done, you can login to the RPi as root, with the password you provided to the above command.
  12. Update the Raspbian software, to make sure you are running the latest version of the software:
    • apt-get update
    • apt-get upgrade
  13. Since we are going to use the Raspberry Pi as a server for the home network, it is better if we give to it a static IP address, rather than having it assigned dynamically by the DHCP server on the router (or any other DHCP server you may have in your network). In the process, also add a few alias IP addresses, so we can have different IP addresses for different servers running on the same system. To do so, edit the file /etc/network/interfaces and modify it as in the following picture (use your IP addresses instead). Make sure the IP addresses you choose are not in the range of the dynamically assignable IP addresses of your DHCP server (i.e. the router)rasp1_network_config_snapshot
  14. Reboot the system one more time.
  15. Create and put in use a swap partition from the external HD:
    • use gparted to verify the name of the device to which the swap partition was assigned. In my case /dev/sda7
    • mkswap /dev/sda7
    • swapon /dev/sda7
    • add the following line to /etc/fstab:
      UUID=<swap_partition_uuid>  none  swap  sw  0  0
  16. Reboot the RPi one last time.

This concludes the setup of the Raspberry Pi to make it work as a server. Next time we will start configuring the actual server applications on it. We will begin with a DNS server, then we will continue by adding a Proxy server, then a WEB server, a SAMBA server and, finally, a DLNA server. All of them can run simultaneously on the RPi 2B keeping the performance at an acceptable level for a home application.