{"id":6474,"date":"2021-05-25T18:05:33","date_gmt":"2021-05-25T18:05:33","guid":{"rendered":"https:\/\/linuxways.net\/?p=6474"},"modified":"2021-05-25T18:05:33","modified_gmt":"2021-05-25T18:05:33","slug":"how-to-install-and-use-unbound-in-ubuntu-20-04","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/ubuntu\/how-to-install-and-use-unbound-in-ubuntu-20-04\/","title":{"rendered":"How to Install and Use Unbound in Ubuntu 20.04"},"content":{"rendered":"<p>Unbound is a recursive, validating, very secure, and DNS caching server which is distributed free of charge under the BSD license. Unbound supports DNS-over-TLS and DNS-over-HTTPS to increase online privacy by allowing clients to encrypt their connection. Depending on your network configuration, Unbound can support both IPV4 and IPV6. The installation and configuration of Unbound in Linux distributions is quite simple and straightforward. The Unbound package is available in most modern OS\u2019s including CentOS, Ubuntu, Fedora. Companies that use their own domain to serve applications or websites internally can utilize unbound as a DNS server. In this article, we will learn how to install and configure a <strong>Unbound <\/strong>server in Ubuntu 20.04.<\/p>\n<h2>Installation<\/h2>\n<p>Installation of Unbound name resolution server in Ubuntu 20.04 is very simple and easy. Run the following command to install the package<\/p>\n<pre>$ sudo apt install unbound -y<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1068\" height=\"362\" class=\"wp-image-6475\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-137.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-137.png 1068w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-137-300x102.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-137-1024x347.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-137-768x260.png 768w\" sizes=\"auto, (max-width: 1068px) 100vw, 1068px\" \/><\/p>\n<p>Also, run the following command to install additional packages which we will use to check the DNS server configurations<\/p>\n<pre>$ sudo apt install bind-utils net-tools -y<\/pre>\n<p>After the installation is completed, the contents of the configuration file can be found by using the command:<\/p>\n<pre>$ cat \/etc\/unbound\/unbound.conf<\/pre>\n<p>Output :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"870\" height=\"267\" class=\"wp-image-6476\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-138.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-138.png 870w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-138-300x92.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-138-768x236.png 768w\" sizes=\"auto, (max-width: 870px) 100vw, 870px\" \/><\/p>\n<p>The output shows that from the unbound.conf.d directory all the .conf files will be loaded.<\/p>\n<p>Lets create a new configuration file under the directory <strong>\/etc\/unbound\/unbound.conf.d<\/strong> directory. In this example I have created an <strong>unbound_test.conf<\/strong> file. You can have your own assumption.<\/p>\n<p>Open the file using the text editor and add the following sample configuration. You can modify the parameters accordingly.<\/p>\n<pre>$ sudo nano \/etc\/unbound\/unbound.conf.d\/unbound_test.conf<\/pre>\n<pre>server:\r\n\r\nport: 53\r\n\r\nverbosity: 0\r\n\r\nnum-threads: 2\r\n\r\noutgoing-range: 512\r\n\r\nnum-queries-per-thread: 1024\r\n\r\nmsg-cache-size: 32m\r\n\r\ninterface: 127.0.0.1\r\n\r\ninterface: 192.168.5.5\r\n\r\nrrset-cache-size: 64m\r\n\r\ncache-max-ttl: 86400\r\n\r\ninfra-host-ttl: 60\r\n\r\ninfra-lame-ttl: 120\r\n\r\noutgoing-interface: 192.168.0.2\r\n\r\naccess-control: 127.0.0.0\/8 allow\r\n\r\naccess-control: 192.168.5.0\/24 allow\r\n\r\nusername: unbound\r\n\r\ndirectory: \"\/etc\/unbound\"\r\n\r\nlogfile: \"\/var\/log\/unbound.log\"\r\n\r\nuse-syslog: no\r\n\r\nhide-version: yes\r\n\r\nso-rcvbuf: 4m\r\n\r\nso-sndbuf: 4m\r\n\r\ndo-ip4: yes\r\n\r\ndo-ip6: no\r\n\r\ndo-udp: yes\r\n\r\ndo-tcp: yes<\/pre>\n<pre>Now create a log file and assign permission to write logs\r\n\r\n$ sudo touch \/var\/log\/unbound.log<\/pre>\n<pre>$ sudo chown unbound:unbound \/var\/log\/unbound.log<\/pre>\n<p>Restart the Unbound service the load the configuration<\/p>\n<pre>$ sudo service unbound restart<\/pre>\n<p>Use the following command to enable to service<\/p>\n<pre>$ sudo service unbound enable<\/pre>\n<p>Check if the unbound service is running or not using the command:<\/p>\n<pre>$ sudo service unbound status<\/pre>\n<p>output :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1086\" height=\"347\" class=\"wp-image-6477\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-139.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-139.png 1086w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-139-300x96.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-139-1024x327.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-139-768x245.png 768w\" sizes=\"auto, (max-width: 1086px) 100vw, 1086px\" \/><\/p>\n<p>Run the following command to check in which port unbound is listening to<\/p>\n<pre>$ sudo netstat -anlpt | grep LIST<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"966\" height=\"215\" class=\"wp-image-6478\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-140.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-140.png 966w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-140-300x67.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-140-768x171.png 768w\" sizes=\"auto, (max-width: 966px) 100vw, 966px\" \/><\/p>\n<p>The output shows that the Unbound service is listening on port 53 to accept the requests.<\/p>\n<p><strong>Configuration file explained:<\/strong><\/p>\n<p><strong>port<\/strong> : Listening port for Unbound<\/p>\n<p><strong>hide-version<\/strong> : Do not display the version of unbound<\/p>\n<p><strong>use-syslog<\/strong> : specify if you want to write logs in syslog<\/p>\n<p><strong>username<\/strong> : User, under which unbound runs<\/p>\n<p><strong>verbosity<\/strong> : Log level which ranges from 0 to 4 and 4 is debug log level<\/p>\n<p><strong>interface<\/strong>: Interfaces in which unbound requests will be listened to<\/p>\n<p><strong>outgoing interface<\/strong> : Interface through which internet comes<\/p>\n<p><strong>num-threads<\/strong> : Number of threads , recommended to specify equal number of processor core<\/p>\n<h3>Open the DNS port in the firewall.<\/h3>\n<p>Once the configuration file is created, you need to open a DNS port to allow your local LAN clients to connect to your Unbound cache-only DNS server.<\/p>\n<pre>$ sudo ufw allow from any to any port 53 proto tcp<\/pre>\n<pre>$ sudo ufw allow from any to any port 53 proto udp<\/pre>\n<p>To check the firewall rule run the following command<\/p>\n<pre>$ sudo ufw status<\/pre>\n<p>Output :<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"830\" height=\"253\" class=\"wp-image-6479\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-141.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-141.png 830w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-141-300x91.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-141-768x234.png 768w\" sizes=\"auto, (max-width: 830px) 100vw, 830px\" \/><\/p>\n<p>Now we have come to the final point to test our new Unbound DNS server. For testing we can use the <strong>dig<\/strong> command which comes with the previously installed package <strong>bind-utils<\/strong>. Perform some DNS queries in the actual DNS server. For this example, I have queried kernel.org for testing. You can have your own assumption.<\/p>\n<pre>$ dig kernel.org @localhost<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"861\" height=\"323\" class=\"wp-image-6480\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-142.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-142.png 861w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-142-300x113.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-142-768x288.png 768w\" sizes=\"auto, (max-width: 861px) 100vw, 861px\" \/><\/p>\n<p>The response time is 4 msec in the first query. Since we have configured the Unbound DNS server, the query is now cached . To verify the dns cache, run the following query with the same domain name.<\/p>\n<pre>$ dig kernel.org @localhost<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"787\" height=\"323\" class=\"wp-image-6481\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-143.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-143.png 787w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-143-300x123.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-143-768x315.png 768w\" sizes=\"auto, (max-width: 787px) 100vw, 787px\" \/><\/p>\n<p>Now you can find that the query time is 0 msec.<\/p>\n<p>If you want to test the Unbound DNS server\u2019s configuration from LAN clients, query the DNS response pointing to the Unbound DNS server\u2019s IP. (In my case my Unbound DNS server IP is 192.168.178.130)<\/p>\n<pre>$ dig kernel.org @192.168.178.130<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"779\" height=\"297\" class=\"wp-image-6482\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-144.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-144.png 779w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-144-300x114.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/05\/word-image-144-768x293.png 768w\" sizes=\"auto, (max-width: 779px) 100vw, 779px\" \/><\/p>\n<h2>Optimization<\/h2>\n<p>The default configuration of Unbound works fine for limited users but in case there is a large number of users to be provided service then certain optimization needs to be made. Here are some optimization options which you can implement to get high performance.<\/p>\n<p><strong>num-threads: <\/strong>Put the equal number of CPU cores on the system, for example, put the value 4 for 2 CPUs with 2 cores each.<\/p>\n<p><strong>so-reuseport: yes <\/strong>In linux, this helps to improve UDP performance<\/p>\n<p><strong>outgoing-range<\/strong>: You can set the value large as possible depending on the number of cores.<\/p>\n<p><strong>so-sndbuf: <\/strong>This value can be set to a larger value 4m or 8m for a busy server.<\/p>\n<p>For more details you can take reference from<\/p>\n<p>https:\/\/nlnetlabs.nl\/documentation\/unbound\/howto-optimise\/<\/p>\n<h2>Conclusion<\/h2>\n<p>In the article, we have covered how to install and configure the Unbound name resolution server in Ubuntu with basic configuration. Certain tests have been performed using the dig command to check the configuration of the Unbound server. Also, we have learned about how to query DNS responses from the local LAN client using the Unbound server\u2019s IP. Any feedback and suggestions will be highly appreciated.<\/p>","protected":false},"excerpt":{"rendered":"<p>Unbound is a recursive, validating, very secure, and DNS caching server which is distributed free of charge under the BSD license. Unbound supports DNS-over-TLS and DNS-over-HTTPS to increase&hellip;<\/p>","protected":false},"author":1,"featured_media":6541,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[100,336],"class_list":["post-6474","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ubuntu","tag-ubuntu-20-04","tag-unbound"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/6474","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=6474"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/6474\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/6541"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=6474"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=6474"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=6474"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}