322

I just updated the DNS record (ns1, ns2, ns3.myhostingcompany.com) for a site I've got hosted, but I still get the domain registrar parking page.

I'd like to see if the problem is Ubuntu's cached DNS records. Is there a way to clear Ubuntu's DNS cache? (if such a thing exists?)

2
  • 1
    Also, check /etc/hosts. I've just been sure that the old IP address of my domain was being cached, but only strace ping example.com revealed that I forgot to remove the /etc/hosts record which I added a time ago because of lacking patience for DNS propagation.
    – ulidtko
    Jul 13, 2014 at 12:42
  • 2
    a lot of these answers suggest caching is disabled by default, but they also refer older versions. It certainly appears to be on by default in my machine (18.04) and various answers below do show you how to flush it, just scroll down
    – Madivad
    Aug 27, 2018 at 5:20

17 Answers 17

246

Ubuntu 17.04 and higher (18.04)

From Ubuntu 17.04 and onwards, systemd-resolve is used for DNS. You can flush systemd's caches like so:

sudo systemd-resolve --flush-caches

Ubuntu 22.04 and higher

sudo resolvectl flush-caches
13
  • 18
    OMG, will you marry me? Solved my issue with 18.04 too \o/
    – NiKo
    May 8, 2018 at 17:37
  • 10
    This didn't work for me on 16.04 LTS - but it was useful to see yet another way it might have been cacheing: sudo systemd-resolve --statistics
    – Phil
    May 10, 2018 at 8:24
  • 15
    For me, systemctl restart systemd-resolved.service did also work on 18.04. May 8, 2019 at 9:04
  • 4
    Any idea why it displays this error on Ubuntu 19.10? Failed to flush caches: Unit dbus-org.freedesktop.resolve1.service not found.
    – baptx
    Mar 16, 2020 at 11:53
  • 18
    For ubuntu 22.04, we need to use: sudo resolvectl flush-caches. See answer from @codezalot in systemd-resolve-command-not-found-in-ubuntu-22-04-desktop.
    – noel aye
    Oct 3, 2022 at 15:36
85

For 18.04 and higher

Look at Mike Shultz' answer.

For 11.10 and below

Ubuntu doesn't cache dns records by default so unless you've installed a dns cache there isn't anything to clear.

DNS records are likely cached by your provider's DNS servers so if you want to check if the DNS changes you made were successful you can interrogate a DNS server from your domain hosting service with dig:

dig -t a ns1.myhostingcompany.com @domain_registrar_dns_server

It you want Ubuntu to start caching dns I recommend installing pdnsd together with resolvconf. nscd is buggy and not advisable.

4
  • 14
    Any references as to why nscd is buggy? Is it still buggy today (2012-10)?
    – jjmontes
    Sep 26, 2012 at 12:13
  • 4
    Can you update your answer- ATM it sounds like Ubuntu versions 11.10+ do cache DNS records. Feb 8, 2015 at 3:08
  • Mike Shultz's answer definitely doesn't work on Ubuntu 20. You get this error message: sd_bus_open_system: No such file or directory Dec 27, 2021 at 21:32
  • So, For 10 years later. If I disabled systemd-resolve for ubuntu and debian. Then there is no DNS cache, Each nslookup will request DNS server?
    – Alceatraz
    Apr 3, 2023 at 0:11
71

12.04

Ubuntu 12.04 uses dnsmasq which is built into network-manager, but it doesn't cache dns so there is no need to flush it. Here is a sample line from my syslog to prove that point:

dnsmasq[2980]: started, version 2.59 cache disabled

There is also no need for any configuration of dnsmasq. If you are running with stock settings it won't be caching dns, as for it to do so you have to explicitly set it up as this Ubuntu article describes.

If you wanted to refresh your settings you could disable and then enable networking or run

sudo service network-manager restart

This restarts dnsmasq because it is built in to network-manager; check your syslog for the evidence for this.

If you are using a wired connection with dhcp network manager will be taking the settings direct from your router and your connection will be automatically established when you login to Ubuntu. You could check that the settings are correct in your router if you can access it via the web interface, and perhaps reboot it if necessary. If it is a general problem with dns, you could try using Google dns instead of your isp dns, and more information on that is detailed here.

1
  • sudo service network-manager restart did the trick for me with Debian Oct 28, 2014 at 9:01
