How To Flush Linux / UNIX DNS Cache

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

Under MS-Windows you can use the ipconfig command to flush dns cache. However, Linux and UNIX provides various ways to flush cache. Linux can run nscd or BIND or dnsmasq as the name service caching daemon. Large and work-group servers may use BIND or dnsmasq as a dedicated caching server to speed up queries.

HowTo: 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
# service nscd restart
# service nscd reload
This daemon provides a cache for the most common name service requests. The default configuration file, /etc/nscd.conf, determines the behavior of the cache daemon.

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 the dnsmasq service to flush out dns cache:
$ sudo /etc/init.d/dnsmasq restart
# service 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
You can also use rndc command as follows flush out all cache:
# rndc restart
# rndc exec
BIND v9.3.0 and above will support flushing all of the records attached to a particular domain name with rndc flushname command. In this example flush all records releated to domain:
# rndc flushname
It is also possible to flush out BIND views. For example, lan and wan views can be flushed using the following command:
# rndc flush lan
# rndc flush wan

A note about Mac OS X Unix users

Type the following command as root user:
# dscacheutil -flushcache
$ sudo dscacheutil -flushcache
If you are using OS X 10.5 or earlier try the following command:
lookupd -flushcache

A note about /etc/hosts file

/etc/hosts act as the static table lookup for hostnames. You need to remove and/or update records as per your requirements under Unix like operating systems:
# vi /etc/hosts
Sample outputs:	localhost	wks01.WAG160N	wks01

# The following lines are desirable for IPv6 capable hosts
::1     ip6-localhost ip6-loopback
fe00::0 ip6-localnet
ff00::0 ip6-mcastprefix
ff02::1 ip6-allnodes
ff02::2 ip6-allrouters     build	nas01	nas02	nas03

# nfs2 
# nfs1 nfs1 nfs2	vm01

See also:

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

🐧 Get the latest tutorials on Linux, Open Source & DevOps via RSS feed or Weekly email newsletter.

🐧 35 comments so far... add one

