Secure and private DNS with Knot Resolver

Update 5 March 2018: this post was updated to work around a problem with the RPZ file from being ignored because it contains CRLF instead of LF where Knot Resolver does not expect them (bug 453) and to fix an error in the configuration of the predict module.

Knot Resolver is a modern, feature-rich recursive DNS server. It is used by Cloudflare for its public DNS service.

To install it on Debian, run:

# apt-get install knot-resolver knot-dnsutils lua-cqueues

The knot-dnsutils contains the kdig command which is useful for testing your DNS server. lua-cqueues is needed for automatic detection of changes in the RPZ file.

By default the kresd daemon will listen on localhost only (see /lib/systemd/system/kresd.socket). If you want it to be available on other addresses, you will need to override the kresd.socket file. Execute

# systemctl edit kresd.socket

This will create the file /etc/systemd/system/kresd.socket.d/override.conf. Add a ListenStream and ListenDatagram line for all addresses you want it to listen on. For example:


If you want to listen on all interfaces, it is enough to put this in the file:


You can do the same with kresd-tls.socket to define the addresses on which to listen over DNS-over-TLS requests (port 853).

Knot Resolver’s configuration file is /etc/knot-resolver/kresd.conf. I give an example configuration file with comments:

-- Default empty Knot DNS Resolver configuration in -*- lua -*-
-- Switch to unprivileged user --

-- Set the size of the cache to 1 GB
cache.size = 1*GB

-- Uncomment this only if you need to debug problems.
-- verbose(true)

-- Enable optional modules
modules = {
  'serve_stale < cache',
  'workarounds < iterate',

-- Accept all requests from these subnets
view:addr('', function (req, qry) return policy.PASS end)
view:addr('[::1]/128', function (req, qry) return policy.PASS end)
view:addr('', function (req, qry) return policy.PASS end)

-- Drop everything that hasn't matched
view:addr('', function (req, qry) return policy.DROP end)

-- Use the RPZ list
policy.add(policy.rpz(policy.DENY, '/etc/knot-resolver/',true))

-- Forward all requests for to and
policy.add(policy.suffix(policy.FORWARD({'', ''}), {todname('')}))
policy.add(policy.suffix(policy.FORWARD({'', ''}), {todname('')}))

-- Uncomment one of the following stanzas in case you want to forward all requests to or via DNS-over-TLS.

-- policy.add(policy.all(policy.TLS_FORWARD({
--          { '', hostname='', ca_file='/etc/ssl/certs/ca-certificates.crt' },
--          { '2606:4700:4700::1111', hostname='', ca_file='/etc/ssl/certs/ca-certificates.crt' },
-- })))

-- policy.add(policy.all(policy.TLS_FORWARD({
--           { '', hostname='', ca_file='/etc/ssl/certs/ca-certificates.crt' },
--           { '2620:fe::fe', hostname='', ca_file='/etc/ssl/certs/ca-certificates.crt' },
-- })))

-- Prefetch learning (20-minute blocks over 24 hours)
predict.config({ window = 20, period = 72 })

I use the RPZ file, which contains a blacklist of malicious domains. You will have to download it first:

# cd /etc/knot-resolver
# curl | sed -e 's/\r$//' -e '/'> /etc/knot-resolver/

I use sed to convert CRLF in LF (otherwise Knot Resolver fails to parse the file), and I filter out According to it hosts some malware, but there is too much useful stuff there too to block the domain completely.

In order to update it automatically, create /etc/systemd/system/update-urlhaus-abuse-ch.service:

Description=Update RPZ file from for Knot Resolver

ExecStart=/bin/sh -c 'curl | sed -e 's/\r$//' -e '/'> /etc/knot-resolver/'

and then create a timer which will run the service approximately every 10-15 minutes./etc/systemd/system/update-urlhaus-abuse-ch.timer:

Description=Update RPZ file from for Knot Resolver



Use the first two commands to enable and start the timer. You can check the status using the last command:

# systemctl enable update-urlhaus-abuse-ch.timer
# systemctl start update-urlhaus-abuse-ch.timer
# systemctl list-timers

Now you need to enable and start one or more instances of kresd. kresd is single-threaded, so if you want to make use of all of your CPU cores, you can start as many instances as the numbers of cores you have. For example in order to enable and start 4 instances run this command:

# systemctl enable --now kresd@{1..4}.service

More information

One Comment

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.