Home Beginner's Guide How to refresh your DNS cache on Linux systems

How to refresh your DNS cache on Linux systems

Flushing the DNS cache in Linux can resolve a host of network issues. This guide provides simple instructions for clearing the DNS cache on various Linux distributions, helping to refresh your network settings and solve connectivity problems quickly and efficiently.

by John Horan
clearing dns cache in linux

Today, we delve into an essential, yet often overlooked aspect of network management: flushing the DNS cache. For both newcomers and seasoned veterans of Linux, understanding how to perform this simple task can be a game-changer. Whether you’re troubleshooting pesky network issues, ensuring privacy, or just keeping your system running smoothly, knowing how to clear your DNS cache is a valuable skill.

In this guide, we’ll walk through the steps to flush the DNS cache across various Linux distributions, offering insights and tips to make the process as seamless as possible. So, let’s roll up our sleeves and dive into the nitty-gritty of DNS management in Linux!

Understanding DNS and its importance

Before we jump into the how-to, let’s take a quick moment to understand what DNS is and why it matters. DNS stands for Domain Name System, and it’s essentially the phonebook of the internet. When you type a website address into your browser, DNS translates that address into the IP address that computers understand. Sometimes, this information is stored (cached) to speed up future requests, but this can lead to outdated or incorrect information being used. That’s where flushing the DNS cache comes in.

When to flush DNS

Flushing your DNS cache can be particularly helpful in several scenarios:

  1. After changing DNS settings: If you’ve switched to a new DNS server, flushing the cache ensures your system uses the latest settings.
  2. Troubleshooting network issues: Sometimes, flushing the DNS can resolve connectivity problems.
  3. Security reasons: If you’re concerned about privacy or security, regularly flushing your DNS cache can be a good practice.

How to flush DNS on Linux

Now, let’s dive into the main event. Flushing the DNS cache in Linux can vary slightly depending on your system and the DNS service you’re using. Here are methods for some of the most common scenarios:

Flushing DNS cache with systemd-resolved

Systemd-resolved is a common service used in many modern Linux distributions. Here’s how to clear its DNS cache:

  1. Open your terminal.
  2. Type the following command and press Enter:
    sudo systemd-resolve --flush-caches
    
  3. You can verify the cache is cleared by running:
    sudo systemd-resolve --statistics
    

    You should see that the cache size is zero.

    $ sudo systemd-resolve --statistics
    DNSSEC supported by current servers: no
    
    Transactions
    Current Transactions: 0
    Total Transactions: 12345
    
    Cache
    Current Cache Size: 0 # <--- Indicates the cache is cleared
    Cache Hits: 6789
    Cache Misses: 9876
    
    DNSSEC Verdicts
    Secure: 0
    Insecure: 0
    Bogus: 0
    Indeterminate: 0

In this output, under the “Cache” section, you’ll notice the “Current Cache Size” is shown as 0. This indicates that the DNS cache has been successfully flushed. The other statistics, like “Cache Hits” and “Cache Misses,” provide additional information about the cache’s performance before it was flushed, but the key detail after flushing is that the current cache size is zero.

Flushing DNS cache for NetworkManager

If you’re using NetworkManager, the process is just as straightforward:

  1. Open the terminal.
  2. Run this command:
    sudo nmcli general reload
    

    This command will reload NetworkManager, which also clears the DNS cache.

    After executing this command, there typically isn’t a detailed output or confirmation message directly indicating that the DNS cache has been cleared. NetworkManager silently processes this command. The primary function of this command is to reload NetworkManager’s configuration, and as a part of this process, it clears the DNS cache.

    To verify that the DNS settings have been reloaded, you can check the status of the NetworkManager service or try accessing a website to ensure your network connectivity is functioning as expected. If there were DNS-related issues that the cache clearing was meant to solve, you should see an improvement or resolution of those issues.

Flushing DNS cache for dnsmasq

For those using dnsmasq, follow these steps:

  • Open your terminal.
  • Restart the dnsmasq service with:
    sudo systemctl restart dnsmasq.service
    

    This will clear the cache as the service restarts. In most cases, this command won’t produce any output if it executes successfully. The absence of output is common in Unix and Linux systems and usually indicates that the command completed without any errors.

    If you want to confirm that the dnsmasq service has restarted and is running correctly, you can use the following command:

    $ sudo systemctl status dnsmasq.service
    

    This command will provide you with the current status of the dnsmasq service. Here’s an example of the output you might see:

    dnsmasq.service - dnsmasq - A lightweight DHCP and caching DNS server
         Loaded: loaded (/lib/systemd/system/dnsmasq.service; enabled; vendor preset: enabled)
         Active: active (running) since Tue 2023-11-26 10:00:00 UTC; 10s ago
       Main PID: 12345 (dnsmasq)
          Tasks: 1 (limit: 4657)
         Memory: 1.2M
         CGroup: /system.slice/dnsmasq.service
                 └─12345 /usr/sbin/dnsmasq -k --enable-dbus --user=dnsmasq --group=dnsmasq
    
    Nov 26 10:00:00 hostname systemd[1]: Starting dnsmasq - A lightweight DHCP and caching DNS server...
    Nov 26 10:00:00 hostname dnsmasq[12345]: started, version 2.85 cachesize 150
    Nov 26 10:00:00 hostname dnsmasq[12345]: compile time options: IPv6 GNU-getopt DBus no-UBus no-i18n IDN DHCP DHCPv6 no-Lua TFTP conntrack ipset auth cryptohash DNSSEC loop-detect inotify dumpfile
    Nov 26 10:00:00 hostname dnsmasq[12345]: DBus support enabled: connected to system DBus
    Nov 26 10:00:00 hostname dnsmasq-dhcp[12345]: DHCP, IP range 192.168.0.50 -- 192.168.0.150, lease time 1h
    Nov 26 10:00:00 hostname dnsmasq[12345]: reading /etc/resolv.conf
    Nov 26 10:00:00 hostname dnsmasq[12345]: using nameserver 192.168.0.1#53
    Nov 26 10:00:00 hostname dnsmasq[12345]: read /etc/hosts - 7 addresses
    Nov 26 10:00:00 hostname systemd[1]: Started dnsmasq - A lightweight DHCP and caching DNS server.
    

    This output indicates that dnsmasq is active and running, which implies that the cache has been cleared and the service has restarted successfully.

