14

Using sudo systemd-resolve --statistics I can see the current DNS cache statistics, for example:

Cache                     
  Current Cache Size: 68  
          Cache Hits: 412 
        Cache Misses: 461

I would like to see all the entries of the DNS cache (here 68), is it possible?

johhnry
  • 165
  • 1
    think you can do: killall -USR1 systemd-resolved (the output will be redirected to journald) –  Jul 10 '20 at 06:56
  • But this is going to kill the process rather than printing the cache? – johhnry Jul 14 '20 at 19:01
  • 1
    no, it wil just sending SIGUSR1 to systemd-resolved, you can find a description in man systemd-resolved –  Jul 14 '20 at 19:26

2 Answers2

21

You can use the following command to send signal USR1 (User-defined signal 1) to systemd-resolved:

sudo pkill -USR1 systemd-resolve

This will NOT stop the service. It just tells systemd-resolved to write all the current cache entries (and some other information) to the system log.

You can then export the log messages written by systemd-resolved to a text file with the following command:

sudo journalctl -u systemd-resolved > ~/resolved.txt

Open the text file generated this way in the text editor of your choice and search for CACHE:. After this, the list of cache entries will follow.

Please note that the text file might contain several lines containing CACHE:.

5

Since systemd 254 (issue 14796, released on 2023) we have

sudo resolvectl show-cache

From v254 release notes:

    * resolvectl gained a new verb "show-cache" to show the current cache
      contents of systemd-resolved. This verb communicates with the
      systemd-resolved daemon and requires privileges.

To empty the cache, resolvectl flush-caches (systemd-resolve --flush-caches in older versions of systemd).

Pablo Bianchi
  • 17,552