How to Flush DNS Cache in Linux Ubuntu 18.04

Posted on

Introduction

This article is mainly focus on how to flush the cache of DNS in a local Linux Ubuntu 18.04 operating system. If there are some changes on the entry in the DNS configuration, there is a specific action for refreshing or flushing the local cache of the DNS entry in Linux Ubuntu 18.04 operating system. It is very useful to get the most update IP Address of the domain name for further access. Those actions are in the following command :

systemd-resolve --flush-caches

For simulating a change of a domain name, just use a local DNS service. In order to prove that the command is working, just add an entry of to that local DNS service. The following is an attempt to resolve a domain name using ‘dig’ command.  This command is useful for DNS lookup utility. For an example, resolve a domain name of ‘test.localhost’net’ using the ‘dig’ tool as follows :

root@hostname# dig test.localhost.net

; <<>> DiG 9.11.3-1ubuntu1.8-Ubuntu <<>> test.localhost.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 41780
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;test.localhost.net.        IN  A

;; ANSWER SECTION:
test.localhost.net. 300 IN  A   91.195.240.126

;; Query time: 302 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Tue Aug 27 22:48:32 WIB 2019
;; MSG SIZE  rcvd: 63

root@hostname#

Solution

The output above is an output of the execution of ‘dig’ tool to resolve the IP Address of a domain name with the name of ‘test.localhost’net’. The result of resolving ‘test.localhost.net’ using dig command is ‘91.195.240.126’. For the purpose of simulating the command to flush the local cache of DNS entry, define the domain name ‘test.localhost.net’ in the local DNS service. Just define that the resolve of ‘test.localhost’net’ domain name will point to the local address of ‘127.0.0.1’. After successfully define it, execute the following command to flush the local cache of DNS entry in the machine :

root@hostname:~# systemd-resolve --flush-caches
root@hostname:~#

Sometimes, if the DNS service is not exist in the local operating system, the above command is enough to see the changes on the entry of domain name. Apparently, if it exist in the local operating system, the above command execution is not enough. In order to implement the change, just restart the NetworkManager as follows :

root@hostname:~# systemctl restart NetworkManager
root@hostname:~#

Execute the above command again to try to resolve ‘test’.localhost.net’. If it is a success, the output will be different in the context of the example on this article. The following is the output of the command execution ‘dig’ to resolve ‘test.localhost.net’ after defining a new IP addresses in the local DNS configuration entry and flushing the local cache DNS entry :

root@hostname:~# dig test.localhost.net

; <<>> DiG 9.11.3-1ubuntu1.8-Ubuntu <<>> test.localhost.net
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 31755
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;test.localhost.net.        IN  A

;; ANSWER SECTION:
test.localhost.net. 86400   IN  A   127.0.0.1

;; Query time: 0 msec
;; SERVER: 127.0.0.53#53(127.0.0.53)
;; WHEN: Tue Aug 27 22:51:46 WIB 2019
;; MSG SIZE  rcvd: 63

root@hostname:~#

As in the above output appears, the command for flushing the old cache of DNS entry is a success. The ‘test.localhost.net’ is not pointing to the IP Address ‘91.195.240.126’ anymore. It points to a different IP Address. Currently, it points to an IP Address of ‘127.0.0.1’.

Leave a Reply