How to clear DNS cache in Ubuntu?

45

8

I'm having problems related to DNS solving in my Ubuntu 9.10.
Prior complaining to my ISP I would like to refresh my local DNS cache, like in Windows we do ipconfig /flushdns.

How can I do that in Ubuntu?

igorsantos07

Posted 2010-04-26T09:30:54.033

Reputation: 1 208

Answers

54

Øhh, ubuntu doesn't cache DNS per default. So there is nothing to flush! (but maybe your SOHO router does). Installing nscd will make ubuntu cache DNS, afterwhich you can clear your cache - but this is besides the point, when you don't have it installed yet!

user42806

Posted 2010-04-26T09:30:54.033

Reputation: 713

2

@LJ_1102 @ exhuma That would be the Ubuntu's network manager, it listens as a local DNS server, but forwards all requests to whatever is defined. As a side-effect (or by design), this allows you to change DNS servers on the fly from the interface without needing root permissions. See for example this question: https://unix.stackexchange.com/questions/59414/understanding-dns-in-ubuntu-12-04

– Raghd Hamzeh – 2014-10-15T18:19:35.867

3Out of curiosity, I am on 14.04 and running nslookup tells me that the response comes from server 127.0.0.1. But - to my knowledge - I have no DNS caching installed (neither nscd nor dnsmasq). I'm on Kubuntu... could this be a KDE Network Manager thing? – exhuma – 2014-04-30T06:11:48.737

@exhuma same here on ubuntu 14.04 so its not a kubuntu thing. – LJ_1102 – 2014-05-02T23:40:57.267

19

Ubuntu 12.04 onwards uses dnsmasq for a local cache. Sending it a SIGHUP (sudo killall -HUP dnsmasq) will re-read the cache.

NOTES
When it receives a SIGHUP, dnsmasq clears its cache and then re-loads /etc/hosts and /etc/ethers and any file given by --dhcp-hostsfile, --dhcp-optsfile or --addn-hosts. The dhcp lease change script is called for all existing DHCP leases. If --no-poll is set SIGHUP also re-reads /etc/resolv.conf. SIGHUP does NOT re-read the configuration file.

ytjohn

Posted 2010-04-26T09:30:54.033

Reputation: 291

this looks reasonable. although, I have to find another of the problems I used to have (that are related to new sites in the /etc/hosts and Opera) to test this and mark as accepted instead of the other :P – igorsantos07 – 2012-12-04T19:28:10.133

That's fine. I actually came across this looking to clear out my own cached dns entry. Once I found the solution though, I felt I should add my findings here. I don't know at what version Ubuntu started using dnsmasq though. – ytjohn – 2012-12-05T15:01:14.787

1This works and should really be marked as the answer! – HDave – 2013-03-13T20:03:19.237

1If minority edits weren't banned and frowned upon, I would correct “SIGUP” to SIGHUP. – James Haigh – 2014-04-20T23:16:08.320

@Igoru: You can use dig for testing whether the cache really has been cleared. I wanted to clear the cache on one of my OpenWrt routers for which I tried dig @192.168.0.3 nixos.org several times (any existent domain will do). While cached, it would usually have a query time of 1ms, but after each time I issued killall -HUP dnsmasq (as root) on the router I got about 27ms after each time I cleared it. – James Haigh – 2014-04-20T23:50:34.427

13

Try the following:

sudo /etc/init.d/dns-clean start

It works for me.

Christian Molina

Posted 2010-04-26T09:30:54.033

Reputation: 131

1I guess things have changed since this answer, since it looks like this won't work any more in 14.04. Hey ubuntu, it would be nice if there were an interface for this that weren't tied up with specifics of particular name resolution systems, and OS versions (and maybe OS distributions later...). – Croad Langshan – 2015-04-05T10:48:40.433

Does work in 15.04 – Milan Simek – 2015-10-10T14:25:06.193

Worked in ubuntu 14.04.5. This script is provided by pppconfig package. I think it isn't included by default in the distribution. – ssoto – 2016-08-16T08:14:45.603

1

I just had this problem with a site for which I changed the DNS, and googling for a solution I found this page.

In my experience Firefox sometimes keeps its own DNS cache and doesn't clear it in time. So if you're viewing a site in Firefox, and keep seeing the old site, trying Chrome might solve the problem. If that works, you can clear the cache in Firefox. This happened in Ubuntu, I don't know if this happens on Windows or OSX as well.

I don't have nscd installed. I noticed however that in the terminal the new DNS is found several minutes later, so the problem solved itself.

SPRBRN

Posted 2010-04-26T09:30:54.033

Reputation: 5 185

1

Depending on the context of your question you might need to clear the cache of your web browser too.

In Firefox this is done by: preferences -> privacy -> clear your recent history [just tick the "cache" checkbox there]

In Chrome: Navigate to chrome://net-internals/#dns and press the "Clear host cache" button. (see another answer)

Tapper

Posted 2010-04-26T09:30:54.033

Reputation: 111