Home CentOS How to flush DNS on Ubuntu and CentOS

How to flush DNS on Ubuntu and CentOS

by Hend Adel
Published: Last Updated on
flush dns ubuntu centos

DNS (Domain Name Service) is responsible for mapping a website name into the respective IP. Click here to know how to install and configure DNS on Ubuntu.

Therefore, DNS is considered being one of the critical parts of your connection to the internet. To access the frequently visited websites faster, our machines keep track of DNS records, or in other words, it caches it.

What if a website changed its address? It may cause an IP conflict. So, to avoid such a problem, your machine DNS cache should be flushed regularly. Also, clearing your DNS cache will help you removing unneeded data occupying your system and solve some technical problems related to the famous “outdated entries” error.

In this tutorial, we will show you how to flush your DNS on Ubuntu and CentOS operating systems in easy and clear steps.

Flush DNS on Ubuntu Machine

First Method: Flush DNS by using systemd-resolve

Step 1. Before flushing your DNS, you can check DNS statistics using the next command.

sudo systemd-resolve --statistics

The output should be something like that:

Cache Statistics

Cache Statistics

As per the previous screenshot, the current cache size is 6.

Step 2. Flush DNS using systemd-resolve. Since the systemd-resolve daemon is almost running on all Ubuntu systems, then we can use it to clear our DNS cache using the next command.

sudo systemd-resolve --flush-caches

Step 3. Re-run the cache statistics command.

sudo systemd-resolve --statistics

Cache Statistics After Flush

As you can see from the above screenshot, the current cache size parameter is Zero, and that is what we need.

Second Method: Flush Cache using the dns-clean command

Another method to clear our DNS cache is using the next command:

sudo /etc/init.d/dns-clean start

Third Method: Flush Cache for some Ubuntu DNS Services

Next, we will show you some commands for clearing DNS cache for some various DNS services:

  • Flush nscd service DNS Cache:
sudo /etc/init.d/nscd restart
  • Flush BIND service DNS Cache:
sudo /etc/init.d/named restart
  • Flush dnsmasq service DNS Cache:
sudo /etc/init.d/dnsmasq restart
  • If you need to flush the DNS cache for a specific domain, use the following command:
sudo rndc flushname test.com
  • Flush DNS cache for entire LAN:
sudo rndc flush lan
  • Respectively, to flush DNS cache for entire WAN:
sudo rndc flush wan

Flush DNS on CentOS Machine

To clear your system DNS cache on a machine running CentOS platform, you can use the following command.

systemctl restart dnsmasq.service

The output should be something like that:

CentOS Flush DNS

CentOS Flush DNS

That’s all you need to know on how to flush DNS on Ubuntu and CentOS. For any further questions, leave us a comment, and we will be happy helping you.

You may also like

5 comments

Mike McGuffie August 27, 2019 - 5:31 AM

Hi there,
I’m missing as to where the SSL certificates I am to copy to the created folder are.
“Copy your SSL certificates to “/etc/apache2/ssl/ ” folder”
Thanks,
McG

Reply
adrian December 5, 2019 - 7:36 AM

why i can’t install the owncloud because in the command line ,they said that can’t locate the owncloud files

Reply
Ram N January 28, 2020 - 6:21 PM

Fantastic. Worked like a charm with no tweets. thanks!

Reply
Paul C Allsopp April 24, 2021 - 4:20 PM

Like MANY MANY resources on the internet, the assumption is always that what is mentioned works. But what if it doesn’t? Where is the section that says: well if you get “service unknown” you need to do this….?

It seems like many of these articles are nothing more than copies of each other, because they all have the same content, and no real solutions to problems.

Reply
Bob April 12, 2022 - 3:23 AM

Well, this is not *quite* all we need to know.
I have a CentOS where *None* of these mentioned tools are availble, yet it still remembers a nodename previously present in its /etc/hosts, and I can’t make it forget that name. So, login to mysql fails from that remote node. And I can’t restart the server-node.

So, you need to rephrase this guide 😛

Reply

Leave a Reply to adrian Cancel Reply

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.