Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

(I am aware that googling this question gives me tons of stuff, but literally none worked) I'm looking for a way to flush the local DNS cache on a Centos 6 system, it is not running any DNS server or anything. I wish to let every DNS query go out to the configured nameserver, even for the duplicate ones. 90% of what I found tells me to

service nscd restart
or reload or
nscd -i hosts
. Well none seems to flush the cache. So I'm wondering if anyone has an idea of how do I do this? Is there some kind of switch in the kernel I need to flip? Or any kind of work around is fine as well. Thanks a lot!

share|improve this question
What are you doing to check if the cache has been flushed or not? – John Mar 11 '13 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. – michael.z Mar 11 '13 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 – michael.z Mar 11 '13 at 19:27
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 '13 at 9:59

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

share|improve this answer
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? – michael.z Mar 11 '13 at 20:18
"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 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.

share|improve this answer
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 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.

share|improve this answer
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 :| – michael.z Mar 11 '13 at 19:55

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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