DNS, or Domain Name System, resolves website names into IP addresses. Multiple DNS servers exist, allowing choice. If experiencing website connection issues or desiring DNS changes recognized by your Ubuntu machine, flush the DNS cache. This action also applies if changes to the hosts file require system acknowledgment without rebooting. To clear DNS cache in Linux:
Note: Demonstrated on an Ubuntu 16.10 “Yakkety Yak” system; applicable to most Linux distros.
Flush DNS Cache in Linux (2020 Guide)
Prior to diving into our tutorial, let’s first determine if DNS caching is enabled on your Linux machine.
Checking DNS Caching Status
Linux distributions vary in their handling of DNS caching. For instance, Ubuntu typically doesn’t cache DNS by default. Before attempting to clear the DNS cache, verify whether caching is enabled:
1. Open Terminal and enter: “ps ax | grep dnsmasq“.
2. Review the output for a field labeled “cache-size“. If its value is zero, caching is disabled.
Note: To enable DNS caching on Linux, use the command “sudo dnsmasq -c 150”. You can replace 150 with any desired number, indicating the number of entries dnsmasq caches.
Flush DNS Cache in Ubuntu
If your Linux system caches DNS entries, you can flush the DNS cache to resolve DNS-related issues. To clear DNS cache in Ubuntu:
1. Launch Terminal (ctrl + alt + T) and type “sudo /etc/init.d/dns-clean restart”.
2. Then type “sudo /etc/init.d/networking force-reload”
This will clear the DNS cache in Ubuntu. If you experienced DNS issues, they should now be resolved.
Flush DNS Cache in DNS Services on Linux
Ubuntu doesn’t cache DNS entries by default. However, if you’ve installed a DNS service like nscd, you can clear its cache. Here are methods to clear cache for common DNS services:
Flush nscd DNS Cache
- sudo /etc/init.d/nscd restart
Flush dnsmasq DNS Cache
- sudo /etc/init.d/dnsmasq restart
Flush BIND DNS Cache
- sudo /etc/init.d/named restart
- sudo rndc restart
- To clear DNS cache for a specific domain, use “sudo rndc flushname beebom.com“
- To clear DNS cache for LAN, use “sudo rndc flush lan“
- To clear DNS cache for WAN, use “sudo rndc flush wan“
Bonus: Change DNS Settings in Ubuntu
We’ve discussed flushing the DNS cache in Ubuntu, but sometimes you simply need to adjust DNS settings. To do so, you can use either the GUI or the Terminal.
Changing DNS Settings via the GUI
Changing DNS settings through the GUI is simpler than using the Terminal. However, if you have multiple connections, you’ll need to adjust the DNS settings for each. To do this:
1. Open System Settings and select Network.
2. Click the arrow next to your network’s name.
3. Click “Settings” to access the connection settings.
4. Switch to the IPv4 tab and find “Additional DNS Servers“. Enter your desired DNS servers here. To add multiple DNS servers, separate them with commas.
Change DNS Settings via Terminal
If you prefer using the Terminal to change DNS settings instead of the GUI, add the nameservers to the dnsmasq config file. Note that dnsmasq might not be installed by default on Ubuntu systems (dnsmasq-base is). First, install dnsmasq by running “sudo apt-get install dnsmasq“ in the Terminal.
- Once dnsmasq is installed, edit the config file with “sudo nano /etc/dnsmasq.conf“.
- Here, you can add your DNS servers’ IP addresses where it says “Add other nameservers here“.
Clear DNS Cache in Linux Using These Methods
You can use any of these methods to clear DNS cache in a Linux system. Issues with DNS cache can cause problems like webpages not loading, nameserver changes not being recognized, and more. Once you flush DNS cache, these problems will likely go away. Have you ever flushed the DNS cache on your Linux system? If yes, which method did you use? If you know other ways to clear the DNS cache on your Linux system, share them in the comments below.
Pritam Chopra is a seasoned IT professional and a passionate blogger hailing from the dynamic realm of technology. With an insatiable curiosity for all things tech-related, Pritam has dedicated himself to exploring and unraveling the intricacies of the digital world.