How to Clear DNS Cache on Linux/Unix/Mac

I use a dial-up connection on Linux, and frequent disconnections cause DNS issues. How to clear DNS cache using shell commands on various Linux/Unix distributions?

On MS-Windows, you can use theipconfig command to clear DNS cache. However, Linux and Unix provide different methods. Linux can run nscd, BIND, or dnsmasq as name service caching daemons. Large or workgroup servers may use BIND or dnsmasq as dedicated caching servers.

How To: Clear nscd DNS Cache

Nscd caches name service requests from libc. If NSS data retrieval is slow, nscd can significantly speed up repeated access and improve system performance. Restart nscd to clear the cache:


  1. $ sudo /etc/init.d/nscd restart

or


  1. # service nscd restart

or


  1. # service nscd reload

This daemon caches the most common name service requests. /etc/nscd.conf controls its behavior.

Clear dnsmasq DNS Cache

dnsmasq is a lightweight DNS, TFTP, and DHCP server designed for LANs. It accepts DNS queries and answers from a local cache or forwards to a recursive DNS server. It is also installed on many cheap routers to cache DNS queries. Just restart the dnsmasq service to clear the DNS cache:


  1. $ sudo /etc/init.d/dnsmasq restart

or


  1. # service dnsmasq restart

Clear BIND Cache Server DNS Cache

A BIND caching server gets information by responding to queries from another server (zone master) and caches data locally. Restart BIND to clear its cache:


  1. # /etc/init.d/named restart

You can also use the rndc command to clear all cache:


  1. # rndc restart

or


  1. # rndc exec

BIND v9.3.0+ supports clearing cache for a specific domain: rndc flushname. This example flushes cyberciti.biz records:


  1. # rndc flushname cyberciti.biz

You can also clear BIND Views. For example, clear LAN and WAN Views:


  1. # rndc flush lan
  2. # rndc flush wan

Tip for Mac OS X Unix Users

Run the following command as root on Mac:


  1. # dscacheutil -flushcache

or


  1. $ sudo dscacheutil flushcache

If using OS X 10.5 or earlier, try:


  1. lookupd flushcache

A Tip about /etc/hosts

/etc/hosts serves as a static lookup table. Modify it as needed on Unix-like systems:


  1. # vi /etc/hosts

Sample output:


  1. 127.0.0.1 localhost
  2. 127.0.1.1 wks01.WAG160N wks01
  3. # The following lines are desirable for IPv6 capable hosts
  4. ::1 ip6localhost ip6loopback
  5. fe00::0 ip6localnet
  6. ff00::0 ip6mcastprefix
  7. ff02::1 ip6allnodes
  8. ff02::2 ip6allrouters
  9. 10.37.34.2 build
  10. 192.168.1.10 nas01
  11. 192.168.1.11 nas02
  12. 192.168.1.12 nas03
  13. #192.168.2.50 nfs2.nixcraft.net.in nfs2
  14. #192.168.2.51 nfs1.nixcraft.net.in nfs1
  15. 172.168.232.50 nfs1.nixcraft.net.in nfs1
  16. 172.168.232.51 nfs2.nixcraft.net.in nfs2
  17. 192.168.1.101 vm01

References

Related: Using ipconfig to clear DNS cache on Windows Vista/XPClear DNS Cache


via: http://www.cyberciti.biz/faq/rhel-debian-ubuntu-flush-clear-dns-cache/

Leave a Comment

Your email address will not be published.