Clear dns cache in Linux / Unix machine
Most DNS clients will cache the results of name resolution request to speed up multiple lookups to the same URL. Just think about how many requests are made to the same domain when visiting a single web page. Every file, image, style sheet, etc. that is on that page and served from the same domain requires a DNS lookup. This process speeds up name resolution if multiple lookups are done to the same address.
Why we need to clear DNS cache.
So if you have an invalid DNS entry cached on your local client you’ll need to flush it out of the cache so your client can do a new lookup and get the correct information. Or your other option is to wait until that DNS entry expires and the cache flushes it automatically… which typically takes about 24 hours.
Consider another situation, if you updated your DNS values for your domain and because of the DNS cache in your machine/server the DNS resolution is not effecting, here you can clear the DNS cache and suddently it will resolve the new vales.
1.Flush nscd dns cache
Nscd is a daemon that provides a cache for the most common name service requests. The default configuration file, /etc/nscd.conf, determines the behavior of the cache daemon.
Nscd provides caching for accesses of the passwd, group, and hosts databases through standard libc interfaces, such as getpwnam, getpwuid, getgrnam, getgrgid, gethostbyname, and others.
There are two caches for each database: a positive one for items found, and a negative one for items not found. Each cache has a separate TTL (time-to-live) period for its data. Note that the shadow file is specifically not cached. getspnam(3) calls remain uncached as a result. As a result of this behavior there is not possible to change non-nscd user to another non-nscd user via su service when nscd is running.
To clear the nscd cache restart the service with the following command.
service nscd restart
2.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. It loads the contents of /etc/hosts so that local hostnames which do not appear in the global DNS can be resolved and also answers DNS queries for DHCP configured hosts.
The dnsmasq DHCP server supports static address assignments, multiple networks, DHCP-relay and RFC3011 subnet specifiers. It automatically sends a sensible default set of DHCP options, and can be configured to send any desired set of DHCP options, including vendor encapsulated options.
It includes a secure, read-only, TFTP server to allow net/PXE boot of DHCP hosts and also supports BOOTP.
Dnsmasq supports IPv6 for DNS, but not DHCP.
To clear the dnsmasq cache restart the service with the following command.
service dnsmasq restart
3.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:
Above methods willl clear cache from your local machine.