How to Clear the DNS Cache in Ubuntu Linux

When you attempt to connect to a website like digitash.com, your Linux machine queries a Domain Name System (DNS) server to translate that human-readable URL into a machine-readable IP address. To speed up future connections, the operating system aggressively caches (saves) that IP address locally.

However, if a website recently migrated to a new server with a new IP address, your Linux system might stubbornly try to connect to the old, cached IP address, resulting in a connection timeout error. To force your machine to fetch the updated DNS records, you must flush the local cache.

How to Flush the DNS Cache via systemd-resolved

Modern Linux distributions, including Ubuntu 20.04 and newer, use the systemd-resolved daemon to manage local DNS resolution and caching.

  1. Open your terminal.
  2. Execute the following command (you will need root privileges):
    sudo systemd-resolve --flush-caches
  3. Press Enter and provide your sudo password if prompted.

The terminal will not output a confirmation message. It simply returns to a blank prompt, but the cache has been successfully wiped.

How to Verify the Cache Was Cleared

If you want mathematical proof that the cache was successfully deleted, you can view the active statistics of the DNS daemon.

  1. Type the following command:
    sudo systemd-resolve --statistics
  2. Press Enter.

The terminal will print a block of text detailing cache operations. Look specifically for the line labeled Current Cache Size. Immediately following a flush command, this number should read 0.

Alternative Command for Ubuntu 22.04+

In very recent versions of Ubuntu (22.04 and newer), the networking commands have been slightly reorganized. The older systemd-resolve command is technically deprecated and replaced by resolvectl. While the older command still works via a symlink, the modern, future-proof method to clear the cache is:

sudo resolvectl flush-caches

This achieves the exact same result, instantly forcing your Linux machine to query the upstream DNS server for fresh IP addresses the next time you open a web browser or run a network script.

Get the best tech tips delivered straight to your inbox.

Join thousands of readers mastering Apple, Google, Microsoft, and Linux.