30

How can I clear my DNS cache on my android phone? (Droid3, Gingerbread, rooted to be specific)

Ideally I'm looking for a command to run through a terminal emulator, something similar to

ipconfig /flushdns

On a windows machine.

What's the Linux version of this?

GAThrawn
  • 22,064
  • 9
  • 78
  • 126
Steve Robbins
  • 752
  • 2
  • 7
  • 16
  • Do you need to do this for a system app, third party app or your app? – aleksikallio Sep 01 '11 at 04:02
  • Third party I guess? I need it for when I ping a server via Terminal Emulator. – Steve Robbins Sep 01 '11 at 04:30
  • Please update your title to reflect that you want a CLI-based answer. This is ranking in Google and 99% of people searching this title do not have root or CLI skills. – PJ Brunet Aug 11 '15 at 16:09
  • Any update here? as @PJBrunet said, having root is not the usual case, moreover current phones do not allow to remove battery. I looking for flushing the DSN because doing it fixed my "ip failure" on windows, it is happening the same in my phone, but there is no shell on Android. – Jason Angel Aug 01 '20 at 17:13

7 Answers7

15

Addresses are cached for 600 seconds (10 minutes) by default. Failed lookups are cached for 10 seconds. From everything I've seen, there's nothing built in to flush the cache. This is apparently a reported bug in Android because of the way it stores DNS cache. Clearing the browser cache doesn't touch the DNS, the "hard reset" clears it because it simply times out.

Can we ask the underlying reason for needing to clear the DNS cache? Perhaps there's another solution that's missed because we're not looking at the bigger picture.

user8522
  • 209
  • 2
  • 3
  • 2
    Not sure about the OP, but I have a use case where my home network serves addresses like server.home.mydomain.com, but my ISP resolves *.mydomain.com (for any value of "*" this is not already mapped) to www.mydomain.com if my phone is on 3G/4G, so I'm sometimes unable to access my home network until the cache times out. – TomG Aug 25 '12 at 01:25
  • Looks like the forced 10-minute caching is fixed in JB — see this commit. For older releases the Java cache behavior could be tuned by networkaddress.cache.ttl and networkaddress.cache.negative.ttl in build.prop. – Sergey Vlasov Jun 02 '13 at 18:19
  • Fwiw, there are still some (perhaps different) DNS issues even in 4.3 - tablets on wifi may not get an IP address for names that Linux and Windows machines (on the network that hosts the wifi) can resolve. – Jon Shemitz Feb 25 '14 at 21:50
11

On a typical Linux system the cache is cleared by running /etc/init.d/nscd restart, but at least my ROM doesn't use nscd to cache DNS. You can check if yours does, but I doubt it. I've seen suggestions that clearing the brower cache would clear DNS cache too, but one sure way is to do a hard reboot (shutdown, remove battery for 30s, reattach battery and boot).

aleksikallio
  • 16,250
  • 5
  • 48
  • 73
  • 1
    All of my googling also brings up the "hard reboot" suggestion. – Chahk Sep 01 '11 at 14:03
  • I'm downvoting because the answer requires root and/or assumes shell access which is unrealistic for most Android users. The rest of the answer did not solve my problem. Also I believe Matthew Read should reconsider locking the question because this is absolutely not a "done deal." DNS is extremely complex and there will be more legitimate answers from people without Android accounts here yet, I'm an example of that. FWIW I do have 10+ reputation and still could not answer the question. – PJ Brunet Aug 11 '15 at 15:51
  • @PJBrunet To be accurate, you have 1 rep and 100 rep from association bonus, which does not count since you haven't earned it on this site. See What is a "protected" question? for more details. As for the question, I'll unprotect it for now. – aleksikallio Aug 11 '15 at 15:54
7

The only way to do this in Android is to do a hard reboot. The necessary command-line tools are not normally available, however in my tests a hard reboot has always done the trick for me (Galaxy Nexus, and HTC Desire, various ROMs).

This is a pain, but it is quicker than the 10min cache timeout.

Martin
  • 989
  • 8
  • 16
  • How does one perform this? – Steve Robbins Nov 29 '11 at 05:20
  • 1
  • Hold down the power button
  • Choose "Power off"
  • Allow phone to fully shut down
  • Remove battery and wait 30s
  • Return battery
  • Power phone back on.
  • In my experience removing the battery is not necessary, but some have reported it doesn't work if you miss this step.

    – Martin Nov 29 '11 at 08:27
  • 4
    Removing the battery is nonsense. Reboot works. I suspect that the additional time for that simply allows some external DNS cache to time out. – Matthias Urlichs Sep 27 '16 at 19:52