CategoryList of Unix and Linux commands
Disk space analyzersdf duf ncdu pydf
File Managementcat cp mkdir tree
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Modern utilitiesbat exa
Network UtilitiesNetHogs dig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg glances gtop jobs killall kill pidof pstree pwdx time vtop
Searchingag grep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
35 comments… add one
  • Apr 18, 2008 @ 8:32

    it dont work for me i have ubunu 7.10

  • kazoolist Jul 10, 2008 @ 22:54

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

    • Subhash Jul 27, 2010 @ 17:26

      That is correct!

    • Lucas Dec 22, 2014 @ 9:06

      Correct! Thx

  • James Mills Sep 19, 2008 @ 8:32

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

    rndc flush

    • Lost Eagle Jan 31, 2011 @ 16:01

      Thank U

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

      Have a nice day

  • RudyD Feb 4, 2009 @ 12:35


    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?


  • Tricky Apr 26, 2009 @ 23:50

    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.

  • takizo Jul 26, 2010 @ 14:58

    it could be nscd is not install?

    @James, rndc reload works too :D

  • dejf Aug 31, 2010 @ 15:04

    “/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…

  • Yogesh Sep 2, 2010 @ 12:08

    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]

  • Tricky Sep 2, 2010 @ 14:14

    @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:

  • Yogesh Sep 2, 2010 @ 15:32

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

  • Tricky Sep 3, 2010 @ 4:48

    @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) – – skip to the sections after the label “Flush Mozilla Firefox’s internal DNS cache”.

  • Manish Dec 4, 2010 @ 18:49


    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 admin

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

    Any suggestions would be appreciated.

    • Tricky Dec 6, 2010 @ 3:49

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

  • nobbiecentos Feb 8, 2011 @ 13:15

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

  • takprosto Apr 9, 2011 @ 23:45

    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

  • Tricky Apr 10, 2011 @ 11:58

    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.

  • andre Jul 22, 2011 @ 7:18

    on Debian “Squeeze/wheezy”:

    Usage: /etc/init.d/nscd {start|stop|force-reload|restart|status}

  • m4h Sep 15, 2011 @ 8:10

    Manish ->

    Change from: admin
    TO: admin admin

  • david.chen Feb 3, 2012 @ 2:45

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

  • Tim Mar 21, 2012 @ 13:23

    The articles recomendations are distribution specific and depend on how the init scripts are set up and what they do. The proper way to flush the cache on nscd is “nscd -i hosts” and for named, “rndc flush”

    Anything else, like “/etc/init.d/nscd reload” depends on how your distro wrote the init script. Just because that works for you on Fedora does not mean Debian or someone else set it up that way as well.

  • Som Jun 13, 2012 @ 20:44

    If a user wants to flush DNS cache and if the user uses Internet via LAN connection ,the best way to do so is changing the DNS address manually . He just needs to alter the DNS entry .Setting alternative DNS address as primary address will solve the issue instantly. This trick worked for me . I faced the same problem . I am aLinux user .so , I installed NSCD client and tried to flush the cache . But , it could not solve my issue .i solved it myself just changing the DNS entry . It worked for me instantly .I kept the new entry for two days without any problem and later reinstated the older entry

  • nick Sep 6, 2012 @ 21:57

    This post is a little misleading. Linux by default does not have a caching system (at least not most of the destributions). So unless you installed a caching system (or your system came with one pre-installed), there is no need to flush DNS because your system doesn’t store any.

    This post isn’t clear about that and will cause people (who have no caching in place) to install a caching package (like nscd) just so that they could flush it. That’s a little counterproductive I think.

    • Luke Jun 2, 2014 @ 5:09

      No wonder it didn’t work, Ubuntu 14.04 :)

      I guess it must be router? Though I’d think etc/host would be checked before that?

      • Tricky Jun 3, 2014 @ 10:43

        Routers can host a DNS cache – all upstream DNS servers (your ISP’s DNS servers for example) will usually also do caching – so even if you flush your router’s cache that may not resolve whatever issue you’re having.

        In general, /etc/hosts overrides everything. There’s probably some general exception somewhere. :)

        The one scenario I know of where a hosts entry gets “overriden” is with a Squid cache server. For example, if you set up a hosts entry on your computer but the HTTP traffic is intercepted by a Squid server, the Squid server may do its own DNS lookup and completely ignore the fact that your request was directed at a different IP address.

  • Mac Jan 11, 2013 @ 15:20

    *NOTE* – The mistake I have often made is my /etc/hosts (on my CentOS6 workstation) I always FORGET to update it.

    So after a DNS update, I cannot reach the site by name and think something is amiss, well I forgot to edit my /etc/hosts file for all of the servers.

  • Willian Braga da Silva Feb 1, 2013 @ 13:44

    The correct way to flush NSCD cache is using the command line:

    sudo /usr/sbin/nscd –invalidate=hosts

    from the man page:

    $ /usr/sbin/nscd –help
    Usage: nscd [OPTION…]
    Name Service Cache Daemon.

    -d, –debug Do not fork and display messages on the current
    -f, –config-file=NAME Read configuration data from NAME
    -g, –statistic Print current configuration statistics
    -i, –invalidate=TABLE Invalidate the specified cache
    -K, –shutdown Shut the server down
    -t, –nthreads=NUMBER Start NUMBER threads
    -?, –help Give this help list
    –usage Give a short usage message
    -V, –version Print program version

    $ sudo /usr/sbin/nscd -g
    nscd configuration:
    hosts cache:

    yes cache is enabled
    yes cache is persistent
    yes cache is shared
    yes check /etc/hosts for changes

    hope this help

  • Jesin Jun 28, 2013 @ 8:29

    Does restarting the networking service clear the cache ?

    I do not find all Linux distributions having the one of the three tools you’ve mentioned above.

  • Mac Nov 16, 2013 @ 14:02

    -> nscd is there any reason why you would use this service, just wanting to help understand what it does?


  • samuel Mar 26, 2014 @ 7:03

    I suggest changing this post about flush dns cache since it’s not correct to reload or restart nscd.

    I read the man and found the correct way is use:

    nscd -i hosts

    This method was mentioned in several comments, but if your post were not updated, more and more users would be misguided.

  • Katta Aug 27, 2014 @ 13:19

    for dnsmasq you can simply send HUP signal to process:

    killall -HUP dnsmasq

  • amir Jun 10, 2015 @ 14:46


    [root@webNG-int01 ~]# service nscd restart
    nscd: unrecognized service

  • Patrick C. Nov 5, 2015 @ 16:39

    I suggest adding a method for “unbound” as well.


    service unbound restart

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre> for code samples. Still have questions? Post it on our forum