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.netfrom local DNS servers172.20.11.11and172.20.11.12 - I want all other domains to be resolved from
8.8.8.8and1.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:
- Install BIND
sudo apt update
sudo apt install bind9
- Open
/etc/bind/named.conf.optionsand configure it like this:
options {
directory "/var/cache/bind";
forwarders {
8.8.8.8;
1.1.1.1;
};
forward only;
};
- Now create this file
/etc/bind/named.conf.customand configure it:
zone "example.net" {
type forward;
forwarders {
172.20.11.11;
172.20.11.12;
};
};
- Edit
/etc/bind/named.conf.localand include the new custom configuration:
include "/etc/bind/named.conf.custom";
- Restart BIND
sudo systemctl restart bind9
- Now configure your
/etc/resolv.conflike 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.
March 8, 2024 ∙