How to flush DNS resolver cache

Note – this works on Ubuntu 18.04, 20.04 and Centos 8

Check that systemd-resolved is running by entering the command below.

> sudo systemctl status systemd-resolved
If the service is running you should see output like that above.

To check the number of items in the cache run:

>sudo systemd-resolve statistics
This will show the size of the cache and current hits.

To clear the local DNS cache run the command below

> sudo systemd-resolve --flush-caches

If you run the command systemd-resolve –statistics again you will now see the cache is empty.