How to Flush DNS Cache on Linux
I’ve been managing Linux servers for over a decade, and DNS issues still catch me off guard sometimes. You type in a domain, hit Enter, and… nothing. Or worse, you’re getting redirected to an old IP address that doesn’t exist anymore. That’s usually when you need to flush your DNS cache.
Let me walk you through how DNS caching works on Linux and how to clear it when things go sideways.
How DNS Cache Works on Linux
When your Linux system needs to find the IP address for a domain like www.bitslovers.com, it sends a DNS query. Instead of making that query every single time, the system caches the result in memory. The next time you need that same domain? Boom — instant response from the cache.
This caching happens through a DNS resolver service. Most modern Linux distributions use systemd-resolved, but you’ll also find dnsmasq, BIND, or nscd depending on your setup.
Here’s the thing: DNS records have a TTL (Time-To-Live) value. When that expires, your system should refresh the record. But sometimes it doesn’t, or you’ve just changed a DNS record and need to see the update immediately. That’s when you manually flush the cache.
When You Need to Flush DNS
I usually flush DNS in these situations:
- I’ve just updated a DNS record and need to verify the change
- A website isn’t loading, but I know my internet connection is fine
- I’m getting 404 errors for a site that should be working
- After connecting to or disconnecting from a VPN
The symptoms are pretty distinctive: everything else works, but specific domains won’t resolve or resolve to the wrong IP.
Finding Your DNS Resolver
Before you can flush anything, you need to know which DNS resolver your system is using. Most Linux systems listen on port 53 for DNS, so check what’s running there:
sudo lsof -i :53 -S
On most modern distributions (Ubuntu 20.04+, Debian 11+, Fedora 33+, Arch Linux), you’ll see something like:
systemd-r 21830 systemd-resolve 12u IPv4 15913414 0t0 UDP localhost:domain
systemd-r 21830 systemd-resolve 13u IPv4 15913415 0t0 TCP localhost:domain (LISTEN)
That tells you systemd-resolved is handling DNS.
Flushing DNS with systemd-resolved
This is what you’ll see on most modern Linux systems. There are two commands you can use, depending on your systemd version.
The modern method (systemd 239+, most 2021+ systems):
sudo resolvectl flush-caches
The older method (still works):
sudo systemd-resolve --flush-caches
Want to verify it actually cleared the cache? Check the statistics:
sudo resolvectl statistics
Or:
sudo systemd-resolve --statistics
You should see “Current Cache Size” at zero.
Pro tip: You can also restart the entire service if flushing doesn’t work:
sudo systemctl restart systemd-resolved
Flushing DNS with dnsmasq
NetworkManager often uses dnsmasq for DNS caching, especially on older Ubuntu versions. If you see dnsmasq in your lsof output:
sudo systemctl restart dnsmasq
Or send it a hangup signal:
sudo killall -HUP dnsmasq
Flushing DNS with BIND
If you’re running BIND as your DNS server (common in enterprise environments):
sudo rndc flush
sudo rndc reload
To view the cache before flushing:
sudo rndc dumpdb -cache
Flushing DNS with nscd
Older Red Hat and Fedora systems sometimes use nscd (Name Service Cache Daemon):
sudo systemctl restart nscd
Or the old-school way:
sudo service nscd restart
Understanding /etc/resolv.conf
The /etc/resolv.conf file tells your system which DNS servers to use. Here’s a typical systemd-resolved setup:
nameserver 127.0.0.53
options edns0
search lan
The nameserver line points to your DNS resolver (127.0.0.53 is systemd-resolved’s stub resolver). The search domain tells the resolver to try appending your local domain to short hostnames. The options line controls resolver behavior.
One thing that trips people up: systemd-resolved manages /etc/resolv.conf as a symlink. Don’t manually edit it or your changes will get overwritten. If you need custom DNS settings, use NetworkManager or systemd-resolved’s own configuration.
What If resolv.conf Is Missing?
If you’re getting “Name or service not known” errors and /etc/resolv.conf is missing or empty:
ping google.com
# ping: google.com: Name or service not known
Try restarting your network connection. For WiFi:
nmcli radio wifi off && sleep 10 && nmcli radio wifi on
This forces NetworkManager to regenerate /etc/resolv.conf from your DHCP or router settings.
Testing Your DNS After Flushing
Once you’ve flushed the cache, verify DNS is working:
Basic ping test:
ping -c 4 google.com
Detailed DNS lookup:
dig google.com +short
Or for a full breakdown:
dig google.com
You should see output like:
; <<>> DiG 9.18.1-1ubuntu1 <<>> google.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 50781
;; flags: qr rd ra; QUERY: 1, ANSWER: 6, AUTHORITY: 0, ADDITIONAL: 1
;; QUESTION SECTION:
;google.com. IN A
;; ANSWER SECTION:
google.com. 74 IN A 142.250.138.139
google.com. 74 IN A 142.250.138.113
google.com. 74 IN A 142.250.138.100
...
The “status: NOERROR” line means DNS resolved successfully.
Quick Reference: Which Command Should I Use?
| Resolver | Command |
|---|---|
| systemd-resolved (modern) | sudo resolvectl flush-caches |
| systemd-resolved (older) | sudo systemd-resolve --flush-caches |
| dnsmasq | sudo systemctl restart dnsmasq |
| BIND | sudo rndc flush |
| nscd | sudo systemctl restart nscd |
Bonus: macOS DNS Flush
Since some of you manage both Linux and Mac systems, here’s the macOS equivalent:
For modern macOS (10.12+):
sudo dscacheutil -flushcache
sudo killall -HUP mDNSResponder
For macOS 10.10:
sudo discoveryutil mdnsflushcache
sudo discoveryutil udnsflushcaches
For older macOS (10.6-10.9):
sudo dscacheutil -flushcache
Wrapping Up
Flushing DNS cache is one of those troubleshooting steps that sounds simple but solves all sorts of weird network problems. The key is knowing which resolver your system uses — 90% of the time on modern Linux, it’s systemd-resolved and resolvectl flush-caches is all you need.
If you’re still having DNS issues after flushing, check your actual DNS server settings. Sometimes the problem isn’t your local cache — it’s the upstream DNS server misbehaving. I usually configure my systems to use reliable public DNS like Cloudflare (1.1.1.1) or Google (8.8.8.8) as a backup.
For more details on /etc/resolv.conf options, check the man page. And if you want to level up your Linux skills overall, I’ve got a learning path that’ll take you from beginner to sysadmin.
Got questions or run into a weird DNS issue? Drop a comment below — I’ve probably seen it before.
Comments