{"id":7934,"date":"2020-03-23T16:53:14","date_gmt":"2020-03-23T08:53:14","guid":{"rendered":"http:\/\/rmohan.com\/?p=7934"},"modified":"2020-03-23T16:55:50","modified_gmt":"2020-03-23T08:55:50","slug":"dnsmasq-centos7","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7934","title":{"rendered":"Dnsmasq  Centos7"},"content":{"rendered":"\n<p>One could only guess that the rationale for lack of DNS caching in RHEL is the arguable efficiency for those systems which aren\u2019t network connected or simply don\u2019t need to make any DNS lookups.<\/p>\n\n\n\n<p>There are of course such cases where you don\u2019t need (many) DNS resolutions. I can think of:<\/p>\n\n\n\n<ul><li>a dedicated DB server<\/li><li>a private server where all hosts are listed in the&nbsp;<code>hosts<\/code>&nbsp;file<\/li><\/ul>\n\n\n\n<p>Those systems will likely issue zero to none DNS lookups while running, and DNS cache isn\u2019t really a thing for them.<\/p>\n\n\n\n<p>But for the most intents of running either a desktop or server RHEL machines,&nbsp;<strong>you will absolutely benefit from a DNS cache.<\/strong><\/p>\n\n\n\n<p>Enabling DNS cache in RHEL 7 and 8 is easy thanks to&nbsp;<code>dnsmasq<\/code>&nbsp;integration of NetworkManager.<\/p>\n\n\n\n<p>The&nbsp;<code>dnsmasq<\/code>&nbsp;is a very lightweight caching DNS forwarder which runs great even on the tiniest hardware like your very own home router.<\/p>\n\n\n\n<p>I won\u2019t torture you with long instructions on how to enable the DNS cache. It\u2019s really quick and goes down to:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>yum -y install dnsmasq\n\ncat &lt;&lt; 'EOF' | sudo tee \/etc\/NetworkManager\/conf.d\/dns.conf \n[main]\ndns=dnsmasq\nEOF\n\nsystemctl reload NetworkManager<\/code><\/pre>\n\n\n\n<p>You have just made your machine already faster by running these.<\/p>\n\n\n\n<p>For more details and fine-tuning, read on.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">NetworkManager and dnsmasq<\/h2>\n\n\n\n<p>Let\u2019s explain what happened when we ran the above commands to enable DNS caching.<\/p>\n\n\n\n<p>In the first bit, we have installed the very essential of DNS caching \u2013&nbsp;<code>dnsmasq<\/code>&nbsp;program.<\/p>\n\n\n\n<p>Then we write out a file,&nbsp;<code>\/etc\/NetworkManager\/conf.d\/dns.conf<\/code>, with contents telling NetworkManager to enable and use its&nbsp;<code>dnsmasq<\/code>&nbsp;plugin. Then we reload NetworkManager configuration to apply our changes.<\/p>\n\n\n\n<p>This, in turn, starts a private instance of&nbsp;<code>dnsmasq<\/code>&nbsp;program, which is bound to the loopback interface,&nbsp;<code>127.0.0.1<\/code>&nbsp;and listening on standard DNS port, 53.<\/p>\n\n\n\n<p>It doesn\u2019t end there. NetworkManager now updated&nbsp;<code>\/etc\/resolv.conf<\/code>&nbsp;and put&nbsp;<code>nameserver 127.0.0.1<\/code>&nbsp;so that the whole operating system will perform DNS lookups against its&nbsp;<code>dnsmasq<\/code>&nbsp;instance.<\/p>\n\n\n\n<p>The&nbsp;<code>dnsmasq<\/code>&nbsp;itself will use whatever nameservers you had setup in NetworkManager explicitly, or the ones provided by DHCP requests.<\/p>\n\n\n\n<p>Very clean and beautiful integration.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Verify dnsmasq is working<\/h2>\n\n\n\n<p>Simply perform a DNS lookup using&nbsp;<code>dig<\/code>, against&nbsp;<code>127.0.0.1<\/code>\u201d<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># yum -y install bind-utils\ndig +short example.com @127.0.0.1<\/code><\/pre>\n\n\n\n<p>If the output looks like a valid&nbsp;<abbr title=\"\">IP<\/abbr>&nbsp;address or a list of IP addresses, then&nbsp;<code>dnsmasq<\/code>&nbsp;is working OK.<\/p>\n\n\n\n<p>You can also check that DNS caching is working. Perform a resolution against another domain by running the following command twice:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>time getent hosts foo.example.com<\/code><\/pre>\n\n\n\n<p>Observe&nbsp;<code>real<\/code>&nbsp;timing in the output reduced for the subsequent queries. E.g. first request yields:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>real   0m0.048s\nuser   0m0.006s\nsys    0m0.006s<\/code><\/pre>\n\n\n\n<p>Subsequent requests yield:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>real   0m0.009s\nuser   0m0.006s\nsys    0m0.002s<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">See what kind of DNS requests your system makes<\/h2>\n\n\n\n<p>To see what DNS request your system makes, you can temporarily enable logging of queries. Note that this will clear DNS cache because&nbsp;<code>dnsmasq<\/code>&nbsp;will be restarted:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo log-queries | sudo tee -a \/etc\/NetworkManager\/dnsmasq.d\/log.conf\nsudo systemctl reload NetworkManager<\/code><\/pre>\n\n\n\n<p>You can then&nbsp;<code>tail<\/code>&nbsp;or&nbsp;<code>less<\/code>&nbsp;the&nbsp;<code>\/var\/log\/messages<\/code>&nbsp;file which will have information of requests being made. Example, on the web server that is using PaperTrail\u2019s&nbsp;<code>remote_syslog<\/code>:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">dnsmasq[20802]: forwarded logs6.papertrailapp.com to 2606:4700:4700::1001\ndnsmasq[20802]: reply logs6.papertrailapp.com is 169.46.82.182\ndnsmasq[20802]: reply logs6.papertrailapp.com is 169.46.82.183\ndnsmasq[20802]: reply logs6.papertrailapp.com is 169.46.82.184\ndnsmasq[20802]: reply logs6.papertrailapp.com is 169.46.82.185<\/pre>\n\n\n\n<p>This approach may be used for finding what external sites your server communicates with.<\/p>\n\n\n\n<p>Once you\u2019re done, don\u2019t forget to turn off the logging:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo rm \/etc\/NetworkManager\/dnsmasq.d\/log.conf\nsudo systemctl reload NetworkManager<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">How well is dnsmasq doing on your system<\/h2>\n\n\n\n<p>The&nbsp;<code>dnsmasq<\/code>&nbsp;manpage has this to say:<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>When it receives a SIGUSR1, dnsmasq writes statistics to the system log. It writes the cache size, the number of names which have had to removed from the cache before they expired in order to<br>make room for new names and the total number of names that have been inserted into the cache. The number of cache hits and misses and the number of authoritative queries answered are also given.<\/p><\/blockquote>\n\n\n\n<p>So we can collect DNS query stats easily:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo pkill --signal USR1 dnsmasq &amp;&amp; sudo tail \/var\/log\/messages | grep dnsmasq<\/code><\/pre>\n\n\n\n<p>The output may include, for example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dnsmasq[31949]: cache size 400, 0\/60 cache insertions re-used unexpired cache entries.\nqueries forwarded 30, queries answered locally 60<\/code><\/pre>\n\n\n\n<p>The 0 in&nbsp;<code>0\/60<\/code>&nbsp;stands for \u201czero cache evictions\u201d. So this number indicates that cache size is adequate. It should be as low as possible.<br>If that number is high, it means that cache size maybe not large enough.<\/p>\n\n\n\n<p>We also see that 30 DNS lookups were forwarded over to upstream nameservers (misses), while 60 were satisfied directly by cache (hits).<\/p>\n\n\n\n<p>Gathering stats like this will work well in case you only have one instance of dnsmasq. Sometimes you have more than one (e.g. libvirt may run one of its own).<\/p>\n\n\n\n<p>It is more reliable to use the statistical information of dnsmasq that is exposed, not surprisingly, via DNS ???? The commands:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dig +short chaos txt hits.bind\ndig +short chaos txt misses.bind<\/code><\/pre>\n\n\n\n<p>\u2026 give you hits and misses, respectively.<\/p>\n\n\n\n<p>With some command line magic, you can easily calculate your DNS cache hit-ratio:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># yum -y install bc\necho \"scale=2; $(dig +short chaos txt hits.bind)*100\/($(dig +short chaos txt hits.bind)+$(dig +short chaos txt misses.bind))\" | \\\n  sed 's@\"@@g' | bc<\/code><\/pre>\n\n\n\n<p>The output is a percentage of DNS requests that were satisfied by DNS cache, e.g.: 80.95%.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Tuning the cache size<\/h2>\n\n\n\n<p>The default cache size of&nbsp;<code>dnsmasq<\/code>&nbsp;instance that is run by Networkmanager is 400.<br>This is a&nbsp;<em>decent default<\/em>&nbsp;for web servers.<\/p>\n\n\n\n<p>For a desktop machine, you may want to increase it by large. This will assist with much less home router strain and faster network experience, especially if you\u2019re a Chrome user. This browser does DNS caching of its own, but only&nbsp;<a href=\"https:\/\/bugs.chromium.org\/p\/chromium\/issues\/detail?id=164026\">as long as 1 minute<\/a>&nbsp;\u2013 the issue that is discarded as a \u201cfeature\u201d.<\/p>\n\n\n\n<p>So to set DNS cache size to 20k, run:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo cache-size=20000 | sudo tee -a \/etc\/NetworkManager\/dnsmasq.d\/cache.conf\nsudo systemctl reload NetworkManager<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">dnsmasq and your desktop<\/h2>\n\n\n\n<p>To expand the topic of the desktop use of dnsmasq, you can also leverage it to block tracking scripts and for speeding up your browsing experience:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo curl https:\/\/raw.githubusercontent.com\/aghorler\/lightweight-dnsmasq-blocklist\/master\/list.txt \\\n  --output \/etc\/NetworkManager\/dnsmasq.d\/blocklist.conf\nsudo systemctl reload NetworkManager<\/code><\/pre>\n\n\n\n<p>Finally, you may also want to improve the DNS speed by ensuring minimum&nbsp;<abbr title=\"\">TTL<\/abbr>&nbsp;for DNS records that have it set too low.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo min-cache-ttl=1800 | sudo tee -a \/etc\/NetworkManager\/dnsmasq.d\/cache.conf\nsudo systemctl reload NetworkManager<\/code><\/pre>\n\n\n\n<p>This will ensure that even if a DNS record is configured with, e.g. 2 minutes TTL on remote nameserver,&nbsp;<code>dnsmasq<\/code>&nbsp;will still cache it for 30 minutes.<\/p>\n\n\n\n<p>Note that this is acceptable for desktop machines, but not for web servers:<\/p>\n\n\n\n<p>Phew, now I think that\u2019s about it for&nbsp;<code>dnsmasq<\/code>&nbsp;today. Enjoy your faster DNS and be sure to subscribe for our&nbsp;<a href=\"https:\/\/twitter.com\/getpagespeed\">Twitter<\/a>&nbsp;for more fine articles ????<\/p>\n","protected":false},"excerpt":{"rendered":"\n<p>One could only guess that the rationale for lack of DNS caching in RHEL is the arguable efficiency for those systems which aren\u2019t network connected or simply don\u2019t need to make any DNS lookups.<\/p>\n<p>There are of course such cases where you don\u2019t need (many) DNS resolutions. I can think of:<\/p>\n<p> a dedicated DB [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[73],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7934"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7934"}],"version-history":[{"count":2,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7934\/revisions"}],"predecessor-version":[{"id":7936,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7934\/revisions\/7936"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7934"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7934"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7934"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}