{"id":1212,"date":"2023-12-09T15:49:55","date_gmt":"2023-12-09T03:49:55","guid":{"rendered":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/?p=1212"},"modified":"2024-01-17T17:26:41","modified_gmt":"2024-01-17T05:26:41","slug":"setting-up-a-dns-dnsmasq-server-for-your-home-network","status":"publish","type":"post","link":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/?p=1212","title":{"rendered":"Setting up a DNS (dnsmasq) server for your home network"},"content":{"rendered":"<p>\n<b>First a what this post is and is not<\/b>. <em>It covers only using <b>dnsmasq<\/b><\/em>.\n<\/p>\n<h2>Who this post is for<\/h2>\n<p>\n<b>It is<\/b> is primarily for people that have a lot of VMs running in their<br \/>\nhome network and are finding the time needed to keep \/etc\/hosts files<br \/>\nup to date on multiple machines.\n<\/p>\n<p>\n<b>It is<\/b> is for people that want a simple home network dns resolver<br \/>\nas the solution rather than investigate deployment tools that could push out<br \/>\nhosts files to dozens of VMs\/servers.\n<\/p>\n<p>\n<b>it is<\/b> for people who used to have dnsmasq working and then it suddenly all<br \/>\n broke a few years ago.<br \/>\n(which is a read the docs, the way short names were handled by default completely<br \/>\nchanged, which is good because now all clients across all OSs can expect the<br \/>\nsame responses).\n<\/p>\n<p>\n<em>And the biggee; it is for people who have tried to set it up but have issues<br \/>\nwith short names not resolving, SERVFAIL requests coming back, or just a<br \/>\ngeneral mess as a result<\/em>. This post covers off the things that are probably<br \/>\nbreaking in the way you have set it up.\n<\/p>\n<p>\nAnd see the very last section on why you would want to, you may not want to bother.\n<\/p>\n<h2>Who this post is not for<\/h2>\n<p>\n<b><em>it is definately not<\/em><\/b> for people that want to assign known ip-addresses<br \/>\n to the many devices that may connect to their home network via wireless connections&#8230;<br \/>\n as of course they get the info for wireless connections from your wireless router which<br \/>\n is outside the scope of this.<br \/>\n<br \/>\nThat includes things like laptops that may have both a cabled static address plus a wireless<br \/>\ndhcp address to your network active at the sametime, the wireless connection will really<br \/>\nmess things up as the dhcp settings happily take precedence over you static ones.\n<\/p>\n<h2>So on to DNSMASQ itself<\/h2>\n<p>\nAs I am sure you are aware dnsmasq builds it entries by reading the \/etc\/hosts<br \/>\nfile in the machine it is running on (by default, you can provide other files if you wish).<br \/>\nThat should be simple should it not ?, if your hosts file works it should also work in dnsmasq right ?.<br \/>\n<br \/>\nOf course not, your hosts files are probably populated with a mix of short names and FQDNs<br \/>\nmaking it impossible for a remote client to know what format to use.<br \/>\n<br \/>\nIf you are reading this post because you may have issues with short names not<br \/>\nresolving for some machines, sometimes long names do and sometimes they do not, or<br \/>\n generally strange and what appears inconsistent behaviour then read on.\n<\/p>\n<p>\nThe key thing is that using dnsmasq to privide DNS lookup services for<br \/>\nyour home network is that everything in your home network <em>should be in the<br \/>\nsame domain<\/em>.<br \/>\n<br \/>\nSo the issues you experience could be caused by\n<\/p>\n<ul>\n<li>you have not configured your domain in dnsmasq<\/li>\n<li>your servers were installed using defaults like localdomain or<br \/>\nif built using dhcp before assigning a static address will be in your<br \/>\nrouters domain (Home, D-Link etc) instead of your home domain<\/li>\n<li>you have made life over complicated by having both short named and FQDN names<br \/>\nin the hosts file used by dnsmasq<\/li>\n<\/ul>\n<h3>STEP1 &#8211; configure DNSMASQ properly<\/h3>\n<p>\nFirst step: in any \/etc\/hosts file on servers running dnsmasq only have the<br \/>\nshort names of the servers (ie: do not have entries like myserver1.myhomenet.org;<br \/>\nyou would only have a entry for myserver1). If you have a FQDN in there you<br \/>\nare doing it all wrong.\n<\/p>\n<p>\nSecond Step: in \/etc\/dnsmasq.conf search for the line &#8220;#local=\/localnet\/&#8221; and<br \/>\nchange it to your domain, for example &#8220;local=\/myhomenet.org\/&#8221; uncommented of course).<br \/>\n<br \/>\nThe effect of this change is that dnsmasq will append myhomenet.org to all the short<br \/>\nnames read from the hosts file it uses (short name being anything without a trailing<br \/>\ndot and data). <em>You may wonder why this is going to help if you want to lookup a short<br \/>\nname; read on as it is the clients fault<\/em>.<br \/>\n<b>Remember to change the example myhomenet.org to your domain of course<\/b>.\n<\/p>\n<p>\nThird step:<br \/>\nIn dnsmasq.conf uncomment &#8220;domain-needed&#8221; and &#8220;bogus-priv&#8221;. You do not want<br \/>\nshort name queries forwarded to the internet;<br \/>\nand we will be correcting client queries later.\n<\/p>\n<p>\nFourth step:<br \/>\nIn dnsmasq.conf uncomment &#8220;strict-order&#8221;<br \/>\n<br \/>\nOn your dnsmasq DNS server(s) you should have configured them to first lookup<br \/>\ntheir own ip-address (have your local dnsmasq server ip as the first nameserver as it<br \/>\nshould be the first queried by tools like nslookup if run on your dnsmasq host;<br \/>\nor you will find client queries work but quesries on the dnsmasq host itself do not, so you<br \/>\nmust also tell this dnsmasq server it can resolve names using itself),<br \/>\n<br \/>\n<em>if you do not have strict-order<\/em> plus your dnsmasq server first<br \/>\n<em>nameservers will<br \/>\n be selected from the list in resolv.conf in random<br \/>\n(or round robin) order<\/em> and you will end up with some queries being sent<br \/>\nto DNS servers that know nothing about your local domain (if step3 was not done<br \/>\nto the internet to resolve), and queries will randonly fail.\n<\/p>\n<p>\nFor those of you who may have got to playing with this in the past and got<br \/>\nfrustrated by some queries made on the dnsmasq host itself working and some not,<br \/>\nomitting strict-order<br \/>\nis probably why, it would have been occasionally querying the upstream server<br \/>\ninstead of itself (for queries from things like &#8220;nslookup&#8221;, tools that do<br \/>\nnot use dns but would have used the \/etc\/hosts file will still have worked<br \/>\nmasking any issue&#8230; assuming your nsswitch.conf has files as the first entry<br \/>\nwhich I think you should have in a home network, even if a clients host file<br \/>\nhas nothing but localhost in it there is always going to be an exception.<br \/>\n<br \/>\nSlightly of topic, &#8220;dig&#8221; and &#8220;nslookup&#8221; work in different ways, if &#8220;dig&#8221;<br \/>\nis working and &#8220;nslookup&#8221; is not you have a config error in your DNS setup.\n<\/p>\n<p>\n<b>That is it for the dnsmasq config but I bet it is still not working :-)<\/b>.<br \/>\nWe have to move onto the clients.\n<\/p>\n<p>\nRandom tip: if your DNS search list (\/etc\/resolv.conf) on the server running<br \/>\ndnsmasq does not contain<br \/>\nthe ip of the machine running yor dnsmasq instance <em>and the interface it listens<br \/>\non is managed by NetworkManager<\/em> the example below may help (eth0 is the<br \/>\nprimary interface on my server and .181 is the server ip&#8230; I lock it down to<br \/>\nserver ip, if you do not do that 127.0.0.1 may work as the first entry).<br \/>\nThe last nameserver (.1) is my router, so with strict order it it cannot find<br \/>\na server locally it will hunt the internet dns servers for it via the router<br \/>\nwith whatver ISP defaults were set on that.\n<\/p>\n<pre>\r\nnmcli conn modify eth0 ipv4.dns 192.168.1.181,192.168.1.1\r\nsystemctl restart NetworkManager\r\n<\/pre>\n<h2>STEP2 &#8211; configure your clients, thats the real problem<\/h2>\n<p>\nWe have dnsmasq setup correctly, why are queries for short names still failing ?.<br \/>\nBecause the clients are misconfigured of course.\n<\/p>\n<p>\nNow I do not now how many servers or VMs you have setup, but if you<br \/>\nhave accepted all the default prompts you probably have localdomain as a domain.<br \/>\nIf you installed a VM using DHCP and later changed it to a static address it probably<br \/>\nstill has the domain name assigned by the DHCP sever (or your router).\n<\/p>\n<p>\nGuess what, that is not going to work. There are two things you can do,<br \/>\nmanually edit \/etc\/resolv.conf on each server after every reboot <b>or<\/b> fix<br \/>\neach client server.<br \/>\n<br \/>\nDo it properly, fix each client server and for future installs use the<br \/>\ncorrect domain name instead of the defaults.\n<\/p>\n<p>\nYou may see in some of your client \/etc\/resolv.conf files entries like &#8220;search localdomain&#8221; or &#8220;domain localdomain&#8221; (or on some of my VMs built from DHCP before changing to static things like Home or D-Link).<br \/>\n<br \/>\n<b>That is obviously a problem that will prevent name lookups working<\/b>&#8230; (<em>well part of it as discussed later<\/em>).\n<\/p>\n<p>\nWhen a DNS lookup is performed on a short name the <b><em>client<\/em><\/b> will<br \/>\nappend to any short name (a hostname without a dot) the domain\/search value from<br \/>\nresolv.conf to the query to make a FQDN to be looked up.<br \/>\n<br \/>\nNow if you had a search value in \/etc\/resolv.conf on the client as used in the dnsmasq<br \/>\nsteps as &#8220;myhomenet.org&#8221; then the short name query would work as the client will<br \/>\nappend the correct domain part and find a match in dnsmasq which has also now been<br \/>\nconfigured to add the domain name to short names.<br \/>\n<br \/>\n<b>But<\/b> if you have left install defaults like localdomain still lying around<br \/>\n on clients that will never work (unless of course all your servers are setup for<br \/>\n localdomain and you set that as the home network domain in dnsmasq).\n<\/p>\n<h3>So, first fix the server running dnsmasq, a unique client setup<\/h3>\n<p>\nThis is what you want on the server running dnsmasq, not for clients in general.<br \/>\nThe main difference is that the server(s) running dnsmasq must only reference themselves and<br \/>\nupstream DNS resolvers, never any other dnsmasq servers you may be running for the same<br \/>\ndomain or you will be an an endless loop as they refer to each other as they try and<br \/>\n resolve a mistyped host name.\n<\/p>\n<p>\nThe assumption in the examples is that the interface name is eth0 and your dns server<br \/>\nip address is 192.168.1.181 with the next upstream server an internet (or router) dns server.<br \/>\nThe next upstream server is required as in many cases you will want to resolve<br \/>\n hostnames\/URLs out in the big wide world also.\n<\/p>\n<p>\nRHEL has pretty much dropped \/etc\/sysconfig\/network-scripts and you must use<br \/>\nNetworkManager; NetworkManager is on most Debian servers as well although you<br \/>\ncan still use the older network\/interfaces.d files there\n<\/p>\n<p>\nNetworkManager: Assuming eth0 is your interface name and we use the example myhomenet.org.\n<\/p>\n<pre>\r\nnmcli conn modify eth0 ipv4.dns-search myhomenet.org\r\nnmcli conn modify eth0 ipv4.dns 192.168.1.181,192.168.1.1\r\nsystemctl restart NetworkManager   # applies changes to resolv.conf, no connection drop\r\n<\/pre>\n<p>\nFor debian static manual interfaces in \/etc\/network\/interfaces.d\n<\/p>\n<pre>\r\nauto eth0\r\niface eth0 inet static\r\n      address 192.168.1.181\r\n      netmask 255.255.0.0\r\n      gateway 192.168.1.1\r\n      dns-nameserver 192.168.1.181\r\n      dns-nameserver 192.168.1.1\r\n      dns-search myhomenet.org\r\n      dns-domain myhomenet.org\r\n<\/pre>\n<h3>When the dnsmasq server is correct, update\/correct all your clients<\/h3>\n<p>\nIt goes without saying that to avoid doing this often, always use the correct domain name<br \/>\nand dns list when building a new VM or server so it will never need to be done on those.\n<\/p>\n<p>\nIt is important to note a few things here about clients in general that do not run<br \/>\nany copies of dnsmasq themselves.\n<\/p>\n<ol>\n<li>they can refer to multiple dnsmasq servers on your home network so they can<br \/>\n resolve names if one is down<\/li>\n<li>while you can also include an upstream DNS server that will probably stop<br \/>\nthings working correctly again, you should only search your home network dnsmasq servers<\/li>\n<\/ol>\n<p>\n<em>The second point above is an important one<\/em>, you will remember we configured dnsmasq<br \/>\nto use strict order to always check the home network dnsmasq instance first.<br \/>\n<br \/>\nGuess what, clients also are unlikely to use a strict order.<br \/>\nDepending upon what version of operating system you are running lookup operations<br \/>\nby the client are quite likely to round-robin through the nameserver list rather<br \/>\n than use strict order so can<br \/>\nquite easily query the upstream server instead of your dnsmasq server;<br \/>\n<em>this is something else that can cause dns lookups of servers in your home network<br \/>\nto sometimes work and sometimes not<\/em>.\n<\/p>\n<p>\nThe solution I use is to have two dnsmasq servers so one is always available,<br \/>\nall clients only use those two for all name resolution (yes, including external)<br \/>\nand no client has any internet dns resolver address configured relying on the<br \/>\ndnsmasq server instead.<br \/>\n<br \/>\nAs the dnsmasq servers are configured with an upsteam dns then any external<br \/>\nhost name they are unable to resolve themselves (ie: google.com) the dnsmasq servers<br \/>\nwill query the external DNS and resturn the correct ip-address for the name to the client<br \/>\nrequesting it. The client does not need an external dns entry and not having one<br \/>\navioids lookup problems if a client round-robins through name servers.<br \/>\n<br \/>\nRemember this is just for the dns name resolution, once the client has the ip-address<br \/>\nit will cache it and all traffic is from the client direct to the ip-address (unless you go<br \/>\nthrough a proxy of course).\n<\/p>\n<p>\nBased on the examples for dnsmasq above with a single server providing dnsmasq,<br \/>\nand letting that dnsmasq server query any upstream dns resolvers on behalf of the client(s)<br \/>\nthen a client configuration would be based on the above examples as below.<br \/>\nIf you have a second dnsmasq servers just add it to the dns ip list,<br \/>\n as long as the two dnsmasq servers do not reference each other in any way to avoid<br \/>\n the endless lokkup loop situation.\n<\/p>\n<pre>\r\n# NetworkManager\r\nnmcli conn modify eth0 ipv4.dns-search myhomenet.org\r\nnmcli conn modify eth0 ipv4.dns 192.168.1.181\r\nsystemctl restart NetworkManager   # applies changes to resolv.conf, no connection drop\r\n<\/pre>\n<p>\nFor debian static manual interfaces in \/etc\/network\/interfaces.d\n<\/p>\n<pre>\r\n# Debian \/etc\/network\/interfaces.d files (xxx is the client ipaddr)\r\nauto eth0\r\niface eth0 inet static\r\n      address 192.168.1.xxx\r\n      netmask 255.255.0.0\r\n      gateway 192.168.1.1\r\n      dns-nameserver 192.168.1.181\r\n      dns-search myhomenet.org\r\n      dns-domain myhomenet.org\r\n<\/pre>\n<h2>Congradulations,it now of course works<\/h2>\n<p>\nYou can lookup your internal servers from any client by either short or FQDN names<br \/>\nusing yout home network domain without<br \/>\nany issue; and internet names are still resolvable for all your clients.\n<\/p>\n<h2>Troubleshooting<\/h2>\n<ul>\n<li>&#8220;dig&#8221; works but &#8220;nslookup&#8221; does not. You have misconfigured it<\/li>\n<li>to check a nameserver &#8220;nslookup hostnametolookup dnsserveripaddr&#8221;,<br \/>\nif querying the dns server explicity by dnsserveripaddr returns the<br \/>\nresults expected there is nothing wrong with it, the issue is your nameserver search order<\/li>\n<\/ul>\n<p>\nYou could of course manually edit \/etc\/resolv.conf to correct search and nameserver<br \/>\nentries for testing, those changes would be lost pretty quickly.\n<\/p>\n<p>\nFor search this may be an option for testing.<br \/>\n(Ref: https:\/\/man7.org\/linux\/man-pages\/man5\/resolv.conf.5.html)<br \/>\nThe search keyword of a system&#8217;s resolv.conf file can be<br \/>\noverridden on a per-process basis by setting the environment<br \/>\nvariable LOCALDOMAIN to a space-separated list of search domains.<br \/>\n<br \/>\nThat depends on the client inplementing it of course.\n<\/p>\n<h2>The big question of course is why you would want to<\/h2>\n<p>\nSo why would you want to run a home network DNS server ?.\n<\/p>\n<p>\nThe main reasons for needing a DNS server for your home network would be<br \/>\nthat you have a lot of servers or VMs and trying to keep their hosts files<br \/>\nall syncronised is becoming too much effort; you want to be able to just<br \/>\nedit a single file and all your servers be able to use it.\n<\/p>\n<p>\nThis would only be an issue if you did not have any sort of deployment<br \/>\ninfrastructure like ansible\/chef\/puppet that could deploy a &#8220;template&#8221;<br \/>\nhosts file to all your servers from a single source file; and yes I do<br \/>\nmean template file not a static hosts file sas each hosts file would have to<br \/>\nbe correctly set with the ip and hostname of the server it is being deployed to<br \/>\nas so many things depend on that.\n<\/p>\n<p>\nNow suppose you did run two dnsmasq servers, without a deployment tool<br \/>\nto push a central edited hosts file to both servers and restart dnsmasq<br \/>\nyou are already editing two hosts files for every change now. Still a lot<br \/>\nless than the effort of doing so on every server but they could also get out<br \/>\nof sync if manual edits on each are required.<br \/>\n<br \/>\nYou should (if you have enough headroom for another small VM on a different<br \/>\nphysical server) run two copies; if you followed the post I have made here you<br \/>\nwill have all your clients now doing internat name resolution via your dnsmasq<br \/>\nservers upstream queries so if you have a single dns server and it stops<br \/>\nyou have lost name resolution to not only your servers but to the internet<br \/>\n(which is not an issue, you just edit \/etc\/resolv.conf to insert a nameserver<br \/>\nline for your router or maybe one of googles nameservers to get internet access<br \/>\nback for that client so you can at least watch youtube while your dnsmasq server reboots).\n<\/p>\n<p>\nUsing something like puppet-ce or ansible would let you deploy a &#8216;source&#8217;<br \/>\nhosts file to both dnsmaqs servers so you only need to edit the file in one place,<br \/>\nhowever you could also go wild and use them to deploy the hsosts file to all<br \/>\nyour servers negating the need for a home network dns server at all&#8230; the drawback with<br \/>\nthe later of course being that anyone that can see your \/etc\/hosts file would then<br \/>\nknow every machine in your network; best to have it on as few machines as possible.\n<\/p>\n<p>\nDeployment tools have a learning curve you may not be interested in, so for<br \/>\na home network dns setup I would say just run two dnsmasq servers and on only<br \/>\nthose two servers have a rsync job that runs occasionally to check for an<br \/>\nupdated hosts file on whatever server you want to make edits to it on. Or if you<br \/>\nonly want to run one then there is only one file to edit so no syncing needed at all.<br \/>\n<br \/>\nOnce you get above 4-5 VMs manually keeping \/etc\/hosts files up-to-date<br \/>\non each becomes a nightmare. A home network DNS resolver (or two) becomes<br \/>\nessential.\n<\/p>\n<p>\nHopefully this long winded post has got you past any issues you were having<br \/>\nwith setting on up using dnsmasq.\n<\/p>\n<p><b>One last note: if the changes above do not result in network or NetworkManager startup correctly setting \/etc\/resolv.conf then&#8230;<\/b> it could simply be you do not have NetworkManager\/resolved or similar service installed that updates it; in which case simply vi\/nano the resolv.conf file and set the values you want. That took me ages to work out when a new VM refused to correctly set resolv.conf, until a check on the last modified time showed nothing was updating it on a reboot at all. <b>So a new first step, see if the file is being updated on boot and if not just manually edit it<\/b>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>First a what this post is and is not. It covers only using dnsmasq. Who this post is for It is is primarily for people that have a lot of VMs running in their home network and are finding the &hellip; <a href=\"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/?p=1212\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-1212","post","type-post","status-publish","format-standard","hentry","category-my-nux-thoughts-and-notes"],"_links":{"self":[{"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1212","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1212"}],"version-history":[{"count":3,"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1212\/revisions"}],"predecessor-version":[{"id":1230,"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/1212\/revisions\/1230"}],"wp:attachment":[{"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1212"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1212"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mdickinson.dyndns.org\/php\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1212"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}