66
+50

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.

11
  • 1
    My output from ps aux|grep is the same as yours, yet something definitely IS caching DNSs on my machine. If it's not dnsmasq it's something else. Proof: I created a subdomain on my server, yet I could not ping it, host unreachable. I checked at downforeveryoneorjustme.com and it was up, so propagation issue discarded. Not my router, nor my ISP, because I use Google's DNS's 8.8.8.8. Then I rebooted the computer (and NOT the router) and I could reach the domain. I had been retrying a dozen times before reboot with no luck. First try after reboot reached the host.
    – matteo
    Mar 17, 2014 at 18:00
  • And this was not the first time I experienced the same thing, by the way. Months ago the same happened and the only way I could reach the domain was restarting the computer, but then I had an older Ubuntu version.
    – matteo
    Mar 17, 2014 at 18:01
  • @matteo The browser?
    – Oli
    Mar 17, 2014 at 19:00
  • no, as I said I tested with ping, not (only) the browser(s).
    – matteo
    Mar 17, 2014 at 23:17
  • @matteo I've added more but I'm running out of ideas. There just aren't that many more places somebody could cache DNS :)
    – Oli
    Mar 18, 2014 at 0:18
40

For 12.04:

Ubuntu 12.04 does cache DNS using dnsmasq ( see man dnsmasq). Use the following to clear the cache:

sudo kill -HUP $(pgrep dnsmasq)
6
  • Thank you! You are the first I've seen mention anything about 12.04's default caching!
    – Tarka
    Sep 13, 2012 at 21:22
  • 6
    12.04 does not cache dns by default- check your syslog after restarting network-manager; it will have an entry that shows dnsmasq is starting with cache-disabled.
    – user76204
    Sep 14, 2012 at 21:02
  • 1
    I don't know whether the cache is enabled by default, but this answer worked for me.
    – jeyk
    Jan 7, 2014 at 23:45
  • Why not just sudo killall -HUP dnsmasq? Apr 20, 2014 at 22:13
  • 1
    use pkill instead of kill and pgrep Sep 9, 2014 at 20:45
18

For ubuntu 22.04:

sudo resolvectl flush-caches

17

sudo /etc/init.d/nscd restart

http://www.ubuntugeek.com/howto-clearflush-dns-cache-in-ubuntu.html

Also as a note you can check and see if your DNS changes have propagated using dig and looking up against someone else other than your default DNS servers. In this case google DNS.

dig @8.8.8.8 example.com

1
  • 12
    Worth noting that nscd isn't installed by default.
    – Scaine
    Jan 18, 2011 at 20:05
11

Personally, I'd use OpenDNS and use their Cache Check function to force a refresh just to make sure the changes work but you can't guarantee they'll refresh for your users within 48 hours.

DNS is a slow beast. Patience will keep you sane.

4
  • +1 I use OpenDNS as well clear cache is very useful. Aug 13, 2010 at 21:38
  • I'm suspicious of OpenDNS. I was happy with it at first, but then I started getting suspicious; though I guess if it's a choice between ISP and OpenDNS, I'm better off with OpenDNS, right?
    – Jono
    Aug 15, 2010 at 20:23
  • 2
    I'd say OpenDNS is the lesser evil. They'd like to monetise your traffic but only on bad domain hits.
    – Oli
    Aug 15, 2010 at 22:35
  • @Oli The link is down. If you still use it could you please update the links?
    – Shayan
    Sep 1, 2019 at 12:46
7

If you are using nscd:

sudo /etc/init.d/nscd restart

It's worth mentioning that it might not be the OS that is caching it. Everyone likes to cache DNS... Some tests:

Check to see if it's the new or old IP. Most browsers cache DNS as well, so if you haven't restarted Chromium or whatever you might not be seeing the latest.

ping yourdomain.com

Switch your local nameserver in the /etc/resolv.conf to another provider, google or level , examples:

nameserver 8.8.8.8
nameserver 4.2.2.2

And then ping again.

Check to make sure your router isn't caching DNS in any form. (Varies by router/firmware/etc)

Finally, patience. DNS can take a bit of time to propagate throughout the internet.

