Flush the DNS cache on Linux
Identify the local DNS resolver and flush its cache with systemd-resolved or dnsmasq.
Flushing the DNS cache on Linux depends on the local resolver in use: systemd-resolved, dnsmasq or another one. The first step is therefore to identify the local resolver.
Identify the local DNS resolver
sudo lsof -i :53 -S
systemd-resolve --statistics
nmcli dev show | grep DNS
For static resolution of specific names, the /etc/hosts file can be used.
Flush the cache with systemd-resolved
resolvectl statistics
sudo systemd-resolve --flush-caches
sudo resolvectl flush-caches
sudo systemd-resolve --statistics
resolvectl statistics
Comparing the statistics before and after the flush shows whether the command took effect.
Dump the systemd-resolved cache to the log
The USR1 signal asks systemd-resolved to write its cache information to the journal:
sudo killall -USR1 systemd-resolved
sudo journalctl -u systemd-resolved > dns.txt
sudo journalctl -r -u systemd-resolved
Flush the dnsmasq cache
sudo killall -HUP dnsmasq
Check the dnsmasq service state, and restart it if needed:
sudo systemctl is-active dnsmasq
sudo systemctl restart dnsmasq
sudo systemctl status dnsmasq
On SysVinit systems, use sudo service dnsmasq status and sudo service dnsmasq restart.