How To Flush Linux / UNIX DNS Cache

by Vivek Gite on January 29, 2008 · 20 comments

Q. I'm on a Dial UP Internet connection under Linux and frequent dial up problem causing dns problems. How do I flush DNS cache under any UNIX / Linux distribution using a shell prompt?

A. Under any Windows version you can use ipconfig command to flush dns cache. However, Linux (UNIX) is more complex and robust as compare to Windows. Linux can run nscd or BIND or dnsmasq as the name service caching daemon under most Linux distributions. Large and workgroup servers may use BIND / dnsmasq as a dedicated caching server.

Flush nscd dns cache

Nscd caches libc-issued requests to the Name Service. If retrieving NSS data is fairly expensive, nscd is able to speed up consecutive access to the same data dramatically and increase overall system performance. Just restart nscd:
$ sudo /etc/init.d/nscd restart
OR
# service nscd restart

Flush dnsmasq dns cache

dnsmasq is a lightweight DNS, TFTP and DHCP server. It is intended to provide coupled DNS and DHCP service to a LAN. Dnsmasq accepts DNS queries and either answers them from a small, local, cache or forwards them to a real, recursive, DNS server. This software is also installed many cheap routers to cache dns queries. Just restart to flush out dns cache:
$ sudo /etc/init.d/dnsmasq restart

Flush caching BIND server dns cache

A caching BIND server obtains information from another server (a Zone Master) in response to a host query and then saves (caches) the data locally. All you have to do is restart bind to clear its cache:
# /etc/init.d/named restart

Related: Windows Vista / XP Flush DNS Cache with ipconfig Command

Featured Articles:

Share this with other sys admins!
Facebook it - Tweet it - Print it -

{ 20 comments… read them below or add one }

1 NoPremium.org April 18, 2008

it dont work for me i have ubunu 7.10

Reply

2 kazoolist July 10, 2008

I my experience service nscd restart does not clear NSCD’s cache. service nscd reload does, however.

Reply

3 Subhash July 27, 2010

That is correct!

Reply

4 James Mills September 19, 2008

Actually, in Bind, you can flush the daemon’s cache by doing

rndc flush

Reply

5 Lost Eagle January 31, 2011

Thank U

This “rndc flush” help lot … restart doesn’t flush …

Have a nice day

Reply

6 RudyD February 4, 2009

Whoops!

It seems that not the perfect view for precise approach. It seems that if I use bind and there is a resolv.conf too not always the bind is in use. (Ubuntu) Other important machine (with debian), there is just resolv.conf. Anyhow I have tried to restart (hope works) the /etc/init.d/dns-clean and it seems it worked. But there were reports that this did not do the job. Anyhow, I want to make sure that – especially my debian system – without restarting, even the networking, how can I just restart the name resolution to the newly set values and how to get sure that it is done well?

Thanks!

Reply

7 Tricky April 26, 2009

If nscd is in use, try “nscd -i hosts” as root.
As per James Mills, “rndc flush” if using bind.

If none of these work you might not have a DNS cache to flush anyway and the problem DNS entry may be stored elsewhere, such as your firefox DNS cache, your router’s cache, or another upstream DNS caching server.

Reply

8 takizo July 26, 2010

it could be nscd is not install?

@James, rndc reload works too :D

Reply

9 dejf August 31, 2010

“/etc/init.d/dnsmasq restart” do not work, even stop and start does not. There is a clear cache option entioned, but it does not help anyway – dnsmasq remembers…

Reply

10 Yogesh September 2, 2010

I guess this is for DNS server. What shall I do if I wish to clear DNS cache on a linux client/desktop machine? [Like ipconfig /flushdns in windows]

Reply

11 Tricky September 2, 2010

@Yogesh: The same commands apply to a desktop

@dejf: Bear in mind per my comment above, perhaps your desktop has a separate DNS cache or the DNS is being cached elsewhere. You have to flush the DNS on all your “upstream” DNS servers as well or change the DNS servers that you’re using.
See the following for info on how to change your DNS settings to these 2 popular public DNS servers:
http://code.google.com/speed/public-dns/
https://store.opendns.com/setup/router/

Reply

12 Yogesh September 2, 2010

@Tricky
Thanks for comment, but I am unable to find /etc/init.d/nscd or named on my Centos 5.3 Desktop. Any help?

Reply

13 Tricky September 3, 2010

@Yogesh. Chances are the DNS is cached outside of your desktop or the DNS is being cached by an application directly.

I wrote an (admittedly regex-cryptic) article on this same issue (sorry for the “competing” link, Vivek) – http://dogma.swiftspirit.co.za/archives/300 – skip to the sections after the label “Flush Mozilla Firefox’s internal DNS cache”.

Reply

14 Manish December 4, 2010

Hi

I have a local name resolution issues on CentOS 5.5. I have edited the /etc/hosts file and still the hostname doesn’t resolves to right IP.

Here is what I have priority configuration
# more /etc/host.conf
order hosts,bind
Also /etc/nsswitch.conf file has

hosts: files dns
more /etc/hosts
192.168.1.1 admin.domain.com admin

# host admin
Host admin not found: 3(NXDOMAIN)

Any suggestions would be appreciated.

Reply

15 Tricky December 6, 2010

host doesn’t bother with the entries in the hosts file. Try ping admin instead.

Reply

16 nobbiecentos February 8, 2011

TY for information its working solution for clear cash from server side

Reply

17 takprosto April 9, 2011

Im sorry for my bad english…. but im have Debian.
apt-get install nscd Ok
-d, –debug Do not fork and display messages on the current tty
-f, –config-file=NAME Read configuration data from NAME
-g, –statistic Print current configuration statistic
-i, –invalidate=TABLE Invalidate the specified cache
-K, –shutdown Shut the server down
-t, –nthreads=NUMBER Start NUMBER threads
–usage Give a short usage message

commands restart and reload not work ! haven`t ))) and -shutdown not work

Reply

18 Tricky April 10, 2011

@takprosto
Do the commands give an error or are they just not refreshing the DNS? If there’s no error then the DNS entry could be cached elsewhere, for example on your router or your ISP’s DNS server.

Reply

19 m4h September 15, 2011

Manish ->

Change from:
192.168.1.1 admin.domain.com admin
TO:
192.168.1.1 admin admin.domain.com admin

Reply

20 david.chen February 3, 2012

why not just use
nscd -i hosts
nscd -i passwd .
to flush the cache

Reply

Leave a Comment

You can use these HTML tags and attributes for your code and commands: <strong> <em> <ol> <li> <u> <ul> <blockquote> <pre> <a href="" title="">
What is 13 + 14 ?
Please leave these two fields as-is:
IMPORTANT! To be able to proceed, you need to solve the simple math so we know that you are a human and not a script.




Previous post:

Next post: