When you modify DNS records (A, CNAME, MX, etc.) on your authoritative DNS server, client servers won't immediately see these changes due to DNS caching mechanisms. This TTL-based caching improves performance but causes propagation delays.
Here's how to force refresh DNS cache across different platforms:
# Linux (systemd-resolved - Ubuntu/Debian/RHEL 8+)
sudo systemd-resolve --flush-caches
sudo systemctl restart systemd-resolved
# Linux (nscd - Name Service Cache Daemon)
sudo service nscd restart
# Or alternatively:
sudo /etc/init.d/nscd restart
# Windows Server
ipconfig /flushdns
# For PowerShell:
Clear-DnsClientCache
# macOS
sudo killall -HUP mDNSResponder
# For newer macOS versions:
sudo dscacheutil -flushcache
After flushing, verify with these diagnostic commands:
# Linux - check systemd-resolved cache statistics
systemd-resolve --statistics
# Windows - display DNS resolver cache
ipconfig /displaydns
# Cross-platform lookup test
nslookup example.com
dig example.com
For infrastructure automation, consider these approaches:
# Ansible playbook snippet for Linux servers
- name: Flush DNS cache on Linux
hosts: all
tasks:
- name: Flush systemd-resolved cache
command: systemd-resolve --flush-caches
become: yes
when: ansible_service_mgr == "systemd"
- name: Restart nscd if installed
service:
name: nscd
state: restarted
become: yes
To minimize cache issues:
- Set lower TTL values (300-600 seconds) before making DNS changes
- Use
dig +trace example.com
to verify propagation - Implement DNS change monitoring with tools like DNSWatch
If problems persist:
# Bypass local cache completely
dig @8.8.8.8 example.com
nslookup example.com 1.1.1.1
Check intermediate resolvers and firewall rules that might intercept DNS traffic.
When you modify DNS records, client machines won't immediately see the changes due to DNS caching mechanisms. Here's why this happens:
- OS-level DNS caching (varies by operating system)
- Browser DNS caching (typically 60-120 seconds)
- Network-level caching (ISP or local network)
Windows Systems
Use Command Prompt with administrative privileges:
ipconfig /flushdns nbtstat -R nbtstat -RR
For PowerShell (Windows 10/11):
Clear-DnsClientCache
macOS Systems
For different macOS versions:
# macOS 10.15+ (Catalina and later) sudo dscacheutil -flushcache sudo killall -HUP mDNSResponder # Older versions sudo discoveryutil mdnsflushcache sudo discoveryutil udnsflushcaches
Linux Systems
The method depends on your DNS service:
# Systemd-resolved (Ubuntu 18.04+) sudo systemd-resolve --flush-caches # Nscd sudo /etc/init.d/nscd restart # Dnsmasq sudo /etc/init.d/dnsmasq restart
Python Implementation
Cross-platform DNS cache clearing:
import os import platform def flush_dns(): system = platform.system() if system == "Windows": os.system('ipconfig /flushdns') elif system == "Linux": os.system('sudo systemd-resolve --flush-caches') elif system == "Darwin": os.system('sudo dscacheutil -flushcache') os.system('sudo killall -HUP mDNSResponder')
Bash Script
For automation across multiple servers:
#!/bin/bash # Detect OS and flush DNS accordingly if [[ "$OSTYPE" == "linux-gnu"* ]]; then sudo systemd-resolve --flush-caches elif [[ "$OSTYPE" == "darwin"* ]]; then sudo dscacheutil -flushcache sudo killall -HUP mDNSResponder elif [[ "$OSTYPE" == "msys" ]]; then ipconfig /flushdns fi
After clearing cache, verify with these commands:
# Windows nslookup example.com # Linux/macOS dig example.com
- TTL (Time To Live) values determine how long records are cached
- Some applications maintain their own DNS caches (e.g., Java applications)
- Cloud environments may have additional caching layers