Checking your work

After you’ve flushed your DNS cache, you might want to check if it worked. While there’s no direct command to view the DNS cache contents, you can confirm by revisiting a website you had issues with or monitoring network performance.

Will the above steps work on all Linux distros?

A question I often hear is, “Will these DNS flushing methods work on all Linux distributions?” The answer is both yes and no, and let me explain why.

The variability of Linux distros

Linux is known for its diversity, with countless distributions (distros) tailored for different needs. Each distro might handle network management and DNS caching differently based on:

  1. The Network manager: While many distros use NetworkManager or systemd-resolved, some might use different tools or configurations.
  2. Version differences: Different versions of the same distro might have varied ways of managing DNS.
  3. Custom configurations: Some users or system administrators customize their systems, which can change how DNS caching works.

Common ground

Despite these differences, the methods I’ve described are broadly applicable, especially for popular distros like Ubuntu, Fedora, Debian, and their derivatives. These distros typically use systemd-resolved or NetworkManager, making the provided commands quite universal.

Frequently Asked Questions (FAQs) about flushing DNS on Linux

What is DNS caching and why is it important?

DNS caching is the process of storing the IP addresses of web servers (websites) that you have recently visited. This is done to speed up the process of retrieving the IP address the next time you visit the same website, leading to faster loading times.

How often should I flush my DNS cache?

It’s typically not necessary to flush your DNS cache regularly unless you’re experiencing specific network issues, like accessing a recently moved website or resolving DNS-related errors. For general users, occasional flushing is sufficient.

Will flushing DNS affect my browsing history?

No, flushing your DNS cache won’t affect your browsing history. Your history is stored by your web browser, whereas the DNS cache is maintained by your operating system.

Can flushing DNS improve internet speed?

Flushing DNS won’t inherently improve your overall internet speed, but it can resolve specific issues causing delays in accessing certain websites. It ensures that your system queries the most current and correct IP addresses.

Is it safe to flush DNS?

Absolutely, it’s completely safe. Flushing your DNS cache is a standard networking practice and doesn’t pose any risk to your computer or network.

How do I flush DNS on other operating systems?

For Windows, you typically use ipconfig /flushdns in the Command Prompt. On macOS, the command varies by version but often starts with sudo killall -HUP mDNSResponder.

Do I need administrative privileges to flush DNS?

Yes, in most cases, you need to have administrative privileges because you are modifying network settings. This is why you use sudo in the commands.

What should I do if flushing DNS does not resolve my network issue?

If flushing the DNS cache doesn’t resolve your issue, the problem might not be DNS related. Consider restarting your router, checking your internet connection, or consulting with a network professional for further troubleshooting.

Does flushing DNS cache affect all applications on my system?

Yes, flushing the DNS cache affects all applications that rely on domain name resolution. Once the cache is cleared, all applications will query for fresh DNS information.

Can I view the contents of my DNS cache?

Directly viewing the contents of the DNS cache is not typically available on Linux systems, unlike Windows where you can use ipconfig /displaydns. However, you can infer that your DNS cache has been used by noticing faster website loading times for previously visited sites.

Conclusion

Understanding how to flush the DNS cache on your Linux system is a valuable skill for anyone regularly navigating the digital world. While it may seem like a small aspect of network management, it plays a crucial role in resolving various network-related issues and ensuring you’re accessing the most current versions of websites.

Additionally, it’s important to note that while flushing DNS is a safe and common practice, it should be done judiciously and typically when troubleshooting specific network problems. This guide aimed to demystify the process and empower you with the knowledge to handle DNS-related issues confidently.

You may also like

Leave a Comment

fl_logo_v3_footer

ENHANCE YOUR LINUX EXPERIENCE.



FOSS Linux is a leading resource for Linux enthusiasts and professionals alike. With a focus on providing the best Linux tutorials, open-source apps, news, and reviews written by team of expert authors. FOSS Linux is the go-to source for all things Linux.

Whether you’re a beginner or an experienced user, FOSS Linux has something for everyone.

Follow Us

Subscribe

©2016-2023 FOSS LINUX

A PART OF VIBRANT LEAF MEDIA COMPANY.

ALL RIGHTS RESERVED.

“Linux” is the registered trademark by Linus Torvalds in the U.S. and other countries.