3
  • 2
    If using NetworkManager and DHCP /etc/resolve.conf will get flushed when the DHCP lease expires, so you will have to set a static ip in NetworkManager to get this to work over longer time. Aug 13, 2010 at 21:53
  • +1 I didn't realize Firefox was caching DNS, this helped greatly.
    – wavemode
    Dec 22, 2014 at 18:57
  • +1 for switching local nameserver to google worked Jan 5, 2017 at 7:37
5

All the answers above forgot one important thing in the name resolution : generally the DNS servers you request the name resolution to is not the one holding the records themselves (the authoritative server). As each DNS record comes with a Time To Live value which will oblige each DNS server in the resolution chain to do caching during the amount of seconds mentioned by this value. So not only you can cache in your machine, but CERTAINLY the result of the name lookup will be cached somewhere on a server you don't control.

The only solution to be immediately notified of a name record change is to use a TTL value of 0 when creating / updating the entry in the authoritative name server. But this means that for each name resolution, the server will be hit, usually this is not allowed by the registrars. For instance, they can provide a list of pre-defined TTL values you can choose in.

I manage different domain names and to be sure that the change are well applied in the authoritative name servers, I'm using a tool called dnstracer that can show the lookup result on each servers from the DNS root.

In conclusion, even without any DNS caching solution in place, there will still be a delay between the moment you change the DNS records and the change is seen on a PC. This delay greatly depends on the TTL of the records and on the number of DNS servers between you PC and the authoritative name server.

1
  • 1
    Of course, but if that was the issue, rebooting the computer wouldn't fix it.
    – matteo
    Mar 18, 2014 at 14:23
5

Ping use another DNS

Seems that Ubuntu 20.10 has two (2) DNS caches:

  1. systemd-resolve
  2. nscd

Flushing the DNS cache with the following command didn't work for me:

sudo systemd-resolve --flush-caches

After disable/enable the network and the wifi connection, or rebooting the system, the problem was still there:

  • nslookup, dig and other commands works
  • ping doesn't work

All the system configuration was ok, but some addresses where like fixed somewhere.

After searching on different sites I found the problem: there was another DNS cache and ping seems to use that.

To clear the second DNS cache just run:

sudo nscd -i hosts

Flush BOTH DNS caches

You can do it alltoghether, just copy and paste on a terminal:

sudo systemd-resolve --flush-caches
sudo nscd -i hosts

Checking DNS cleared

Not sure if this works? Just take a look:

sudo systemd-resolve --statistics
sudo nscd -g
1
  • ubuntu dns resolution: confusing users since 2007 ;-) (afair)
    – morgwai
    Jan 27, 2022 at 17:39
2

I used the following command to flush the dns cache on my 12.10 ubuntu box and it worked fantastic.

sudo kill -HUP $(pgrep dnsmasq)

Another helpful signal is the SIGUSR1 which dump a little statistic to syslog or as it is note from man dnsmasq:

In --no-daemon mode or when full logging is enabled (-q), a complete dump of the contents of the cache is made.

1
  • use pkill instead of kill and pgrep Sep 9, 2014 at 20:44
2

For ubuntu 14.04, I recommand this command :

sudo service dns-clean
1
  • Not on the default server installs. Only works on 14.04 and below.
    – R J
    Aug 31, 2018 at 13:58
1

On Ubuntu 16.04, I couldn't get the cache to clear unless I did:

sudo service dnsmasq restart
2
  • just broke my DNS completly DNS_PROBE_FINISHED_BAD_CONFIG can't visit any website. Restarting my computer fixed it though and dns is refreshed. May 1, 2020 at 17:03
  • @PhilipRego a non-sequitir. A bad DNS config has nothing to do with a restarted DNS service being the cause. It has to do with a bad DNS config. Restarting your computer may have fixed the issue -- but there might be something else at play, such as your DNS may have already been loaded once and was jammed, file corruption on a process file, or some other factor.
    – Volomike
    May 1, 2020 at 17:26
1

Or even

If using bind9 based utiliites:

rndc flush

or, depending on your infrastructure

sudo rndc flush

But in your host and/or your local domain name server...

0

I also found contradictions, but this: https://superuser.com/a/521562 woks for me (Ubuntu 13.10 with latest updates, no special network packages installed).
In short, just use this
sudo /etc/init.d/dns-clean

0

For those using Ubuntu via Windows Subsystem for Linux (WSL2), it's useful to know that DNS caching is handled on the Windows (host) side.

Thus, the cache can be cleared with: ipconfig.exe /flushdns

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .