I'm looking for a way to flush the local DNS cache on a CentOS 6.

The system is not running any DNS server or anything, and I wish to let every DNS query go out to the configured nameserver, even for the duplicate ones.

Most of what I found online tell me to do service nscd restart, reload or do nscd -i hosts. However, none seems to flush the cache.

So I'm wondering if anyone has an idea on how I might do this. Is there some kind of switch in the kernel I need to flip? Any kind of work around is fine as well.

  • What are you doing to check if the cache has been flushed or not?
    – John
    Mar 11, 2013 at 19:18
  • ok it's a bit complicated, I have a program on my system listening on port 53 and forward the DNS queries in certain way, and also a http proxy using localhost as 'DNS server'; on the first query (say wget -e 'http_proxy=localhost:3128' xxx.com) I can see the query is being forwarded correctly, but all subsequent ones are not. If I wait long enough (cache expires), then it'll work again. Mar 11, 2013 at 19:24
  • And also I've configured the proxy (squid) to not cache any object, so I'd assume it's the system still caching the answer somehow Mar 11, 2013 at 19:27
  • 1
    nscd -i hosts -> works everytime. I restarted nscd 3 times in a row and it didn't want to clear the cache.
    – Danie
    Mar 14, 2013 at 9:59
  • 1
    nscd doesn't seem to be a thing in CentOS 7 minimal. I know the question calls out CentOS 6, but the title calls out CentOS in general. What's the CentOS 7 way? Feb 22, 2019 at 15:54

3 Answers 3


Its not your local box which is caching the DNS requests but it is the DNS resolver which you are using in your /etc/resolv.conf who is caching.

To prevent to get those cached queries reply:

  1. Change the resolver.

    $ dig @<resolve-ip> www.google.com

  2. Flush the DNS cache on the resolver, if you can access the DNS server.

    $ sudo /etc/init.d/bind restart

  • Hmm but I have set dns_nameservers in proxy config file, and the listener only forwards queries to preconfigured name server, shouldn't it be the case that resolv.conf is not even consulted? Mar 11, 2013 at 20:18
  • 4
    "bash: /etc/init.d/bind: No such file or directory" And the OP's method gets "Failed to restart nscd.service: Unit nscd.service failed to load: No such file or directory." I guess these have been moved/changed. Why can't they just leave things that work in place, or at least maintain aliases? Bad enough to have to become an OS-specialist to simply use a Linux-box. Worse when you have to re-learn the OS with every release.
    – JosephK
    Mar 19, 2016 at 7:45

Even after refresh or flush of DNS cache on client machine if it doesn't work then look your server or client machine is bound to any NIS server if yes then change the "hosts: files nis dns" to "hosts: files dns nis" entry in /etc/nsswitch.conf file and also you need to change the ip address in NIS master server hosts list.

  • This led to my solution. Inside /etc/hosts was the previously statically configured old ip address. Something I had done a while back. Deleting that line (or replacing with the new ip) solved the problem and allowed me to ping my machine using its hostname.
    – Paul
    Mar 30, 2016 at 13:26

I'm almost certain it's not the system caching the response - that part (system caching) is only handled by the nscd daemon. Restarting (or stopping entirely) that daemon resets or eliminates OS caching of name service request responses.

I'll offer two possibilities, though the custom listener you've set up on port 53 muddies the waters considerably:

  • A) Your system is issuing queries upstream, but the immediate upstream name resolver is caching the response based on either it's settings or the record's TTL.
  • B) Your custom listener is caching responses internally and just handing that response right back to the system when it gets asked again before the cache time has expired.
  • Thanks for the input, but the listener does nothing but forward the query and response, it's only like 200 lines of code after all. So shouldn't be the second case; also the listener is printing out everything it receives, so I'm sure it's indeed not getting anything :| Mar 11, 2013 at 19:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.