How To Flush Dns in Linux

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

We select and review products independently. When you purchase through our links we may earn a commission.

Learn more.

How-To Geek

Linux

How to Flush DNS in Linux


DAVE MCKAY
NOV 14, 2022, 12:00 PM EST
| 4 MIN READ

fatmawati achmad zaenuri/Shutterstock.com

Key Takeaway

You can flush the DNS cache on a systemd-based Linux


computer with the "resolvectl flush-caches" command. If
you use dnsmasq, you can clear the DNS using "sudo killall -
HUP dnsmasq" instead.

Is your internet browsing experience slow on your Linux device,


or are the websites you’re visiting outdated or the wrong
website altogether? Let’s discuss flushing the DNS cache on
Linux, and how to know whether you really need to.
Table of Contents

What Are DNS Caches?

Is Your Computer Using a Local DNS Cache?


Reviewing Your DNS Cache

How to Clear the DNS Cache on Linux

How to Flush the dnsmasq Cache on Linux


Flushed, With Success

What Are DNS Caches?

The domain name service is the bit of magic that converts


names into numbers. It takes device network names and
website names and looks up their IP addresses. The network
can then use the IP address to correctly route traffic to those
devices or sites.

These look-ups, known as requests, don’t happen


instantaneously. There’s a small, finite period of time involved.
Internet DNS requests may require querying precursor DNS
servers, root name servers, top-level domain servers, and
authoritative name servers. DNS requests are fast, but to make
them faster still, the answers to recent DNS requests are
cached on the DNS precursor servers.

If the answer to a DNS request is found in the precursor


server’s cache, no further servers need to be contacted. The
answer is sent back from the precursor server’s cache.
Similarly, a small cache is maintained by your broadband
router at home. If you ask for a local network device using its
network device name, your router provides the IP address. It
may also cache responses it has received from external DNS
servers.
Usually, networks and Linux computers are configured to use
external DNS services, either provided by your Internet Service
Provider or by a free service such as OpenDNS or Google DNS.
There are good reasons why some people run their own DNS
server, but most of us don’t. However, your Linux computer—
even if it isn’t running a DNS server—can optionally cache DNS
request results.

The trouble with


using cached data RELATED
is the whole thing What is DNS Cache

is predicated on Poisoning?

the assumption
that none of the cached details have changed since they were
cached. If the details have changed, the information you
receive will be out of date.

If a cache entry or the entire cache becomes corrupt, you’ll


receive flaky performance at best, and security vulnerabilities
at worst. That’s when you’ll want to looking into “flushing” or
clearing the DNS cache.

Is Your Computer Using a Local DNS


Cache?

Some of our test computers had local DNS caches turned on,
and others had it turned off. It was off on our Manjaro 21
computer, but it was turned on by default on Fedora 37 and
Ubuntu 22.10.

To determine whether your Linux computer is caching DNS


requests, use the is-active option of the systemctl
command. The daemon that manages the DNS cache is the
systemd network name resolution manager, known as
systemd-resolved.
systemctl is-active systemd-resolved

If the response is “active”, DNS caching is taking place. If the


response is “inactive”, it isn’t. On this particular computer, it is
active. We can use the resolvectl command with the
statistics option to see how many records are in the cache.

resolvectl statistics

We can see there are 330 entries in the DNS cache of this
computer.

RELATED: How to Kill Processes From the Linux Terminal

Reviewing Your DNS Cache

Reviewing the DNS cache entries is not a prerequisite to


flushing the cache, and if you have no interest in doing so, you
can skip this entire step. Sometimes, though, it can be
informative. You might see scrambled entries that indicate
corruption, or you might see error messages related to device
addressing problems on your network.

Now, there isn’t a straightforward way to see these entries. We


can do it but need to be a little creative. USR1, or user-defined
signal number one, is a signal that can be sent by the kill and
killall commands. This signal has no predefined meaning.
Applications are free to ignore this signal or to react in
whatever way the developers have implemented.

The systemd-resolved daemon reacts to USR1 by writing its


cache to the system logs. We can then use the journalctl
command to filter out the DNS entries.

