This guide will go through how to flush DNS cache in Linux. On a computer’s operating system or web browser, the term “DNS cache” refers to the temporary storage of data about past DNS lookups.
A DNS lookup can be swiftly retrieved by your OS or browser if you keep a local copy of it, which makes it possible to resolve a website’s URL to its matching IP much more quickly.
How to flush DNS cache in Linux
- Start by finding out which type of resolver your system is using i.e systemd-resolved or dnsmasq using the command below.
sudo lsof -i :53 -S
Sample output
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME systemd-r 709 systemd-resolve 13u IPv4 35920 0t0 UDP localhost:domain systemd-r 709 systemd-resolve 14u IPv4 35921 0t0 TCP localhost:domain (LISTEN)
In the example above the system uses systemd-resolved
How to flush DNS cache in Linux using systemd-resolved
- To flush the DNS cache using systemd-resolved, use one of the following command.
sudo systemd-resolve --flush-caches
or
sudo resolvectl flush-caches
- Next, check the current cache size using the following command.
sudo systemd-resolve --statistics
or
sudo resolvectl statistics
Sample output
DNSSEC supported by current servers: no Transactions Current Transactions: 0 Total Transactions: 2050 Cache Current Cache Size: 2 Cache Hits: 389 Cache Misses: 1671 DNSSEC Verdicts Secure: 0 Insecure: 0 Bogus: 0 Indeterminate: 0
Flush DNS cache in Linux using signals
- Sending a USR2 signal to the systemd-resolved can also be used to clear the DNS cache.
sudo killall -USR2 systemd-resolved
- Next, use the USR1 command below to dump the current state.
sudo killall -USR1 systemd-resolved
- Print the journal on your terminal using the following command.
sudo journalctl -r -u systemd-resolved
Sample output
Nov 01 01:03:44 itnixpro systemd-resolved[709]: Server dropped DO flag: no Nov 01 01:03:44 itnixpro systemd-resolved[709]: Seen invalid packet: no Nov 01 01:03:44 itnixpro systemd-resolved[709]: Seen RRSIG RR missing: no Nov 01 01:03:44 itnixpro systemd-resolved[709]: Seen OPT RR getting lost: no Nov 01 01:03:44 itnixpro systemd-resolved[709]: Seen truncated packet: no Nov 01 01:03:44 itnixpro systemd-resolved[709]: Failed TCP attempts: 0 Nov 01 01:03:44 itnixpro systemd-resolved[709]: Failed UDP attempts: 0 Nov 01 01:03:44 itnixpro systemd-resolved[709]: Maximum UDP fragment size received: 512 Nov 01 01:03:44 itnixpro systemd-resolved[709]: Can do DNSSEC: no Nov 01 01:03:44 itnixpro systemd-resolved[709]: DNSSEC Mode: no Nov 01 01:03:44 itnixpro systemd-resolved[709]: Possible feature level: UDP+EDNS0 Nov 01 01:03:44 itnixpro systemd-resolved[709]: Verified feature level: UDP+EDNS0 Nov 01 01:03:44 itnixpro systemd-resolved[709]: [Server 192.168.171.2 type=link interface=ens33] Nov 01 01:03:44 itnixpro systemd-resolved[709]: [Scope protocol=dns] Nov 01 01:03:44 itnixpro systemd-resolved[709]: www.googleapis.com IN AAAA 2a00:1450:401a:805::200a Nov 01 01:03:44 itnixpro systemd-resolved[709]: www.googleapis.com IN AAAA 2a00:1450:401a:800::200a Nov 01 01:03:44 itnixpro systemd-resolved[709]: www.googleapis.com IN AAAA 2a00:1450:401a:801::200a Nov 01 01:03:44 itnixpro systemd-resolved[709]: www.googleapis.com IN A 172.217.170.170 Nov 01 01:03:44 itnixpro systemd-resolved[709]: www.googleapis.com IN A 172.217.170.202 Nov 01 01:03:44 itnixpro systemd-resolved[709]: CACHE: Nov 01 01:03:44 itnixpro systemd-resolved[709]: [Scope protocol=dns interface=ens33] Nov 01 00:53:12 itnixpro systemd-resolved[709]: Flushed all caches. Nov 01 00:50:38 itnixpro systemd-resolved[709]: Flushed all caches. Okt 31 11:57:58 itnixpro systemd-resolved[709]: Clock change detected. Flushing caches. Okt 31 11:57:38 itnixpro systemd-resolved[709]: ens33: Bus client set DNS server list to: 192.168.171.2 Okt 31 11:57:38 itnixpro systemd-resolved[709]: ens33: Bus client set default route setting: yes Okt 31 11:57:38 itnixpro systemd-resolved[709]: ens33: Bus client set search domain list to: localdomain Okt 31 11:57:31 itnixpro systemd[1]: Started Network Name Resolution. Okt 31 11:57:31 itnixpro systemd-resolved[709]: Using system hostname 'itnixpro'. Okt 31 11:57:30 itnixpro systemd-resolved[709]: Negative trust anchors: home.arpa 10.in-addr.arpa 16.172.in-addr.arpa 17.172.in-addr.arpa 18.172.in-addr.arpa 19.172.in-addr.arpa 20.172.in-addr.arpa 21.1> Okt 31 11:57:30 itnixpro systemd-resolved[709]: . IN DS 20326 8 2 e06d44b80b8f1d39a95c0b0d7c65d08458e880409bbc683457104237c7f8ec8d Okt 31 11:57:30 itnixpro systemd-resolved[709]: Positive Trust Anchors: Okt 31 11:57:29 itnixpro systemd[1]: Starting Network Name Resolution... -- Boot 7f2caea837404e919cd6b59fcdd0a6fc -- Okt 30 01:15:00 itnixpro systemd[1]: systemd-resolved.service: Consumed 4.395s CPU time. Okt 30 01:15:00 itnixpro systemd[1]: Stopped Network Name Resolution. Okt 30 01:15:00 itnixpro systemd[1]: systemd-resolved.service: Deactivated successfully. Okt 30 01:15:00 itnixpro systemd[1]: Stopping Network Name Resolution... Okt 30 01:09:30 itnixpro systemd-resolved[536]: ens33: Bus client set DNS server list to: 192.168.171.2 Okt 30 01:09:30 itnixpro systemd-resolved[536]: ens33: Bus client set default route setting: yes Okt 30 01:09:30 itnixpro systemd-resolved[536]: ens33: Bus client set search domain list to: localdomain Okt 30 01:09:30 itnixpro systemd-resolved[536]: ens33: Bus client reset DNS server list. Okt 30 01:09:30 itnixpro systemd-resolved[536]: ens33: Bus client set default route setting: no Okt 30 01:09:30 itnixpro systemd-resolved[536]: ens33: Bus client reset search domain list. Okt 30 01:08:09 itnixpro systemd-resolved[536]: ens33: Bus client set DNS server list to: 192.168.171.2 Okt 30 01:08:09 itnixpro systemd-resolved[536]: ens33: Bus client set default route setting: yes Okt 30 01:08:09 itnixpro systemd-resolved[536]: ens33: Bus client set search domain list to: localdomain Okt 30 01:08:09 itnixpro systemd-resolved[536]: ens33: Bus client reset DNS server list. Okt 30 01:08:09 itnixpro systemd-resolved[536]: ens33: Bus client set default route setting: no Okt 30 01:08:09 itnixpro systemd-resolved[536]: ens33: Bus client reset search domain list. Okt 30 01:07:35 itnixpro systemd-resolved[536]: ens33: Bus client set DNS server list to: 192.168.171.2 Okt 30 01:07:35 itnixpro systemd-resolved[536]: ens33: Bus client set default route setting: yes Okt 30 01:07:35 itnixpro systemd-resolved[536]: ens33: Bus client set search domain list to: localdomain Okt 30 01:07:31 itnixpro systemd-resolved[536]: ens33: Bus client reset DNS server list.
How to flush DNS cache in Linux using dnsmasq
- Run the command below to flush the DNS cache using dnsmasq. Note, your system should be using dnsmasq.
sudo killall -HUP dnsmasq
- Next, check if dnsmasq is active using the command below.
sudo systemctl is-active dnsmasq
- To check the status, use the following command.
sudo service dnsmasq status
- Restart dnsmasq using the command below.
sudo systemctl restart dnsmasq
or
sudo service dnsmasq restart
- You have reached the end of our article, we have gone through how to flush DNS cache in Linux.
Read more about DNS
Other Tutorials
Install vnStat network traffic monitor on Debian 11