zaro

How to flushdns in Linux?

Published in DNS Management 2 mins read

To flush the DNS cache in Linux, you typically need to interact with the specific DNS caching service running on your system via the terminal.

Here is a general process based on common Linux configurations, incorporating information from the provided reference:

Steps to Flush DNS Cache

Flushing the DNS cache involves accessing your system's command line interface and running a command specific to the DNS caching service in use.

  1. Open the Terminal: Begin by opening the terminal application. A common shortcut for this is pressing Ctrl + Alt + T simultaneously.
  2. Determine the DNS Service: The exact command depends on the DNS caching service your Linux distribution uses. Many modern distributions, including Ubuntu, use systemd-resolved. Other systems might use services like dnsmasq or nscd.
  3. Execute the Flush Command: Enter the appropriate command for your system.
    • For distributions using systemd-resolved (like Ubuntu): According to the reference, the command is:
      sudo systemd-resolve --flush-caches
    • For other services: Commands will vary. For example:
      • If using nscd: sudo nscd -i hosts
      • If using dnsmasq: sudo systemctl restart dnsmasq (restarting the service often flushes its cache)
      • If using the resolvectl utility (alternative for systemd-resolved): sudo resolvectl flush-caches
    • Note: Use the command relevant to your specific setup. The reference specifically highlights the systemd-resolve command for Ubuntu.
  4. Enter Password (if prompted): If you used sudo, you will likely be prompted to enter your user password to confirm the command execution. Type your password and press Enter. Note that the password won't be displayed as you type for security reasons.

Successfully executing the command clears the local DNS cache maintained by the specified service.

Verifying the Flush

While not always necessary, some services provide a way to check the cache statistics before and after flushing.

  • For systemd-resolved: You can view cache statistics using systemd-resolve --statistics before and after flushing to see the cache size change.

By following these steps, you can effectively clear the local DNS cache on your Linux system, which can help resolve issues related to outdated IP addresses or connectivity problems after DNS record changes.