1 Configuring upstream DNS forwarders from resolv.conf
Vladimír Čunát edited this page 2025-01-11 09:44:21 +01:00

dnsmasq-like configuration of forwarders for Knot Resolver

In constrast to Dnsmasq, the Knot Resolver does not read resolv.conf file on its own. Knot Resolver's configuration file is proper Lua script which allows you to tweak its behavior as needed.

Following config snippet was contributed by user who wanted to read resolv.conf file in similar way to dnsmasq.

Config file snippet

policy.forwarders = {}

function policy:reload_resolv_file()
  for i = 1, #policy.forwarders do
    print('removing forwarder rule:', policy.forwarders[i].id)
    policy.del(policy.forwarders[i].id)
  end

  policy.forwarders = {}

  for line in io.lines(policy.resolv_file) do
    if not line:match("^%s+#") then
      local split = string.gmatch(line, "[^%s]+")
      local name = split()
      local value = split()

      if name == 'nameserver' then
        print('adding new forwarder rule for:', value)
        table.insert(policy.forwarders, policy.add(policy.all(policy.FORWARD(value))))
      end
    end
  end
end

policy.resolv_file = '/var/run/NetworkManager/resolv.conf'

policy:reload_resolv_file()

This snippet will load forwarders form file /var/run/NetworkManager/resolv.conf into Knot Resolver. The list can be re-read at run-time by calling Lua function policy:reload_resolv_file().

Depending on your configuration, this can be done using following command

echo "policy:reload_resolv_file()" | sudo nc -U /run/knot-resolver/control

Do not forget to amend the socket path as necessary for your system.

(Based on https://github.com/CZ-NIC/knot-resolver/issues/40)