66 votesby askubuntu.com/users/449written 2014-03-17last updated 2018-08-31
Note that Ubuntu uses systemd-resolve from 17.04 and onwards so this answer doesn't apply anymore to recent Ubuntu versions. See "flush DNS cache in Ubuntu 17.04 and higher (18.04)"
By default, DNS is not cached in Ubuntu < 17.04 (but it might be cached in the network or application)
To confirm one way or the other whether dnsmasq is caching, run ps ax | grep dnsmasq and look at the running command. Here's a breakdown of my default 13.10 machine:
/usr/sbin/dnsmasq \
--no-resolv \
--keep-in-foreground \
--no-hosts \
--bind-interfaces \
--pid-file=/var/run/NetworkManager/dnsmasq.pid \
--listen-address=127.0.1.1 \
--conf-file=/var/run/NetworkManager/dnsmasq.conf \
--cache-size=0 \
--proxy-dnssec \
--enable-dbus=org.freedesktop.NetworkManager.dnsmasq \
--conf-dir=/etc/NetworkManager/dnsmasq.d
/etc/NetworkManager/dnsmasq.d is empty by default. So there aren't any overrides coming in there and just to check --cache-size=0 means what we think it means (instead of an unlimited cache), man dnsmasq shows:
-c, --cache-size=<cachesize>
Set the size of dnsmasq's cache. The default is 150 names.
Setting the cache size to zero disables caching.
So while dnsmasq can cache DNS, it isn't caching out the box. You can check your machine and various configuration directories to check you're on the same page.
If you are seeing cache issues, this is likely happening in one of a few places:
Upstream from your computer. Some routers cache. Many corporate networks will cache DNS. Many ISP-run DNS servers and will use their own caches. The only way to guarantee against a network cache is to use a cache you can manually refresh. This is why I like OpenDNS.
In the client application (notably browsers). Applications can do all sorts of their own caching that Ubuntu has no effect on. How Firefox caches DNS. How to clear Chrome's DNS cache. Other browsers (and applications) might have their own mechanisms.
I'm scraping the barrel here but perhaps you've installed a non-standard DNS server in Ubuntu instead of turning caching on in dnsmasq. There are many: nscd, DJBDNS dnscache (aka TinyDNS), pdns, pdnsd, Bind9 (and its variants), and more I can't even remember. These will probably be evidenced in /etc/resolv.conf (with config in /etc/resolvconf/` to autogen that file). The following shows an locally intercepted DNS query:
$ nslookup askubuntu.com
Server: 127.0.1.1
Address: 127.0.1.1#53
Non-authoritative answer:
Name: askubuntu.com
Address: 198.252.206.24
If you're not hitting 8.8.8.8 (or whatever you expect your DNS server to be), check what you are hitting instead. In my case I can see this is just dnsmasq set up to mirror DNS queries back for LXC, but in your case it might be doing bad cachey things.
If you have done of the listed caches, the process for clearing each varies:
sudo /etc/init.d/nscd reload # nscd
sudo /etc/init.d/named restart # bind9
On a slightly related note, see this to enable caching in dnsmasq.