Raspberry Pi: Faster Internet with DNS caching

While DNS resolution is an issue that most people probably don't worry about, the process of resolving an IP address from a domain has a significant impact on the speed of browsing. Some relief can be provided by a DNS cache on a Raspberry Pi.

After I changed my internet provider and now have a VDSL line with a sync of 51.4 Mbps downstream and 10 Mbps upstream I started to release the last brakes.

Raspberry Pi Logo
Raspberry Pi Logo
At first I noticed that after entering a URL it takes a short reminder second until the loading of the website starts.

One approach: a Raspberry Piconnected directly to the Fritzbox (I use a Fritz!Box 7490) works as DNS cache [with pdnsd] and DHCP server (since no alternative DNS server can be entered in the Fritzbox for distribution via DHCP).

Raspberry Pi as DNS cache

If you are using raspbian (Debian) and do not want to use a full DNS server like bind, the package pdnsd is a good choice. pdnsd has the advantage that the cache data is not only stored in RAM but also on the SD card of the Pi. Cached DNS entries are therefore preserved even after a restart of the Pi. The configuration of pdnsd is easy, the most important adjustments in the /etc/pdnsd.confI have left most of the values on their standards:

perm_cache=8192; // Cache size in kilobyte (here: 8 megabyte)
cache_dir="/var/cache/pdnsd"; // Cache directory
server_ip = 0.0.0.0.0; // IP on which listening is done: 0.0.0.0.0 means on all IPs

In order to be able to answer queries that are not yet in the cache, you must also configure DNS servers for pdnsd. By default, the program resolvconf the name servers must then be accessed via the file /etc/network/interfaces can be configured. There, an interface entry contains the line dns-nameservers 8.8.8.8 8.8.4.4 added:

car eth0
iface eth0 inet static
address 192.168.0.10
netmask 255.255.255.0
network 192.168.0.0
gateway 192.168.0.1
dns-nameservers 217.0.43.161 217.0.43.177 8.8.8 8.8.4.4

With this configuration the Raspberry Pi gets the IP address 192.168.0.10, uses the Fritzbox (192.168.0.1) as gateway and the Telekom-DNS-Server 217.0.43.161 and 217.0.43.177 and Google's public DNS servers (8.8.8.8 and 8.8.4.4). Runs pdnsdyou can use sudo /etc/init.d/pdnsd status learn what the cache status is:

Cache status:
=============
8192 kB maximum disk cache size.
613647 of 8398848 bytes (7.31%) memory cache used in 2275 entries.

DHCP on Raspberry Pi

To make sure that the settings are adopted by all clients without additional configuration effort, you have to deactivate the DHCP server of the Fritzbox and set up an alternative DHCP server on the Raspberry Pi install:

sudo aptitude install isc-dhcp-server

The configuration is then saved using the file /etc/dhcp/dhcpd.conf realized. Since this is also well documented with comments, I only go into the most important points here again:

option domain name server 192.168.0.10; // IP of our DNS cache
subnet 192.168.0.0 netmask 255.255.255.0 {
range 192.168.0.70 192.168.0.100;
option domain name servers 192.168.0.10;
option domain name "fritz.box";
option routers 192.168.0.1;
option broadcast-address 192.168.0.255;
default-lease-time 600;
max-lease-time 7200;
}

This gives all DHCP clients an IP between 192.168.0.70 and 192.168.0.100 and the correct data needed to use the DNS cache on the Raspberry Pi. DNS resolution of already visited domains runs within 2 milliseconds (which is basically twice the ping in my network).

DNS cache on Raspberry Pi - Conclusion

It may not seem important to most people, but for a heavy user like me it makes a difference if the DNS resolution is a few milliseconds faster. The biggest advantage: surfing feels "crisper", especially on websites that you visit more often and where the DNS data is safely cached. I set the minimum cache duration to one day and the maximum to one week. If a website now changes the IP address, which is the case for example when a server is moved. In such a case the pdnsd cache must be reset.

A side note: the Raspberry Pi can be stably operated on the USB port of the Fritz!Box 7490, so a separate power supply for the small computer is not required for this configuration. A further possibility to use or extend this setup is the Using the Raspberry Pi as a Privoxy server in the home network.

12 thoughts on “Raspberry Pi: Schnelleres Internet mit DNS-Caching”

  1. Is it possible to overwrite individual DNS entries? I would like the Raspberry to resolve certain domains with local IPs. The Fritz! boxes all struggle with performance problems when the target points to them again (NAT loopback).

  2. Thank you for the brief instructions. DNA works great. What about DHCP? Do I still have to connect to the router? How does the Raspberry Pi recognize that I should use DHCP when I connect to the router (I have an old speedport)?

    1. You need to configure the router to distribute the Raspberry Pi as a DNS server in the DHCP response for the clients. I wouldn't run the Pi itself over DHCP, but assign a fixed IP address like the router itself.

      The IP address to "outside" is always dynamic anyway, you can't set it. But it doesn't matter with this setup.

  3. Does your router have the option of using the Fritz!Box software? Probably works with some older models. Then you could adjust it. That was one of the things that drove me from the rented Telekom Speedport router to the Fritzbox 😉

  4. Hi there!

    I have set up pdnsd as specified. However, I have observed the following phenomenon.
    If I trigger a dns resolution with "dig domain", I usually get a result between 150-300ms as first value. If I then resolve the same domain again with "dig domain", the value is 5-10ms, as hoped.
    So far so good!
    But if I enter a domain name in the browser and then resolve it with "dig domain", the value is again 150-300ms. Why this? The resolution by the browser must have been cached?

    TIA

  5. What exactly is the point? DNS entries in the name server have TTLs of mostly 3600 seconds (1h). Since most people don't empty their DNS cache more often (like by rebooting the system), a cache on the SD card doesn't help at all. And if you ignore the TTL and cache it longer, you run the risk of caching outdated IP addresses. The point of TTLs is to give you an expiration date for DNS entries.
    If you have a halfway modern router, you will also have a DNS cache with it, and if you measure the time to resolution once, you will see that a second, identical request is answered under/equal to 1 msec, i.e. directly from the router's cache.
    The only reason why you would want to use a DNS server on the Raspberry Pi would be to have a DNSSEC-enabled resolver at home or to manage your own DNS zone, which I would certainly not use a Raspberry Pi for 😉

Leave a Comment

Your email address will not be published. Required fields are marked *

en_USEnglish
Scroll to Top