Coder Perfect

In Linux, DNS caching is possible.

Problem

DNS caching has perplexed me. On a Linux system, I’m creating a simple forward proxy server and want to use the OS DNS cache.

If my understanding is correct, DNS caching occurs at the browser level. Then there’s DNS caching on the operating system level (Windows has it). I’m not sure if it’s included by default in Linux distributions).

So, how does a browser/proxy server make use of DNS caching on the operating system? I’m trying to figure out whether I can use Linux for DNS caching rather than doing it myself inside my proxy.

Thanks

Asked by agent.smith

Solution #1

Unless nscd is installed and functioning, there is no OS-level DNS caching for Linux (and possibly most Unix). Even yet, because it is faulty, the DNS caching feature of nscd is disabled by default, at least in Debian. The practical upshot is that your linux system very very probably does not do any OS-level DNS caching.

You could build your own cache in your app (as they did for Squid, according to diegows’ comment), but I wouldn’t encourage it. It’s a lot of work, it’s simple to do it wrong (nscd did!!! ), it’s probably not as configurable as a specialized DNS cache, and it replicates functionality that already exists outside your application.

If a user of your software requires DNS caching because the DNS query load is too high or the RTT to the external DNS server is too long, they can install a caching DNS server like Unbound on the same machine as your application and configure it to cache responses and forward misses to the regular DNS resolvers.

Answered by Celada

Solution #2

On Linux, there are two more software packages that can be used for DNS caching:

You set the system’s DNS resolver to 127.0.0.1 in /etc/resolv.conf after setting the software for DNS forwarding and caching.

If your system uses NetworkManager, you can either use the dns=dnsmasq option in /etc/NetworkManager/NetworkManager.conf or change your connection settings to Automatic (Address Only), then use a script in the /etc/NetworkManager/dispatcher.d directory to get the DHCP nameserver, set it as the DNS forwarding server in your DNS cache software, and then force a configuration reload.

Answered by Zan Lynx

Solution #3

You have here available an example of DNS Caching in Debian using dnsmasq.

Configuration summary:

# Ensure you add this line
DNSMASQ_OPTS="-r /etc/resolv.dnsmasq"
# Your preferred servers
nameserver 1.1.1.1
nameserver 8.8.8.8
nameserver 2001:4860:4860::8888
nameserver 127.0.0.1

Then just restart dnsmasq.

Using DNS 1.1.1.1 as a benchmark:

for i in {1..100}; do time dig slashdot.org @1.1.1.1; done 2>&1 | grep ^real | sed -e s/.*m// | awk '{sum += $1} END {print sum / NR}'

Benchmark test using you local cached DNS:

for i in {1..100}; do time dig slashdot.org; done 2>&1 | grep ^real | sed -e s/.*m// | awk '{sum += $1} END {print sum / NR}'

Answered by Tk421

Solution #4

A DNS cache is included in Firefox. To turn off the DNS cache, do the following:

Firefox will use the DNS cache provided by the operating system if this option is deactivated.

Answered by Focus Linux

Post is based on https://stackoverflow.com/questions/11020027/dns-caching-in-linux