DNS cache stores resolved domain name mappings to IP addresses, speeding up subsequent requests. On a Virtual Private Server (VPS), clearing the DNS cache is necessary when domains resolve to outdated IPs, causing connectivity issues, or after DNS configuration changes (e.g., updating records for a website). This guide provides practical steps to clear DNS cache on a Linux VPS (e.g., Ubuntu or CentOS) for common DNS services like systemd-resolved, dnsmasq, or bind.
Why Clear DNS Cache?
- Resolve Connectivity Issues: Fix incorrect domain resolutions due to stale cache.
- Apply DNS Changes: Ensure updated DNS records take effect immediately.
- Troubleshoot Errors: Address issues with websites or services hosted on the VPS.
- Improve Security: Remove potentially corrupted or malicious cache entries.
Prerequisites
- Root Access: Administrative access via SSH or console.
- Linux OS: Ubuntu, CentOS, or another distribution with DNS tools.
- DNS Service Knowledge: Identify if your VPS runs systemd-resolved, dnsmasq, bind, or another DNS resolver.
- Backup: Save DNS configurations before changes to avoid misconfigurations.
Steps to Clear DNS Cache on a Linux VPS
This guide uses Ubuntu 20.04, with notes for CentOS where applicable, and assumes the VPS hosts a web server or application requiring DNS resolution.
Step 1: Access the VPS
- Connect via SSH:
ssh user@your-vps-ip
- If SSH is unavailable, use the provider’s console. For example, VPS.DO’s SolusVM control panel offers VNC console access for direct management.
Step 2: Identify the DNS Resolver
- Check for common DNS services:
sudo systemctl status systemd-resolved # Ubuntu sudo systemctl status dnsmasq # Common on lightweight setups sudo systemctl status named # For BIND
- If no service is running, the VPS may rely on external resolvers (e.g., /etc/resolv.conf), which typically don’t cache locally.
Step 3: Clear Cache for systemd-resolved (Ubuntu)
- Check if systemd-resolved is active:
sudo systemctl status systemd-resolved
- Clear the cache:
sudo systemd-resolve --flush-caches
- Verify cache is cleared:
sudo systemd-resolve --statistics
Look for Cache Size: 0 or reduced entries. - Restart the service if needed:
sudo systemctl restart systemd-resolved
Step 4: Clear Cache for dnsmasq
- Check if dnsmasq is running:
sudo systemctl status dnsmasq
- Clear the cache by restarting the service:
sudo systemctl restart dnsmasq
- Alternatively, send a signal to clear cache without restarting:
sudo killall -HUP dnsmasq
Step 5: Clear Cache for bind (Named)
- Check if BIND is running:
sudo systemctl status named
- Clear the cache:
sudo rndc flush
- Clear cache for a specific domain (optional):
sudo rndc flushname your-domain.com
- Restart BIND if necessary:
sudo systemctl restart named
Step 6: Flush Local Resolver Cache (Fallback)
- If no DNS service is running, clear the local resolver cache (if applicable):
sudo resolvectl flush-caches # Modern systems with resolvectl
- Check /etc/resolv.conf for external resolvers:
cat /etc/resolv.conf
If it points to external DNS (e.g., 8.8.8.8), no local cache exists, and clearing is unnecessary.
Step 7: Test DNS Resolution
- Test resolution to verify cache clearing:
nslookup your-domain.com
Or:dig your-domain.com
- Ensure the returned IP matches the updated DNS record.
- Monitor connectivity to hosted services (e.g., web server):
curl http://your-domain.com
- Use VPS.DO’s SolusVM dashboard to check bandwidth usage, ensuring DNS tests don’t exceed plan limits (e.g., 1–10 TB for USA plans).
Step 8: Verify with Logs (Optional)
- Check logs for DNS activity:
sudo journalctl -u systemd-resolved # Ubuntu sudo tail -f /var/log/messages # CentOS (for BIND or dnsmasq)
- Look for cache flush or service restart entries.
Troubleshooting
- Persistent Old IPs: Verify the DNS service was restarted or flushed correctly. Check /etc/resolv.conf for stale nameservers.
- Service Not Found: If no DNS service is running, focus on external resolvers or client-side caches.
- Resolution Failures: Ensure nameservers in /etc/resolv.conf are valid (e.g., 8.8.8.8 or 1.1.1.1).
- Provider Restrictions: Some providers may filter DNS traffic; contact support if resolution issues persist.
Best Practices
- Verify DNS Changes: Confirm updated records with tools like dig or nslookup before clearing cache.
- Monitor Regularly: Schedule periodic cache clearing for dynamic environments:
echo "0 0 * * * systemd-resolve --flush-caches" | sudo crontab -
- Backup Configurations: Save /etc/dnsmasq.conf or /etc/named.conf before changes.
- Use Reliable Nameservers: Set trusted DNS servers (e.g., Google or Cloudflare) in /etc/resolv.conf.
- Secure DNS: Consider enabling DNSSEC for added security if using BIND.
When to Seek Help
If DNS resolution issues persist after clearing the cache, review logs (/var/log/syslog or /var/log/messages) and verify configurations. Contact your VPS provider with details, including test results. Providers typically offer 24/7 support for network-related issues.
Clearing the DNS cache on a VPS ensures accurate domain resolution, maintaining connectivity and reliability for hosted services.