Admins eHow SysAdmin Tips & Tricks

December 2, 2022

Redirect all DNS requests to local caching DNS server

Filed under: dns,linux — Tags: , , , , — admin @ 2:44 am

For caching DNS Server, I use PowerDNS recursor server. Install it first :

apt install pdns-recursor

By default it listens on and should work right after the installation, but for faster performance I want it to forward all queries to which is Google’s public DNS server. so change /etc/powerdns/recursor.conf and add the following line :

forward-zones-recurse= .=;

Restart the service after config change :

systemctl restart pdns-recursor.service

Now you can test it :

dig @

You should get a valid response.
Now lets redirect all DNS queries to our local server :

iptables -t nat -I OUTPUT -m owner --uid-owner pdns -j RETURN
iptables -t nat -I POSTROUTING -m owner --uid-owner pdns -j RETURN
iptables -t nat -A OUTPUT -p udp --dport 53 -j DNAT --to
iptables -t nat -A POSTROUTING -p udp --dport 53 -j SNAT --to-source

The first two iptables rules prevent a loop in redirecting pdns queries to outside world ( in our case).
Done. Easy 😉

Powered by WordPress