30
October
2023
11:11

Configuring a systemd-resolved caching DNS server

30 October 2023 11:11

To speed up DNS name resolution, a local caching server is used. The difference (speedup) is 50-150 milliseconds per request for cached data. How to enable the service systemd-resolved in Linix Mint or Ubuntu?

  1. Installing DNS resolver for SystemD in Ubuntu/Linux Mint:
sudo apt install libnss-resolve
  1. Setting up the ResolveD service
sudo nano /etc/systemd/resolved.conf

Remove comments next to lines

[Resolve]
DNS=192.168.1.1
FallbackDNS=77.88.8.1

where 192.168.1.1 - replace with the IP address of the main router on the local network,
instead of 77.88.8.1 you can specify any public DNS server.

  1. Start and enable the ResolvedD service for DNS resolution
sudo systemctl start systemd-resolved
sudo systemctl enable systemd-resolved
  1. Checking status
resolvectl status
  1. Display cache statistics (hits/misses)
resolvectl statistics
  1. We make synchronous changes to Resolv.conf parameters in accordance with /run/systemd/resolve/resolv.conf

Some utilities - ping, dig, host, whois - bypass the caching proxy, because use the resolveconf service, which accesses the /etc/resolv.conf file, which is not associated with SystemD. For such programs you need to create a permanent symbolic link from the SystemD file /run/systemd/resolve/resolv.conf на целевое имя ссылки /etc/resolv.conf. (This "trick" is described in article on jtprog.ru).

sudo cp /etc/resolv.conf /etc/resolv.conf.bak
sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
ls -l /etc/resolv.conf

Following the symbolic link to the file /etc/resolv.conf, the lines are now written in the file /run/systemd/resolve/resolv.conf
nameserver DNS_server IP_address
search.

Where the DNS_server_IP_address was automatically transferred using a symbolic link /etc/resolv.conf на файл /run/systemd/resolve/resolv.conf, соответствующей файлу /etc/systemd/resolved.conf, edited in paragraph 2 of the instructions.

Check:

  • corrected the file nano /etc/systemd/resolved.conf => указал IP-адрес DNS=8.8.8.8 and
  • restarted the service sudo systemctl restart systemd-resolved

    Result:

  • the higher-level DNS server (uplink) has changed from the previous IP address of the router to 8.8.8.8:
    The command output indicates a change in the DNS server:
    resolvectl status

This item can also be useful for setting up synchronous changes in /etc/resolv.conf when updating /etc/systemd/resolved.conf

  1. Using the console utility resolvectl

Утиlita resolvectl can be used for service management systemd-resolved.service and for domain name resolution. The most common command of this utility is query HOST_NAME for name resolution, both IPv4 and IPv6. When used with the -t TYPE switch, where TYPE is a designation of the type of DNS record, for example, TXT records (certificates) versus regular A, AAAA records. A detailed description of the resolvectl command switches is available in the man resolvectl help pages.

Examples of use:

resolvectl query ya.ru
resolvectl query google.com -t AAAA

7.1 Displaying the service status resolved

systemctl is-active systemd-resolved

Answer: "active" - the service is running.

7.2. Displaying statistics of calls to the local DNS cache

resolvectl statistics

Answer:

DNSSEC supported by current servers: no

Transactions
Current Transactions: 0
Total Transactions: 7606

Cache
Current Cache Size: 136
          Cache Hits: 2139
        Cache Misses: 5493

DNSSEC Verdicts
              Secure: 0
            Insecure: 0
               Bogus: 0
       Indeterminate: 0

7.3. Clearing the DNS cache using the resolvectl command in Linux:

resolvectl flush-caches

Last edit of the article: 01/23/2025


Sources:
https://zevilz.dev/posts/496/
systemd-resolve-command-not-found
*systemd-resolved at wiki.ubuntuusers.de



Related publications