How To Flush Linux / UNIX DNS Cache
I ‘m on a Dial UP Internet connection under Linux and frequent dial up disconnection causing dns problems. How do I flush DNS cache under UNIX / Linux distribution using a shell prompt?
Under MS-Windows you can use the ipconfig command to flush dns cache. However, Linux and UNIX provides various ways to flush cache. Linux can run nscd or BIND or dnsmasq as the name service caching daemon. Large and work-group servers may use BIND or dnsmasq as a dedicated caching server to speed up queries.
HowTo: Flush nscd dns cache
Nscd caches libc-issued requests to the Name Service. If retrieving NSS data is fairly expensive, nscd is able to speed up consecutive access to the same data dramatically and increase overall system performance. Just restart nscd:
$ sudo /etc/init.d/nscd restart
OR
# service nscd restart
OR
# service nscd reload
This daemon provides a cache for the most common name service requests. The default configuration file, /etc/nscd.conf, determines the behavior of the cache daemon.
Flush dnsmasq dns cache
dnsmasq is a lightweight DNS, TFTP and DHCP server. It is intended to provide coupled DNS and DHCP service to a LAN. Dnsmasq accepts DNS queries and either answers them from a small, local, cache or forwards them to a real, recursive, DNS server. This software is also installed many cheap routers to cache dns queries. Just restart the dnsmasq service to flush out dns cache:
$ sudo /etc/init.d/dnsmasq restart
OR
# service dnsmasq restart
Flush caching BIND server dns cache
A caching BIND server obtains information from another server (a Zone Master) in response to a host query and then saves (caches) the data locally. All you have to do is restart bind to clear its cache:
# /etc/init.d/named restart
You can also use rndc command as follows flush out all cache:
# rndc restart
OR
# rndc exec
BIND v9.3.0 and above will support flushing all of the records attached to a particular domain name with rndc flushname command. In this example flush all records releated to cyberciti.biz domain:
# rndc flushname cyberciti.biz
It is also possible to flush out BIND views. For example, lan and wan views can be flushed using the following command:
# rndc flush lan
# rndc flush wan
A note about Mac OS X Unix users
Type the following command as root user:
# dscacheutil -flushcache
OR
$ sudo dscacheutil -flushcache
If you are using OS X 10.5 or earlier try the following command:
lookupd -flushcache
A note about /etc/hosts file
/etc/hosts act as the static table lookup for hostnames. You need to remove and/or update records as per your requirements under Unix like operating systems:
# vi /etc/hosts
Sample outputs:
See also:
Related: Windows Vista / XP Flush DNS Cache with ipconfig Command
How to Clear/Flush the DNS Cache on Linux
by Magesh Maruthamuthu · Last Updated: April 15, 2020
You may need to flush or clear the local DNS cache on your Linux system for various reasons.
This could be one of the below reasons.
- Sometimes you may have difficulty reaching certain websites.
- When troubleshooting network related issues.
- After DNS resolvers change.
When you run flush DNS cache, this will destroy the DNS entries in the cache and perform a subsequent query to resolve the domain based on the newly configured DNS settings.
What is DNS cache and what it Does?
The DNS cache is a temporary database maintained by the computer’s operating system.
It stores information about previous DNS lookups (like information on recently visited websites and other web domains).
This will quickly resolve DNS queries when you visit the cached website by getting details from the local DNS database instead of the actual DNS server.
If you want to know DNS information about a website, use one of the commands below.
What is nscd DNS Cache
nscd stands for name service cache daemon, nscd is a daemon that provides a cache for the most common name service requests. The default configuration file located at /etc/nscd.conf .
What is dnsmasq DNS Cache
Dnsmasq is a lightweight, small footprint, easy to configure, DNS forwarder and DHCP server. It is designed to provide DNS and optionally, DHCP, to a small network & suitable for resource constrained routers and firewalls. It can serve the names of local machines which are not in the global DNS. It is designed for personal computer use and small size networks, not for big networks.
What is BIND Server DNS Cache
BIND stands for “Berkeley Internet Name Domain”. The most widely used Name Server Software, BIND is open source software that implements the Domain Name System (DNS) protocols for the Internet. BIND is by far the most widely used DNS software on the Internet, providing a robust and stable platform.
How to Clear/Flush the DNS Cache on Linux
By default, DNS caching is not installed or enabled at the OS level, but if you have installed any of the caching services listed below, use the appropriate commands to flush them.
Below is a list of the major DNS cache services used in the Linux operating system.
- systemd Resolved Service
- nscd DNS Cache
- dnsmasq DNS Cache
- BIND server DNS Cache
1) How to Flush DNS Cache on Linux Using the systemd Resolve Service
Most modern Linux operating systems use systemd, so use the command below to flush the DNS cache.
Run the below command to check if the DNS cache service is active on your system.
You can check the DNS statistics using the below command.
If it’s active and the service is running, use the below command to flush it.
2) How to Clear BIND Server DNS Cache on Linux
The BIND DNS server is used on most servers where the Web Hosting Control Panel is installed. We have seen this on servers where cPanel and CentOS Web Panel are installed.
Run the below command to flush the BIND server DNS cache on Systemd-based Linux systems.
Run the following command to clear the BIND server DNS cache on SysVinit-based Linux systems.
Also, you can use the following commands for the BIND service.
To clear the BIND DNS cache, use the below command.
To clear DNS cache for a particular domain, use the below command.
To clear DNS cache for LAN, use the below command.
To clear DNS cache for WAN, use the below command.
3) How to Flush nscd server DNS Cache on Linux
Some Linux distributions use the nscd DNS server. If so, flush it using the below commands.
Run the below command to flush the nscd server DNS cache on Systemd-based Linux systems.
Run the following command to clear the nscd server DNS cache on SysVinit-based Linux systems.
4) How to Clear the dnsmasq Server DNS Cache on Linux
Some distribution may use the dnsmasq DNS server. If so, use the below commands to flush it.
Run the below command to flush the dnsmasq server DNS cache on Systemd-based Linux systems.
Run the following command to clear the dnsmasq server DNS cache on SysVinit-based Linux systems.
5) How to Clear the DNS Cache on Windows
Use the below command to Flush the dns cache on windows systems.
Conclusion
You have learned how to clear or flush DNS cache on Windows and Linux operating systems.
How to Flush DNS Cache in Linux
DNS, or a Domain Name System, is responsible for resolving website names into their respective IP addresses. There are multiple DNS servers and you can pick and choose the one you want to use. So, if you’re having trouble connecting to a website, or if you just want a DNS change to be seen by your Ubuntu machine, you should try to flush the DNS cache. You can also clear DNS cache in Ubuntu if you made changes to the hosts file, and you want it to be “seen” by your system without having to reboot it. Well, if you’re in any of these situations, and you want to clear DNS cache, here is how to flush DNS cache in Linux.
Note : I’m using an Ubuntu 16.10 “Yakkety Yak” system to demonstrate these methods. However, the process should be similar on most Linux distros.
Flush DNS Cache in Linux in 2020
Before we get to our tutorial, let’s see first if DNS caching is enabled on your machine or not.
Checking if DNS Caching is Enabled
All Linux distros do not behave the same way, when it comes to things like caching DNS. For example, Ubuntu, by default, doesn’t cache DNS. Before we try to clear DNS cache on a Linux machine, let’s check if caching is even enabled. To do this on an Ubuntu system, you can follow the steps below:
1. Launch Terminal, and type “ps ax | grep dnsmasq“.
2. In the output from the command, you will be able to see a field called “cache-size“. Check its value. If the value is zero, it means that caching is disabled on the system.
Note : If you want to enable DNS caching on Linux, you can do so by using the command “sudo dnsmasq -c 150”. You can use any number in place of 150, which is basically the number of entries that dnsmasq can cache.
Flush DNS Cache in Ubuntu
If your Linux system is caching DNS entries, then you can try to flush the DNS cache in order to get rid of any DNS related problems. To clear DNS cache in Ubuntu, you can follow the steps below:
1. Launch Terminal (ctrl + alt + T), and type “sudo /etc/init.d/dns-clean restart“.
2. Next, type the command “sudo /etc/init.d/networking force-reload”
This will clear your DNS cache in Ubuntu, and if the problems you were experiencing were due to DNS issues, they should now be gone.
Flush DNS Cache in DNS Services on Linux
Like I said, Ubuntu doesn’t cache DNS entries by default, however, if you have manually installed a DNS service such as nscd, you can clear its cache. Methods to clear cache for some common DNS services are given below:
Flush nscd DNS Cache
Flush dnsmasq DNS Cache
Flush BIND DNS Cache
If you’re using BIND, you can clear DNS cache by using one of the following commands:
- sudo /etc/init.d/named restart
- sudo rndc restart
- sudo rndc exec
BIND version 9.3.0, and greater, support flushing DNS cache for a particular domain, as well as for the LAN, or the WAN. You can use the following commands to make use of this feature:
- To clear DNS cache for a particular 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 been talking about flushing the DNS cache in Ubuntu, however, there may be cases where you just want to make a change to the DNS settings in Ubuntu. If you want to change the DNS settings in Ubuntu, you can do that using the GUI, or via the Terminal.
Change DNS Settings using the GUI
Using the GUI to change DNS settings is easier than using the Terminal. However, if you use multiple connections, you will have to change the DNS setting for each one of them. To change DNS settings using the GUI, just follow the steps below:
1. Open System Settings, and click on Network.
2. Click on the arrow next to the name of the network you’re connected to.
3. Now, click on “Settings”. This will open up the settings for that connection.
4. Switch over to the IPv4 tab, and you’ll see an option called “Additional DNS Servers“. You can enter the DNS servers you want to use in this field. If you want to enter more than one DNS server, just separate them with commas.
Change DNS Settings using the Terminal
If you don’t want to use the GUI, and prefer changing DNS settings using the Terminal, you can do this by adding the nameservers to the dnsmasq config file. However, dnsmasq does not come installed by default in Ubuntu systems (dnsmasq-base, does). So, first you’ll have to install dnsmasq by launching a Terminal, and using “sudo apt-get install dnsmasq“.
- Once you have installed dnsmasq. You can 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 such as a webpage not loading, nameserver changes not being recognized by your system, and a lot more. Once you flush DNS cache, these problems will most likely go away. So, have you ever had to flush the DNS cache on your Linux system? If so, what method did you use? If you know of any other method to clear the DNS cache on your Linux system, do let us know in the comments section below.
2 COMMENTS
Thanks Akshay,
great article. I added the DNS-entry of the router.
But there is still a DNS-entry in System Settings that is incorrect and should be removed.
How can this be done?
With friendly greetings,
Martin
The obsolete DNS-entry was automatically removed after restarting the computer.
Almost did something like “nmcli con modify eth0 -ipv4.dns 192.168.1.1”.
With friendly greetings,
Martin