If you recently migrated a website to a new server or changed the IP address of a local network device, your Ubuntu machine might stubbornly refuse to connect to the new location. This happens because Ubuntu caches (saves) DNS records locally to speed up web browsing. To force your computer to fetch the brand-new IP address, you must manually flush the DNS cache.
How to Flush the Cache
Modern versions of Ubuntu (version 18.04 and newer) use systemd-resolved as the default service for managing DNS. You can clear its cache instantly using the terminal.
- Open your terminal application.
- Run the following command to flush the cache:
sudo resolvectl flush-caches
(Note: If you are using an older version of Ubuntu, the command issudo systemd-resolve --flush-caches). - The command will execute silently without outputting a confirmation message.
How to Verify the Cache Was Cleared
Because the flush command is silent, it is helpful to verify that the cache size actually dropped back down to zero.
- Run the following command to view the DNS statistics:
resolvectl statistics - Look for the section labelled Cache in the output.
- Check the number next to Current Cache Size.
If the flush was successful, the Current Cache Size will report as 0. Your Ubuntu system will now query your external DNS servers (like Google DNS or your ISP) the next time you attempt to access a domain name, ensuring you receive the most up-to-date IP routing information.