We’ll use the killall command with USR1 To send the signal
to the systemd-resolved daemon. Note that although we’re
using the killall command, the systemd-resolved daemon
continues to run. This isn’t a termination signal that we’re
sending.

sudo killall -USR1 systemd-resolved

Now we’ll use the journalctl command with the -u (filter by


systemd unit) option to extract the log entries that have been
generated by systemd-resolved . We’ll redirect that output
into a text files called “dns.txt.”

sudo journalctl -u systemd-resolved > dns.txt


We’ll use the less file viewer to view the contents of the file.

less dns.txt

You’ll be able to find the cached mappings between domain


names and IP addresses by scrolling and searching through
the text.

We can see an entry for Google that has an IP address of


216.58.212.196. You can check that by putting the IP address
in a web browser. You should see the home page of Google
search.

How to Clear the DNS Cache on


Linux

Flushing the cache removes all entries, and starts the


collection process once more. If there are any, this forcibly
removes incorrect and corrupt entries from the cache.
The command is simple; we use resolvectl with the flush-
caches option.

resolvectl flush-caches

We’re silently returned to the command line. To confirm that


something has actually happened, we’ll check the DNS cache
statistics again.

resolvectl statistics

We can see that the cache size is dropped to zero. It will


increase over time as it accumulates new entries.

How to Flush the dnsmasq Cache on


Linux
The dnsmasq application provides a DNS cache and a DHCP
server. It is popular with users who want to run their own DNS
server, especially on non-systemd installations.

Flushing the dnsmasq DNS cache is easy. We need to send the


SIGHUP signal, which tells the dnsmasq daemon to effectively
reinitialize. Doing so clears its DNS cache. To send the signal
we use the killall command with the -HUP flag, and the
name of the application.

sudo killall -HUP dnsmasq

Flushed, With Success

Of course, if your computer isn’t caching at all, there’s nothing


you need to check.

If it is caching DNS requests but everything is working just fine,


you can ignore it too. But if you’re experiencing slow or
sporadic webpage updates when you’re browsing the web, or
seeing the wrong webpages altogether, it’s probably a good
time to clear your DNS cache.

Linux Commands

Files tar · pv · cat · tac · chmod · grep · diff · sed


· ar · man · pushd · popd · fsck · testdisk ·
seq · fd · pandoc · cd · $PATH · awk · join ·
jq · fold · uniq · journalctl · tail · stat · ls ·
fstab · echo · less · chgrp · chown · rev ·
look · strings · type · rename · zip · unzip ·
mount · umount · install · fdisk · mkfs · rm ·
rmdir · rsync · df · gpg · vi · nano · mkdir ·
du · ln · patch · convert · rclone · shred ·
srm

alias · screen · top · nice · renice · progress


· strace · systemd · tmux · chsh · history · at
· batch · free · which · dmesg · chfn ·
usermod · ps · chroot · xargs · tty · pinky ·
Processes lsof · vmstat · timeout · wall · yes · kill ·
sleep · sudo · su · time · groupadd ·
usermod · groups · lshw · shutdown ·
reboot · halt · poweroff · passwd · lscpu ·
crontab · date · bg · fg

netstat · ping · traceroute · ip · ss · whois ·


fail2ban · bmon · dig · finger · nmap · ftp
Networking
· curl · wget · who · whoami · w · iptables ·
ssh-keygen · ufw

RELATED: Best Linux Laptops for Developers and Enthusiasts

DAVE MCKAY
Dave McKay first used computers when
punched paper tape was in vogue, and he has
been programming ever since. After over 30
years in the IT industry, he is now a full-time
technology journalist. During his career, he has worked as a
freelance programmer, manager of an international software
development team, an IT services project manager, and, most
recently, as a Data Protection Officer. His writing has been
published by  howtogeek.com, cloudsavvyit.com,
itenterpriser.com, and opensource.com. Dave is a Linux
evangelist and open source advocate. READ FULL BIO »
How-To Geek is where you turn when you want experts to explain technology. Since we
launched in 2006, our articles have been read more than 1 billion times. Want to know
more?

You might also like