• DNS

    How to Flush Your Linux DNS Cache in Seconds

    DNS (Domain Name System) is like the phonebook of the internet, translating human-friendly domain names into IP addresses. To make browsing faster, your Linux system stores DNS lookups locally in a DNS cache. But sometimes, that cache causes issues—like loading outdated websites, running into connection errors, or struggling with DNS changes.

    The quick fix? Flushing your DNS cache.

    In this guide, we’ll explain what the DNS cache is, why you might need to clear it, and how to flush it instantly on different Linux distributions.

    What Is the DNS Cache in Linux?

    The DNS cache is a temporary database your computer keeps to remember recent domain lookups. For example, when you visit example.com, the IP address is stored locally.

    Benefits of the cache:

    • Faster browsing (no need to resolve domains repeatedly).
    • Reduced DNS traffic.

    Problems arise when cached records become stale or corrupted, causing:

    • Websites failing to load.
    • Old IP addresses being used after a DNS update.
    • Security issues from DNS spoofing.

    Why Flush the DNS Cache?

    You may need to flush your DNS cache when:

    • A website recently changed its DNS records.
    • You encounter repeated “server not found” or connection errors.
    • You suspect DNS spoofing or security issues.
    • You’re testing DNS changes as a developer or sysadmin.

    How to Flush DNS Cache in Linux

    Different Linux systems use different DNS caching services. Here are the most common methods:

    1. systemd-resolved (Ubuntu 18.04+, Debian, Fedora, etc.)

    If your system uses systemd-resolved:

    sudo systemd-resolve --flush-caches
    

    Check the cache size:

    systemd-resolve --statistics
    

    2. nscd (Name Service Cache Daemon)

    Some older distributions use nscd:

    sudo /etc/init.d/nscd restart
    

    Or:

    sudo service nscd restart
    

    3. dnsmasq

    If your system uses dnsmasq:

    sudo /etc/init.d/dnsmasq restart
    

    Or:

    sudo systemctl restart dnsmasq
    

    4. BIND (named)

    For servers running BIND:

    sudo rndc flush
    

    Flush a specific zone:

    sudo rndc flushname example.com
    

    Quick One-Liners by System

    • Ubuntu/Debian (systemd): sudo systemd-resolve --flush-caches
    • CentOS/RHEL (nscd): sudo service nscd restart
    • Fedora (dnsmasq): sudo systemctl restart dnsmasq
    • BIND servers: sudo rndc flush

    Verifying DNS Cache Flush

    After flushing, test with:

    dig example.com
    

    or

    nslookup example.com
    

    If you see updated IPs or reduced response times, your cache was successfully cleared.

    Keeping Your DNS Healthy

    Regularly maintaining your DNS setup helps avoid many common connection issues. Along with the occasional cache flush, it’s a good idea to keep your system updated, monitor DNS performance, and double-check settings after making changes. These small habits ensure your browsing stays fast, reliable, and secure.

    Conclusion

    Flushing your Linux DNS cache is quick, easy, and can save you from hours of frustration. Whether you’re a developer testing DNS changes or a user troubleshooting connection issues, knowing these commands keeps your system clean and responsive.

    So next time a website doesn’t load correctly, remember: sometimes all it takes is a simple DNS cache flush to get things back on track.

  • Commands

    Comparing nslookup and dig Commands for Network Diagnostics

    Diagnosing DNS issues is a routine task for system administrators, network engineers, and anyone managing Linux servers. Two commonly used tools for DNS lookups are nslookup and dig. While they serve similar purposes, they have different strengths, syntax, and outputs. This article takes a deep dive into both nslookup and dig commands, comparing their features, use cases, and which tool is more suited for modern-day diagnostics.

    Understanding DNS Lookup

    Before comparing the tools, it’s important to understand what DNS lookups are. Every time you type a URL into your browser, your system performs a DNS query to translate the domain name into an IP address. This process is fundamental to almost every interaction on the internet.

    Network diagnostic tools like nslookup and dig commands allow you to manually perform these DNS queries, analyze the results, and troubleshoot issues such as:

    • Slow DNS resolution
    • Incorrect DNS records
    • Domain propagation delays
    • Network misconfigurations

    What is nslookup?

    nslookup, short for “name server lookup,” is one of the oldest tools for querying DNS to obtain domain name or IP address mapping. It was originally developed as part of the BIND (Berkeley Internet Name Domain) software suite.

    Key Characteristics:

    • Simple command-line syntax
    • Available on almost all operating systems, including Windows, Linux, and macOS
    • Supports interactive and non-interactive modes
    • Considered deprecated by some Linux distributions (e.g., newer versions of BIND)

    Basic Syntax:

    nslookup [domain]

    Example:

    nslookup linuxserverpro.com

    This will return the domain’s IP address, the DNS server queried, and basic record information.

    Interactive Mode:

    nslookup

    > set type=MX

    > linuxserverpro.com

    The interactive mode allows you to perform multiple lookups without exiting the tool.

    What is dig?

    dig (Domain Information Groper) is a powerful DNS query tool used to retrieve detailed DNS information. It was designed to replace older tools like nslookup and is widely used in Linux and Unix environments.

    Key Characteristics:

    • Provides structured, detailed output
    • Designed for ease of scripting and automation
    • Supports all DNS record types and advanced queries
    • Part of the BIND9 package (may need to be installed)

    Basic Syntax:

    dig [domain]

    Example:

    dig linuxserverpro.com

    This outputs detailed DNS information, including:

    • Header flags (e.g., recursion desired, authoritative answer)
    • Question section
    • Answer section
    • Authority and Additional sections
    • Query time and server used

    You can easily modify your query to target specific record types:

    dig linuxserverpro.com MX

    Or perform a reverse lookup:

    dig -x 1.1.1.1

    Side-by-Side Comparison of nslookup and dig commands

    Featurenslookupdig
    AvailabilityPre-installed on most OSesMay require installing dnsutils or BIND
    Output DetailBasic informationRich, structured, and complete DNS info
    Scripting & AutomationLimited usability in scriptsDesigned for scripting
    Output ReadabilityEasier for beginnersMore verbose, better for diagnostics
    Deprecation StatusDeprecated in some environmentsActively maintained and recommended
    Reverse Lookup SupportYesYes
    DNSSEC SupportNoYes
    Advanced Query OptionsLimitedExtensive options with flags and parameters

    Practical Examples

    1. A Record Lookup

    nslookup:

    nslookup linuxserverpro.com

    dig:

    dig linuxserverpro.com A

    2. MX Record Lookup (Mail Server)

    nslookup:

    nslookup -query=MX linuxserverpro.com

    dig:

    dig linuxserverpro.com MX

    3. Reverse DNS Lookup

    nslookup:

    nslookup 8.8.8.8

    dig:

    dig -x 8.8.8.8

    4. Query Specific DNS Server

    nslookup:

    nslookup linuxserverpro.com 1.1.1.1

    dig:

    dig linuxserverpro.com @1.1.1.1

    Performance and Use Cases

    When to Use nslookup

    • Quick lookups on systems where dig is not installed
    • Simple queries (e.g., checking if a domain resolves)
    • Familiarity for Windows users

    When to Use dig

    • Detailed DNS analysis
    • Scripted network diagnostics
    • Verifying DNSSEC records
    • Investigating DNS propagation issues
    • Reverse lookups and record tracing

    Pros and Cons

    nslookup Pros:

    • Pre-installed on most systems
    • Easy to use and understand
    • Familiar to many legacy admins

    nslookup Cons:

    • Deprecated on some Linux systems
    • Output is limited and less structured
    • Not ideal for scripting or automation

    dig Pros:

    • Rich and structured output
    • Scripting-friendly
    • Modern and widely supported
    • Detailed control over queries

    dig Cons:

    • May need to install dnsutils or BIND tools
    • Verbose output may be overwhelming for beginners

    Conclusion: Which Should You Use?

    If you’re looking for a quick, one-time DNS query, nslookup will get the job done. However, for in-depth diagnostics, scripting, and modern network troubleshooting, dig is the superior tool. Its comprehensive output, flexibility, and active maintenance make it a go-to choice for system administrators and network engineers working in Linux environments.

    In most professional setups, you’ll want dig in your toolkit — it’s simply more powerful, flexible, and informative.