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

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,371