{"id":7063,"date":"2020-10-06T23:55:17","date_gmt":"2020-10-06T20:55:17","guid":{"rendered":"https:\/\/kifarunix.com\/?p=7063"},"modified":"2024-03-14T23:31:51","modified_gmt":"2024-03-14T20:31:51","slug":"configure-local-dns-server-using-dnsmasq-on-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/kifarunix.com\/configure-local-dns-server-using-dnsmasq-on-ubuntu-20-04\/","title":{"rendered":"Configure Local DNS Server using Dnsmasq on Ubuntu 20.04"},"content":{"rendered":"\n<p>Welcome to our tutorial on how to install and configure local DNS Server using Dnsmasq on Ubuntu 20.04. &#8220;<em>Dnsmasq is a lightweight, easy to configure, DNS forwarder and DHCP server. It is designed to provide DNS and optionally, DHCP and&nbsp;TFTP, to a small network. It can serve the names of local machines which are not in the global DNS<\/em>&#8220;.<\/p>\n\n\n\n<p>If you want to save yourself the hustle of having to configure the DNS server the BIND9 way, using Dnsmasq is the simplest and quickest way to get your local DNS server up and running.<\/p>\n\n\n\n<p>In this setup, therefore, we will be configuring Dnsmasq as our local caching DNS server to speed to the local DNS resolution.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Configuring Local DNS Server using Dnsmasq on Ubuntu<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Run System Update<\/h3>\n\n\n\n<p>Update your system package cache;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt update<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"install-dnsmasq-ubuntu-20.04\"><a href=\"#install-dnsmasq-ubuntu-20.04\">Install Dnsmasq on Ubuntu 20.04<\/a><\/h3>\n\n\n\n<p>Dnsmasq is available on the Ubuntu 20.04 Universe repos. However, before you can install <code><strong>dnsmasq<\/strong><\/code> on Ubuntu 20.04, disable Systemd-resolved service&nbsp;(<em>system service that provides network name resolution to local applications<\/em>).<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl disable --now systemd-resolved<\/code><\/pre>\n\n\n\n<p>Remove the default resolv.conf file and create a new one with your custom DNS server details to enable you do the installation.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>rm -rf \/etc\/resolv.conf<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo \"nameserver 8.8.8.8\" &gt; \/etc\/resolv.conf<\/code><\/pre>\n\n\n\n<p>Once that is done, Dnsmasq can be installed by running the command below;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>apt install dnsmasq<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>Reading package lists... Done\nBuilding dependency tree       \nReading state information... Done\nThe following additional packages will be installed:\n  dns-root-data dnsmasq-base libidn11\nSuggested packages:\n  resolvconf\nThe following NEW packages will be installed:\n  dns-root-data dnsmasq dnsmasq-base libidn11\n0 upgraded, 4 newly installed, 0 to remove and 73 not upgraded.\nNeed to get 382 kB of archives.\nAfter this operation, 1,155 kB of additional disk space will be used.\nDo you want to continue? &#91;Y\/n] y<\/code><\/pre>\n\n\n\n<p>This installs and starts and enables Dnsmasq service to run on system boot.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl status dnsmasq<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>\u25cf dnsmasq.service - dnsmasq - A lightweight DHCP and caching DNS server\n     Loaded: loaded (\/lib\/systemd\/system\/dnsmasq.service; enabled; vendor preset: enabled)\n     Active: active (running) since Tue 2020-10-06 19:12:31 UTC; 15s ago\n   Main PID: 17726 (dnsmasq)\n      Tasks: 1 (limit: 2282)\n     Memory: 868.0K\n     CGroup: \/system.slice\/dnsmasq.service\n             \u2514\u250017726 \/usr\/sbin\/dnsmasq -x \/run\/dnsmasq\/dnsmasq.pid -u dnsmasq -7 \/etc\/dnsmasq.d,.dpkg-dist,.dpkg-old,.dpkg-new --local-service --trust-anchor=.,20326,8,2,e&gt;\n\nOct 06 19:12:31 ubuntu20 systemd&#91;1]: Starting dnsmasq - A lightweight DHCP and caching DNS server...\nOct 06 19:12:31 ubuntu20 dnsmasq&#91;17705]: dnsmasq: syntax check OK.\nOct 06 19:12:31 ubuntu20 dnsmasq&#91;17726]: started, version 2.80 cachesize 150\nOct 06 19:12:31 ubuntu20 dnsmasq&#91;17726]: DNS service limited to local subnets\nOct 06 19:12:31 ubuntu20 dnsmasq&#91;17726]: compile time options: IPv6 GNU-getopt DBus i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth DNSSEC loop-detect inotify dumpfi&gt;\nOct 06 19:12:31 ubuntu20 dnsmasq&#91;17726]: reading \/etc\/resolv.conf\nOct 06 19:12:31 ubuntu20 dnsmasq&#91;17726]: using nameserver 8.8.8.8#53\nOct 06 19:12:31 ubuntu20 dnsmasq&#91;17726]: read \/etc\/hosts - 7 addresses\nOct 06 19:12:31 ubuntu20 systemd&#91;1]: Started dnsmasq - A lightweight DHCP and caching DNS server.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"dnsmasq-local-dns-server\"><a href=\"#dnsmasq-local-dns-server\">Configuring Local DNS Server using Dnsmasq<\/a><\/h3>\n\n\n\n<p>Once Dnsmasq is installed, you can now proceed to configure it as the local caching DNS server on Ubuntu 20.04. <code><strong>\/etc\/dnsmasq.conf<\/strong><\/code> is the default Dnsmasq configuration file. To configure dnsmasq therefore, you need to edit&nbsp;the <code><strong>\/etc\/dnsmasq.conf<\/strong><\/code> file.<\/p>\n\n\n\n<p>First off, create a copy of the configuration file;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cp \/etc\/dnsmasq.conf{,.bak}<\/code><\/pre>\n\n\n\n<p>Next, open the configuration file for editing;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>vim \/etc\/dnsmasq.conf<\/code><\/pre>\n\n\n\n<p>The file is well commented and all configuration options are self explanatory.<\/p>\n\n\n\n<p>To begin with, set the port on which Dnsmasq will listen for DNS requests. This defaults to port 53 UDP by default. You can as well explicitly set the port using the <strong><code>port<\/code><\/strong> option.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>port=53<\/code><\/pre>\n\n\n\n<p>Disable forwarding of names without a dot or domain part;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>domain-needed<\/code><\/pre>\n\n\n\n<p>Disable forwarding of addresses in the non-routed address spaces;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>bogus-priv<\/code><\/pre>\n\n\n\n<p>Define an interface (eg, <code><strong>interface=enp0s8<\/strong><\/code>) or the IP address (e.g, <code><strong>listen-address=192.168.x.x<\/strong><\/code>) on which the Dnsmasq can listen for the DNS requests. This usually defaults to the loopback address. In this setup, we set the Dnsmasq to respond to both internal and external DNS requests via a loopback and non-loopback interface IP.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>listen-address=127.0.0.1,192.168.57.3 <\/code><\/pre>\n\n\n\n<p>Replace you interface IP address accordingly.<\/p>\n\n\n\n<p>Enable Dnsmasq to automatically append the domain part to the simple names;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>expand-hosts<\/code><\/pre>\n\n\n\n<p>Set the domain for dnsmasq to append to simple names;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>domain=kifarunix-demo.com<\/code><\/pre>\n\n\n\n<p>Adjust the size of the cached domain names. The default is&nbsp;<code>150<\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cache-size=1000<\/code><\/pre>\n\n\n\n<p>The above configuration options are enough for the basic local caching DNS server using Dnsmasq.<\/p>\n\n\n\n<p>Without comment lines, this is how our Dnsmasq configuration file looks like;<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>port=53\ndomain-needed\nbogus-priv\nlisten-address=127.0.0.1,192.168.57.3\nexpand-hosts\ndomain=kifarunix-demo.com\ncache-size=1000<\/code><\/pre>\n\n\n\n<p>Save and exit the configuration file once done making changes.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Add the Dnsmasq DNS server IP on \/etc\/hosts file<\/h4>\n\n\n\n<p>Next, add the Dnsmasq IP address as the primary DNS server on the <code><strong>\/etc\/resolv.conf<\/strong><\/code>.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>sed -i '1i nameserver 192.168.57.3' \/etc\/resolv.conf<\/code><\/pre>\n\n\n\n<p>The <code><strong>\/etc\/resolv.conf<\/strong><\/code> now looks like;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>cat \/etc\/resolv.conf<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>nameserver 192.168.57.3\nnameserver 8.8.8.8<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Add local DNS Records to Dnsmasq Server<\/h4>\n\n\n\n<p>Add local DNS entries on the Dnsmasq server <code><strong>\/etc\/hosts<\/strong><\/code> file.<\/p>\n\n\n\n<p>echo -e &#8220;192.168.57.19 centos8.kifarunix-demo.com\\n192.168.57.6 ubuntu18.kifarunix-demo.com&#8221; &gt;&gt; \/etc\/hosts<\/p>\n\n\n\n<p>Do the same for your other local domain names.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Restart Dnsmasq<\/h4>\n\n\n\n<p>Run Dnsmasq configuration check;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dnsmasq --test<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code><strong>dnsmasq: syntax check OK.<\/strong><\/code><\/pre>\n\n\n\n<p>Restart Dnsmasq;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>systemctl restart dnsmasq<\/code><\/pre>\n\n\n\n<p>Confirming Port 53;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>netstat -alnp | grep -i :53<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>tcp        0      0 0.0.0.0:53              0.0.0.0:*               LISTEN      18313\/dnsmasq       \ntcp6       0      0 :::53                   :::*                    LISTEN      18313\/dnsmasq       \nudp        0      0 0.0.0.0:53              0.0.0.0:*                           18313\/dnsmasq       \nudp6       0      0 :::53                   :::*                                18313\/dnsmasq<\/code><\/pre>\n\n\n\n<p>Note that dnsmasq binds to the wildcard address, even when it is listening on only some interfaces. This has the advantage of working even when interfaces come and go and change address.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Open DNS Port on UFW<\/h4>\n\n\n\n<p>If UFW is enabled, open the DNS port 53, UDP.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>ufw allow from 192.168.0.0\/16 to any port 53 proto udp<\/code><\/pre>\n\n\n\n<p>Update your source network accordingly.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Verify DNS resolution<\/h4>\n\n\n\n<p>Local domain resolution;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dig ubuntu18.kifarunix-demo.com +short<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>192.168.57.3<\/code><\/pre>\n\n\n\n<p>External DNS resolution;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dig google.com +short<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>216.58.223.110<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Configure DNS Server on Remote Clients<\/h4>\n\n\n\n<p>Now that the Dnsmasq is ready to server out both DNS local and external queries via the local DNS server, update the DNS entry for the clients on the \/etc\/resolv.conf.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>echo \"nameserver 192.168.57.3\" &gt; \/etc\/resolv.conf<\/code><\/pre>\n\n\n\n<p>Replace the Dnsmasq IP accordingly.<\/p>\n\n\n\n<p>Perform local dns queries;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>dig ubuntu18.kifarunix-demo.com<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>; &lt;&lt;&gt;&gt; DiG 9.11.13-RedHat-9.11.13-6.el8_2.1 &lt;&lt;&gt;&gt; ubuntu18.kifarunix-demo.com\n;; global options: +cmd\n;; Got answer:\n;; -&gt;&gt;HEADER&lt;&lt;- opcode: QUERY, status: NOERROR, id: 57550\n;; flags: qr aa rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1\n\n;; OPT PSEUDOSECTION:\n; EDNS: version: 0, flags:; udp: 4096\n;; QUESTION SECTION:\n;ubuntu18.kifarunix-demo.com.\tIN\tA\n\n;; ANSWER SECTION:\nubuntu18.kifarunix-demo.com. 0\tIN\tA\t192.168.57.3\n\n;; Query time: 1 msec\n;; SERVER: 192.168.57.3#53(192.168.57.3)\n;; WHEN: Tue Oct 06 00:20:02 EAT 2020\n;; MSG SIZE  rcvd: 72<\/code><\/pre>\n\n\n\n<p>Now, let us time the DNS queries using the <strong>drill<\/strong> utility. To use this tool, you need to install <code><strong>ldns-utils<\/strong><\/code> package on CentOS or <code><strong>ldnsutils<\/strong><\/code> package on Ubuntu. Assuming the packages are installed, use <code><strong>drill<\/strong><\/code> utility to verify DNS caching;<\/p>\n\n\n\n<p>First time query run;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>drill google.com | grep \"Query time\"<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>;; Query time: 25 msec<\/code><\/pre>\n\n\n\n<p>Second time query;<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>drill google.com | grep \"Query time\"<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>;; Query time: 1 msec<\/code><\/pre>\n\n\n\n<p>Hurraaay!! the Dnsmasq is now configured and running a local caching DNS server.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Related Tutorials<\/h3>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/setup-caching-only-dns-server-using-bind9-on-ubuntu-20-04\/\" target=\"_blank\" rel=\"noreferrer noopener\">Setup Caching-Only DNS Server using BIND9 on Ubuntu 20.04<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/configure-bind-dns-server-using-webmin-on-centos-8\/\" target=\"_blank\" rel=\"noreferrer noopener\">Configure BIND DNS Server using Webmin on CentOS 8<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/setup-bind-dns-using-webmin-on-debian-10\/\" target=\"_blank\" rel=\"noreferrer noopener\">Setup Bind DNS Using Webmin on Debian 10<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/configure-bind-as-slave-dns-server-on-ubuntu-18-04\/\" target=\"_blank\" rel=\"noreferrer noopener\">Configure BIND as Slave DNS Server on Ubuntu 18.04<\/a><\/p>\n\n\n\n<p><a href=\"https:\/\/kifarunix.com\/how-to-setup-master-slave-dns-server-using-bind-on-centos-7\/\" target=\"_blank\" rel=\"noreferrer noopener\">How to Setup Master-Slave DNS Server using BIND on CentOS 7<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to our tutorial on how to install and configure local DNS Server using Dnsmasq on Ubuntu 20.04. &#8220;Dnsmasq is a lightweight, easy to configure,<\/p>\n","protected":false},"author":3,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"rank_math_lock_modified_date":false,"footnotes":""},"categories":[121,971,128,173],"tags":[2623,2622,2621,2619,973,2620,2617,2618,2616,1200],"class_list":["post-7063","post","type-post","status-publish","format-standard","hentry","category-howtos","category-dns","category-lpic-2","category-lpic-2-exam-202-405-topics-and-objectives","tag-etc-hosts","tag-etc-resolv-conf","tag-caching-only-dns","tag-configure-dnsmasq-as-local-caching-dns-server","tag-dns","tag-dnsmasq","tag-dnsmasq-local-caching-dns-server","tag-dnsmasq-ubuntu-20-04","tag-install-dnsmasq-ubuntu-20-04","tag-ubuntu-20-04","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-50"],"_links":{"self":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/7063"}],"collection":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/comments?post=7063"}],"version-history":[{"count":6,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/7063\/revisions"}],"predecessor-version":[{"id":21540,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/posts\/7063\/revisions\/21540"}],"wp:attachment":[{"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/media?parent=7063"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/categories?post=7063"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/kifarunix.com\/wp-json\/wp\/v2\/tags?post=7063"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}