Networking How to Install and Configure dnsmasq

30 Comments

This post will describe how to install and configure dnsmasq on a Linux- or Unix-based host. Once configured, you’ll be able to use dnsmasq to provide DNS and DHCP services in your home network.

So, why do you even need dnsmasq? Afterall, your ISP provides DNS and your home network gateway/router likely provides DHCP service for your network, right? Perhaps the best way to answer then is to explain the problem I was trying to solve. In my home network I would typically assign a static IP address to each host on my network, and then use its host file to resolve the host’s name to the IP address it was assigned. This approach allowed me to easily communicate between these hosts by simply typing their name rather than trying to remember their static IP address. However, as the number of hosts on my network started to grow, configuring static IP addresses and constantly updating the host files became unwieldy. dnsmasq solves this problem.

dnsmasq is a small, lightweight, and easy to configure caching DNS proxy and DHCP server targeted at small or home networks. It can serve the names of local hosts which are not in the global DNS, and its DHCP server integrates with its DNS server to allow hosts with DHCP-allocated addresses to appear in the DNS along with names configured either in each host or in its configuration file. dnsmasq supports static and dynamic DHCP leases and even BOOTP/TFTP for network booting of diskless hosts. dnsmasq is opensource software and is distributed under the terms of the GPL. Supported platforms include Linux, *BSD, Solaris and Mac OS X.

