$ [email protected]


Configure a specific DNS server for a specific domain

Recently I’ve encountered a challenge where I needed to resolve a certain domain from a specific DNS server.

Let’s define the scenario like this:

  • I want to resolve the domain example.net from local DNS servers 172.20.11.11 and 172.20.11.12
  • I want all other domains to be resolved from 8.8.8.8 and 1.1.1.1

I tried different solutions such as configuring systemd-resolvd but nothing seems to work. Either I’m doing something wrong, or there is something wrong with my version of systemd-resolvd.

So, the next best solution is to set up a local DNS resolver such as BIND and then use DNS forwarder zones to forward requests for example.net to the desired DNS servers.

Follow these steps:

  1. Install BIND
sudo apt update
sudo apt install bind9
  1. Open /etc/bind/named.conf.options and configure it like this:
options {
    directory "/var/cache/bind";
    forwarders {
        8.8.8.8;
        1.1.1.1;
    };

    forward only;
};
  1. Now create this file /etc/bind/named.conf.custom and configure it:
zone "example.net" {
    type forward;
    forwarders {
        172.20.11.11;
        172.20.11.12;
    };
};
  1. Edit /etc/bind/named.conf.local and include the new custom configuration:
include "/etc/bind/named.conf.custom";
  1. Restart BIND
sudo systemctl restart bind9
  1. Now configure your /etc/resolv.conf like this:
nameserver 127.0.0.53
search .

All requests will go to BIND that’s running locally and based on the configuration it decides where to send the DNS traffic.