I am confused about DNS caching. I am writing a small forward proxy server and want to use OS DNS cache on a Linux system.
If I understand correctly, then there is DNS caching at the browser level. Then there is DNS caching at OS level (Windows has it. I am not sure if Linux distros have it by default).
So, how does a browser/proxy_server use OS DNS caching? I am trying to find if I can rely on Linux for DNS caching instead of doing it on my own inside my proxy.
Thanks
On Linux (and probably most Unix), there is no OS-level DNS caching unless nscd is installed and running. Even then, the DNS caching feature of nscd is disabled by default at least in Debian because it's broken. The practical upshot is that your linux system very very probably does not do any OS-level DNS caching.
You could implement your own cache in your application (like they did for Squid, according to diegows's comment), but I would recommend against it. It's a lot of work, it's easy to get it wrong (nscd got it wrong!!!), it likely won't be as easily tunable as a dedicated DNS cache, and it duplicates functionality that already exists outside your application.
If an end user using your software needs to have DNS caching because the DNS query load is large enough to be a problem or the RTT to the external DNS server is long enough to be a problem, they can install a caching DNS server such as Unbound on the same machine as your application, configured to cache responses and forward misses to the regular DNS resolvers.
You have here available an example of DNS Caching in Debian using dnsmasq.
Configuration summary:
/etc/default/dnsmasq
/etc/resolv.dnsmasq
/etc/resolv.conf
Then just restart dnsmasq.
Benchmark test using DNS 1.1.1.1:
Benchmark test using you local cached DNS:
Here are two other software packages which can be used for DNS caching on Linux:
After configuring the software for DNS forwarding and caching, you then set the system's DNS resolver to 127.0.0.1 in /etc/resolv.conf.
If your system is using NetworkManager you can either try using the
dns=dnsmasq
option in/etc/NetworkManager/NetworkManager.conf
or you can change your connection settings to Automatic (Address Only) and 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 trigger a configuration reload.Firefox contains a dns cache. To disable the DNS cache:
When disabled, Firefox will use the DNS cache provided by the OS.