Difference between revisions of "Unbound"

From ArchWiki
Jump to: navigation, search
m (style, wording etc.)
m (added link)
Line 1: Line 1:
[[Category:Domain Name System]]
[[Category:Domain Name System]]
Unbound is a validating, recursive, and caching DNS resolver.
[https://unbound.net/ Unbound] is a validating, recursive, and caching DNS resolver.
== Installation ==
== Installation ==

Revision as of 18:36, 31 August 2013

Unbound is a validating, recursive, and caching DNS resolver.


Install unbound, and expat which is dependency for DNSSEC:


For querying a host that is not cached as an address the resolver needs to start at the top of the server tree and query the root servers to know where to go for the top level domain for the address being queried. Therefore it is necessary to put a root hints file into the unbound config directory. The simplest way to do this is to run the command:

# wget ftp://FTP.INTERNIC.NET/domain/named.cache -O /etc/unbound/root.hints

It is a good idea to run this every six months or so in order to make sure the list of root servers is up to date. This can be done manually or by setting up a cron job for the task.

If you will want logging for unbound, then create a log file which can also be in the same directory, but you can choose any location. One way is then to do as root:

# touch /etc/unbound/unbound.log
# chown unbound:unbound /etc/unbound/unbound.log

Then you can include the logging parameter when you set up the main unbound.conf file as below.

Basic configuration

Unbound configuration

unbound is easy to configure. There is a sample config file /etc/unbound/unbound.conf.example, which can be copied to /etc/unbound/unbound.conf and with the adjustments to the file for your own needs it is enough to run on both IPv4 and IPv6 without access restrictions.

For the case where you want the unbound server to listen to requests from the machine that it is running on then make sure that the line defining which address it is going to listen on is included:


It is also important to include a line to point unbound to the root.hints file that was prepared before editing the unbound.conf file:

root-hints: "/etc/unbound/root.hints"

If you have a local network which you wish to have DNS queries for and there is a local DNS server that you would like to forward queries to then you should include this line:

private-address: local_subnet/subnet_mask

for example:


To include a local DNS server for both forward and reverse local addresses a set of lines similar to these below is necessary with a forward and reverse lookup (choose the IP address of the server providing DNS for the local network accordingly by changing in the lines below):

local-zone: "10.in-addr.arpa." transparent

This line above is important to get the reverse lookup to work correctly.

name: "mynetwork.com."
forward-addr:        # Home DNS
name: "10.in-addr.arpa."

Note that there is a difference between forward zones and stub zones - stub zones will only work when connected to an authoritative DNS server directly. This would work for lookups from a bind DNS server if it is providing authoritative DNS - but if you are referring queries to an unbound server in which internal lookups are forwarded on to another DNS server, then defining the referral as a stub zone in the machine here will not work. In that case it is necessary to define a forward zone as above, since forward zones can have daisy chain lookups onward to other DNS servers. i.e. forward zones can refer queries to recursive DNS servers. This distinction is important as you do not get any error messages indicating what the problem is if you use a stub zone inappropriately.

You can set up the localhost forward and reverse lookups with the following lines:

local-zone: "localhost." static
local-data: "localhost. 10800 IN NS localhost."
local-data: "localhost. 10800 IN SOA localhost. nobody.invalid. 1 3600 1200 604800 10800"
local-data: "localhost. 10800 IN A"
local-zone: "127.in-addr.arpa." static
local-data: "127.in-addr.arpa. 10800 IN NS localhost."
local-data: "127.in-addr.arpa. 10800 IN SOA localhost. nobody.invalid. 2 3600 1200 604800 10800"
local-data: " 10800 IN PTR localhost."

Then to use specific servers for default forward zones that are outside of the local machine and outside of the local network (i.e. all other queries will be forwarded to them, and then cached) add this to the configuration file (and in this example the first two addresses are the fast google DNS servers):

  name: "."

This will make unbound use Google and OpenDNS servers as the forward zone for external lookups.

Note: OpenDNS strips DNSSEC records from responses. Do not use the above forward zone if you want to enable DNSSEC validation (below).

Adding unbound to boot process

Start the service:

systemctl start unbound.service

Then enable it so that it starts at boot once you have tested that it works.

systemctl enable unbound.service

Set /etc/resolv.conf to use the local DNS server

Edit /etc/resolv.conf (See also resolv.conf):


Also if you want to be able to use the hostname of local machine names without the fully qualified domain names, then add a line with the local domain such as:

domain localdomain.com

That way you can refer to local hosts such as mainmachine1.localdomain.com as simply mainmachine1 when using the ssh command, but the drill command below still requires the fully qualified domain names in order to perform lookups.

Testing the server before making it default can be done using the drill command from the ldns package with examples from internal and external forward and reverse addresses:

drill @ www.cnn.com
drill @ localmachine.localdomain.com
drill @ -x w.x.y.z 

where w.x.y.z can be a local or external IP address and the -x option requests a reverse lookup. Once all is working, and you have /etc/resolv.conf set to use as the nameserver then you no longer need the @ in the drill command, and you can test again that it uses the default DNS server - check that the server used as listed at the bottom of the output from each of these commands shows it is being queried.

Configuring unbound to validate DNSSEC

You will need the root server trust key anchor file. It is provided by the dnssec-anchors package (already installed as a dependency), however, unbound needs read and write access to the file. You can follow an example configuration below:

# mkdir /etc/unbound/keys
# cp /etc/trusted-key.key /etc/unbound/keys/dnssec-root-anchor.key
# chown -R unbound:unbound /etc/unbound/keys

Then point unbound to the file:

  auto-trust-anchor-file: "/etc/unbound/keys/dnssec-root-anchor.key"

Also make sure that if a general forward to the google servers had been in place, then comment them out otherwise DNS queries will fail.

Restart unbound:

# systemctl restart unbound

Now DNSSEC validation will be done if the DNS server being queried supports it.

Note: Including DNSSEC checking significantly increases DNS lookup times for initial lookups. Once an address is cached locally, then the lookup is virtually instantaneous.

Adding an authoritative DNS server

For users who wish to run both a validating, recursive, caching DNS server as well as an authoritative DNS server on a single machine then it may be useful to refer to the wiki page nsd which gives an example of a configuration for such a system. Having one server for authoritative DNS queries and a separate DNS server for the validating, recursive, caching DNS functions gives increased security over a single DNS server providing all of these functions. Many users have used bind as a single DNS server, and some help on migration from bind to the combination of running nsd and bind is provided in the nsd wiki page.

WAN facing DNS

It is also possible to change the configuration files and interfaces on which the server is listening so that DNS queries from machines outside of the local network can access specific machines within the LAN. This is useful for web and mail servers which are accessible from anywhere, and the same techniques can be employed as has been achieved using bind for many years, in combination with suitable port forwarding on firewall machines to forward incoming requests to the right machine.

Issues concerning num-threads

The man page for unbound.conf mentions:

     outgoing-range: <number>
             Number of ports to open. This number of file  descriptors  can  be  opened  per thread.

and some sources suggest that the num-threads parameter should be set to the number of cpu cores. The sample unbound.conf.example file merely has:

       # number of threads to create. 1 disables threading.
       # num-threads: 1

However it is not possible to arbitrarily increase num-threads above 1 without causing unbound to start with warnings in the logs about exceeding the number of file descriptors. In reality for most users running on small networks or on a single machine it should be unnecessary to seek performance enhancement by increasing num-threads above 1. If you do wish to do so then refer to official documentation and the following rule of thumb should work:

Set num-threads equal to the number of CPU cores on the system. E.g. for 4 CPUs with 2 cores each, use 8.

Set the outgoing-range to as large a value as possible, see the sections in the referred web page above on how to overcome the limit of 1024 in total. This services more clients at a time. With 1 core, try 950. With 2 cores, try 450. With 4 cores try 200. The num-queries-per-thread is best set at half the number of the outgoing-range.

Because of the limit on outgoing-range thus also limits num-queries-per-thread, it is better to compile with libevent, so that there is no 1024 limit on outgoing-range. If you need to compile this way for a heavy duty DNS server then you will need to compile the programme from source instead of using the unbound package.