HomeTechnologyNewsHow to flush DNS on Linux

How to flush DNS on Linux

- Advertisement -
- Advertisement -
- Advertisement -
- Advertisement -

[ad_1]

fatmawati achmad zaenuri / Shutterstock.com

You can flush the DNS cache on a systemd-based Linux computer with the “resolvectl flush-caches” command. If you use dnsmasq, you can kill DNS using “sudo killall -HUP dnsmasq” instead.

Is your internet browsing experience slow on your Linux device, or are the websites you are visiting out of date or the wrong website? Let’s discuss how to clear the DNS cache on Linux and how to tell if you really need it.

What are DNS caches?

Domain Name Service is the magic that turns 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 properly route traffic to those devices or sites.

These searches, known as petitionsIt doesn’t happen instantly. There is a small and finite period of time involved. DNS requests from the Internet 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 even faster, responses to recent DNS requests are cached on precursor DNS servers.

If the response to a DNS request is found in the cache of the parent server, no further servers need to be contacted. The response is returned from the precursor server’s cache. Similarly, your broadband router at home maintains a small cache. If you request a local network device using its network device name, your router provides the IP address. You can also cache responses that you have received from external DNS servers.

Linux computers and networks are typically configured to use external DNS services, either provided by your Internet Service Provider or 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 is not running a DNS server, may cache the results of the DNS request.

The problem with using cached data is that everything is based on 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 gets corrupted, you get spotty performance at best and security vulnerabilities at worst. That’s when you’ll want to look for “flush” or clear the DNS cache.

Is your computer using a local DNS cache?

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

To determine if your Linux computer is caching DNS requests, use the is-active option of the systemctl domain. The daemon that manages the DNS cache is the systemd network name resolver, known as systemd-resolved.

systemctl is-active systemd-resolved

Using the systemctl command to check if DNS caching is active

If the response is “active”, then DNS caching is taking place. If the answer is “inactive”, it is not. 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

using the resolvectl command to view DNS caching statistics

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

RELATED: How to kill processes from Linux terminal

Check your DNS cache

Checking DNS cache entries is not a prerequisite to flushing the cache, and if you have no interest in doing so, you can skip this step entirely. Sometimes, however, it can be informative. You may see scrambled entries indicating corruption, or you may see error messages related to device addressing issues on your network.

Now, there is no easy way to view these entries. We can do it, but we have to be a little creative. USR1either user defined signal number oneis a signal that can be sent by the kill Y killall commands This signal has no predefined meaning. Applications are free to ignore this signal or react in whatever way the developers have implemented.

the systemd-resolved Daemon reacts to USR1 writing its cache to the system logs. So we can use the journalctl command to filter DNS entries.

We will use the killall command with USR1 To send the signal to systemd-resolved evil. Note that although we are using the killall command, the systemd-resolved daemon continues to run. This is not a termination signal that we are sending.

sudo killall -USR1 systemd-resolved

Sending the USR1 signal to the daemon resolved by systemd

Now we will use the journalctl command with the -u (filtered by systemd unit) option to extract log entries that have been generated by systemd-resolved . We’ll redirect that output to a text file called “dns.txt”.

sudo journalctl -u systemd-resolved > dns.txt

Filter log entries resolved by systemd and send them to a file

We will use the less file viewer to view the contents of the file.

less dns.txt

You will 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 this by putting the IP address into a web browser. You should see the Google search home page.

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 the bad and corrupt entries from the cache.

The command is simple; we use resolvectl with the flush-caches option.

resolvectl flush-caches

Flush the DNS cache with the resolvectl command

We quietly return to the command line. To confirm that something really happened, we’ll check the DNS cache statistics again.

resolvectl statistics

Cache statistics showing that the cache size has been reduced to zero

We can see that the cache size is reduced to zero. It will increase over time as you accumulate new entries.

How to flush the dnsmasq cache on Linux

the dnsmasq The app 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.

rinse the dnsmasq DNS caching is easy. We need to send the SIGHUP signal, which tells the dnsmasq daemon to effectively restart. Doing so clears your DNS cache. To send the signal we use the killall command with the -HUP indicator and the name of the application.

sudo killall -HUP dnsmasq

Flush the DNS cache of the dnsmasq application

Blushing, Successful

Of course, if your computer isn’t caching at all, there’s nothing you need to check.

If you are caching DNS requests but everything is working fine, you can also ignore it. But if you experience slow or sporadic web page updates when you browse the web, or see the wrong web pages, it’s probably a good time to clear your DNS cache.

[ad_2]

- Advertisement -
- Advertisement -
Must Read
- Advertisement -
Related News
- Advertisement -