contador Saltar al contenido

How to empty DNS Cache in Linux

The DNS, or a Domain Name System, responsible for resolving the names of the websites in their respective IP addresses. So, if you have problems connecting to a website, or if you simply want a DNS change to be seen by your Ubuntu machine, you should try clearing the DNS cache. You can also empty the DNS cache in Ubuntu if you've made changes to the hosts file and want it to be "seen" by your system without having to restart it. Well, if you find yourself in one of these situations and want to clear the DNS cache, here is how to clear the 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 in most Linux distributions.

Check if DNS caching is enabled

All Linux distributions do not behave the same way when it comes to things like DNS caching. For example, Ubuntu, by default, does not cache DNS. Before trying to empty the DNS cache on a Linux machine, let's check if caching is enabled. To do this on an Ubuntu system, you can take the following steps:

1. Start Terminal and type " ps ax | grep dnsmasq ".

2. In the command output, it will be possible to display 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 this by using the "sudo dnsmasq -c 150" command. You can use any number in place of 150, which basically is the number of entries that dnsmasq can cache.

Clear the DNS cache in Ubuntu

If your Linux system caches DNS entries, you can try clearing the DNS cache to eliminate any DNS-related problems. To empty the DNS cache in Ubuntu, you can follow the steps below:

1. Start 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 erase your DNS cache in Ubuntu, and if the problems you were experiencing were due to DNS problems, they should now disappear.

Clear the DNS cache in DNS services on Linux

As I said, Ubuntu does not cache DNS entries by default, however, if you have manually installed a DNS service like nscd, you can clear the cache. Here are the methods to clear the cache for some common DNS services:

Clear the nscd DNS cache

  • sudo /etc/init.d/nscd restart

Flush dnsmasq DNS Cache

  • sudo /etc/init.d/dnsmasq restart

Flush BIND DNS Cache

If you use BIND, you can clear the DNS cache using one of the following commands:

  • sudo /etc/init.d/named restart

BIND version 9.3.0 and later, support flushing the DNS cache for a particular domain, as well as for the LAN or WAN. You can use the following commands to use this feature:

  • To empty the DNS cache for a particular domain, use " sudo rndc flushname "

  • To empty the DNS cache for LAN, use " sudo rndc flush lan "

  • To empty the DNS cache for WAN, use " sudo rndc flush wan "

Bonus: change the DNS settings in Ubuntu

We talked about emptying the DNS cache in Ubuntu, however, there may be cases where you want to make a change to the DNS settings in Ubuntu. If you want to change the DNS settings in Ubuntu, you can do it using the GUI or via the terminal.

Change the 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 need to change the DNS settings for each one. To change the DNS settings using the GUI, follow these steps:

1. Open System settings and click Network.

2. Click on arrow next to the network name a to which you are connected.

3. Now, click "Settings". This will open the settings for that connection.

4. Switch to the IPv4 card and you will 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, separate them with commas.

Change the DNS settings using the terminal

If you do not want to use the GUI and prefer to change the DNS settings using the terminal, you can do so by adding the name servers to the dnsmasq configuration file. However, dnsmasq is not installed by default in Ubuntu systems (dnsmasq-base, does). So first you will have to install dnsmasq launching a terminal and using " sudo apt-get install dnsmasq ".

  • Once installed dnsmasq. can edit configuration file with " sudo nano /etc/dnsmasq.conf ".

  • Here you can add the IP addresses of your DNS servers where it says " Add more name servers here ".

Clear the DNS cache in Linux using these methods

You can use any of these methods to clear the DNS cache on a Linux system. Problems with the DNS cache can cause problems like a non-loading web page, name server changes not recognized by the system and much more. Once the DNS cache is cleared, these problems will most likely go away.

So, have you ever had to empty the DNS cache on your Linux system? If yes, which method did you use? If you know any other method to clear the DNS cache on your Linux system, let us know in the comments section below.