The versions for the software used in this post were as follows:

  • FreeBSD 7.2 RELEASE and dnsmasq v2.52,1
  • Fedora v12 and dnsmasq v2.51-1.fc12
  • Ubuntu server v9.10 and dnsmasq v2.47-3_all.deb
  • Download and Install

    I chose to install dnsmasq on my Ubuntu server-based machine. However, any host on your network in which you can add one or more external nameservers to /etc/resolv.conf and some or all of your hosts in /etc/hosts, can easily be used for hosting dnsmasq.

    To install dnsmasq on a Debian-based distribution like Ubuntu:

    On Fedora-based distributions:

    And on *BSD, if you’ve installed the Ports collection:

    Or, if you would prefer to add the package:

    The dnsmasq script will be installed in /etc/init.d, symlinked from runlevels 2-5, and start automatically in a Debian-based distribution like Ubuntu. In Fedora-based distributions, the dnsmasq script is installed in /etc/init.d; however, you will need to create a symbolic link to it from the appropriate runlevel directory in order for it to start automatically at boot time. This is typically done using chkconfig command as root. The following example shows how to add the dnsmasq script to runlevels 2-5 and start dnsmasq in Fedora:

    Newer versions of Fedora, however, may require this set of commands instead:

    In *BSD, the dnsmasq script will be installed in /usr/local/etc/rc.d. To get dnsmasq to start at boot time, add the following line to /etc/rc.conf:

    Then start dnsmasq:

    Configure

    Configuring dnsmasq is straightforward. The various DHCP and DNS options can be passed via command line when starting dnsmasq, or may be set via its configuration file, dnsmasq.conf. I generally prefer to use dnsmasq’s configuration file; it’s very well commented and easy to follow.

    Let’s walk through the changes I made to the default configuration file in order to provision both DNS and DHCP service for my network. Make sure you create a backup copy of your default file before you begin.

    To start, I uncommented the following two options to force dnsmasq to filter my local network DNS queries so they did not reach the public DNS servers.

    By default, dnsmasq will send queries to any of the nameservers you define in /etc/resolv.conf, however, it will try to favor those it knows to be up. Uncommenting the following setting forces dnsmasq to use the nameservers listed in /etc/resolv.conf strictly in the order they appear. Since I had a pretty good sense of which DNS servers I wanted to use and in what order I uncommented this line:

    By default dnsmasq will listen for DNS queries on all network interfaces. I have several interfaces on my server (Hamachi, eth0, eth1, etc.), but only one that is physically connected to my local network, so I uncommented the following line in order to force dnsmasq to listen for DHCP and DNS requests on that interface only – in my case eth0. Simply repeat the line with the another interface name if you have additional interfaces you would like dnsmasq to listen to.

    The following two lines are optional; however, if used, dnsmasq will append the domain name you choose to the host names defined in dnsmasq.conf and/or /etc/hosts. I use these, but the only real benefit I saw in my network was that I was able to ping devices such as my game consoles based on the names I defined for them using the dhcp-host parameter (see below).

    To enable dnsmasq’s integrated DHCP server you’ll need to uncomment the following line and provide the range of addresses available for lease in your network, and optionally, a lease time.

    If you have a host on your network that you’d like to have receive the same IP address every lease, then uncomment the following line and provide the host’s MAC address, as well as the preferred IP address – one from the dhcp-range you defined above. For example, I like to have the computer I use most often receive the same IP address. That way I can easily forward ports to it, etc. Alternatively, I could have simply given it a static IP address and defined the name/address combination in the /etc/hosts file of the machine hosting dnsmasq.

    If your network is anything like mine you probably have devices that don’t have a host names associated with them the same way a computer does (e.g., Xbox 360). The following parameter will assign a name to these devices in dnsmasq. You’ll need to provide the devices’s MAC address and the name you’d like associated with it. Here’s an example of how I have this defined in my network:

    By default dnsmasq assumes that host running dnsmasq is your gateway/router. That wasn’t the case in my network so I needed to specify the IP address of my Cisco gateway/router in the following line:

    The DHCP server needs somewhere keep its lease database file. I simply retained the default location chosen by dnsmasq for my Ubuntu server install. Note that this default location will vary depending on which platform your using to host dnsmasq:

    Finally, you can adjust the number of entries dnsmasq will keep in its DNS cache in the following line. I retained the default of 150.

    That’s it for configuring dnsmasq.conf. Keep in mind though that the options described here really only scratch the surface. I would strongly urge you to read through dnsmasq.conf thoroughly as there are many more options available for fine-tuning dnsmasq’s numerious capapbilities. But for now let’s move on and consider two additional files, /etc/resolv.conf and /etc/hosts, that are important when configuring dnsmasq.

      resolv.conf

    dnsmasq will consult a several locations when going about the business of resolving your network’s DNS queries. These locations include its internal cache, for any queries it may have already resolved; /etc/hosts, for any static name/IP address combinations that may be defined there; and, if the DHCP server is being utilized, it will of course know from its configuration file and lease database file which IP addresses it has assigned to the hosts configured to use DHCP. When it can’t resolve DNS queries via these methods, dnsmasq will send queries to the nameservers defined in /etc/resolv.conf. You must have at least one public DNS server defined there and it’s typical to simply use the DNS server(s) provided by your ISP. Following is an example of how I have my /etc/resolv.conf file configured. Recall that I uncommented the strict-order line in dnsmasq.conf as described above so dnsmasq will utilize DNS servers in the order I have them listed here.

      /etc/hosts

    As I mentioned, dnsmasq will consult the /etc/hosts file on the host its running on when resolving DNS queries. This comes handy when there are hosts in your network that you have assigned, or would like to assign, static IP addresses to. In those cases the host name/IP address combinations can simply be added to /etc/hosts. In fact, if desired, you could elect not to use dnsmasq’s DHCP server at all and rely soley on dnsmasq’s use of /etc/hosts to resolve local IP addresses. In this respect, /etc/hosts is no different than any other host file resident on most computers except that now you only need to maintain the one file. Of course, the tradeoff is that you’ll need to configure static IP addresses on all your hosts. I settled on a hybrid approach for my network. I configured all client hosts (laptops, desktop PCs, game consoles, etc) to use dnsmasq’s DHCP server, and configured all servers and network equipment (access point, router, network printers, etc.) with static IP addresses. Here’s an example of my /etc/hosts file:

    Final Steps

    Once dnsmasq.conf, /etc/resolv.conf, and /etc/hosts are configured to your liking restart dnsmasq:

    Or, if your using *BSD:

    Make sure to disable any other DHCP servers that may be running in your network, then simply configure your hosts to use DHCP – they should recieve an IP address that’s in the range defined in dnsmasq.conf. If you’re planning on configuring some hosts with static IP addresses, set the IP address of the host running dnsmasq as the DNS server and IP address of the gateway/router as the gateway. You’ll also want to make sure to enter that host/IP address information in /etc/hosts on the host running dnsmasq. That’s it! You now have DNS and DHCP service up and running in your network.

    Now let’s run a quick test to make sure dnsmasq is caching DNS queries. The simplest to do that is to use the dig utility:

    When you look at the output from dig and find the line showing the query time. Note the time and run the command again. You should see a noticable improvement in response time indicating that dnsmasq is caching query results locally.

    Conclusion

    This concludes the article on how to install and configure dnsmasq on your Linux- or Unix-based host. As you can see, dnsmasq isn’t terribly complicated and proves to be a really nice open source package for implementing a small, lightweight caching DNS proxy and DHCP server. For a full list of all the configuration options and other information I encourage you to visit the dnsmasq web site.

    References

    http://www.thekelleys.org.uk/dnsmasq/docs/setup.html
    http://www.thekelleys.org.uk/dnsmasq/docs/FAQ

    Tags: , , , , ,

    30 Responses to “How to Install and Configure dnsmasq”

    1. Clement Says:

      Excellent article ! So great, especially about the additional info that i did not know about, like adding other “hosts” to the /etc/hosts file, and further configuration of dnsmasq.conf. Great and thanks !!
      Clemo

    2. iceflatline Says:

      Clement, you’re very welcome; glad you liked it, and thanks much for the kinds words.

    3. Kris Says:

      Great article, thank you! Just what I was looking to do on my home network.
      Kris

    4. iceflatline Says:

      Kris, you’re welcome. Thanks for the kind compliment. Good look with your home net.

    5. James Says:

      I’ve got a similar solution, though running through dd-wrt.

      Any idea why Windows hosts, in my case Windows 7 and 2008 R2, can resolve internal hosts with nslookup fine but when I try ping those hosts it won’t work unless I append the domain name suffix.

    6. James Says:

      I forgot to mention that I have a linux host which works without appending the domain suffix

    7. iceflatline Says:

      James, thanks for your comment and question. I’m afraid I don’t have much experience with dd-wrt. I understand though that it does run dnsmasq, or at least some variation of it. According to the dnsmasq FAQ, it may be that your Win hosts are not using DNS at all but rather trying to look up the name using WINS. The solution is to make sure that all your hosts have a domain suffix set or set a domain in your DHCP server options. Check out the dnsmasq FAQ for more information. In addition, I recommend flushing the DNS cache on your Win hosts.

      [text light=”true”]
      c:\> ipconfig /flushdns
      [/text]

      Hope this helps. Good luck, and let me know what your solution was.

    8. David Szpunar Says:

      Also note that dnsmasq is included in pfSense (from another of your posts) with a very easy web interface since you are running that now. Easy to override DNS entries as needed or for entire domains.

    9. iceflatline Says:

      David, thanks! Good points. I do find, however, that the pfSense implementation of dnsmasq lacks some of the configuration options available in the stand-alone version. No worries though, pfSense more than makes up for it in so many other ways.

    10. Sudhanshu Says:

      Excellent article. Great work and Thanks

    11. iceflatline Says:

      Sudhanshu, you’re welcome. Thanks for the kind comment.

    12. Greg Says:

      Thanks for taking the time to type this up. it answered some nagging questions and ihmo, is a lot easer to read then the docs at thekellys.

    13. iceflatline Says:

      Greg, my pleasure. Thanks for taking the time to comment.

    14. How to Install and Configure dnsmasq and chrony | BohnenStroh Says:

      […] tutorial for dnsmasq is given here  and for chrony is here. As alternative to chrony the time server/client openntpd can be used for […]

    15. Jon Says:

      Very helpful, thanks. You have a small typo:

      # chkconfig dnsmasg on
      # /etc/init.d/dnsmasq start

      You have a ‘g’ instead of a ‘q’ on the first line there. The second command wont work on Fedora 17 (possiblly earlier), I suggest using this instead:

      # su
      # systemctl enable dnsmasq.service
      # systemctl start dnsmasq.service

      Thanks again.

    16. iceflatline Says:

      Jon, doh!! Good catch. I’ve revised the text to correct that error and added the commands you’ve suggested.

      Thanks for helping to improve the post.

    17. Roger Says:

      This is by far the most helpful post I have read on this subject. Thank you for the information.

    18. iceflatline Says:

      Thanks for the kind words Roger, much appreciated.

    19. techieveej Says:

      Really well explained article without noise – just the concise essentials to get it going. Thanks a lot!

    20. iceflatline Says:

      techieveej, you’re very welcome. Thanks for the kind words.

    21. Ziaya Says:

      I was struggling hard to implement dns on my FreeBSD server, dnsmasq rescued me. If I would not have seen your article, I may not have used dnsmasq, not it is my favorite. It saves me from two big different servers i.e. DHCP and DNS.
      Great Ariticle, Keep it up.
      I wish if you would write some article on PF on FreeBSD.

    22. iceflatline Says:

      Ziaya, thank you for your kind comments. Concerning PF, you might be interested in the pfSense project. pfSense is a free, open source customized distribution of FreeBSD tailored for use as a firewall and router. It has many other features as well, including a DHCP server and DNS (based on dnsmasq). I’ve used this project in many networks and it works very well. I’ve written several posts here at http://www.iceflatline.com on its use.

    23. Seb Says:

      Great post. Great Blog and AWESOME color scheme! :) Thanks!

    24. bluecon Says:

      Thank you very much. ;)

    25. Rupesh Says:

      Thank you so much, But please put the article to install and configure dnsmasq in windows

    26. Tom Says:

      Really clear explanations for why you would want to use particular options – thanks!

    27. Russell Adkins Says:

      This tutorial worked great, My BSD Server is a FreeNAS box that I also learned how to create and use JAIL’s on, I installed DNSMASQ into a FreeNAS JAIL and ran it from there and it worked spectacularly and the lookups went from +20 msec to 1msec so yes I guess it is working great article even four years later

    28. iceflatline Says:

      Russell, thanks very much for taking the time to post your comment. Very glad it worked well for you.

    29. Elias Mcito Says:

      comment installer les logiciels dnsmasq sur windows

    30. Linux Wan Load Balancing | Fatih USTA Says:

      […] olarak aşağıdakiler olsa iyi olur. 1- “dnsmasq” Buradan faydalanabilirsiniz. (Sistem DNS önbellek desteklemelidir. Aksi durumda Linux router’inizin […]

    Leave a Reply