Linux’s method of clearing DNS cache and refreshing DNS in shell terminal (Ubuntu, Debian)

preface

Use the following command to query DNS in Linux system

dig baidu.com @114.114.114.114

Or use the default DNS server to query

nslookup baidu.com

The following methods may not all work, but you can always find one that works

1.network-manager and networking

In some Linux operating systems, such as the specific version of Ubuntu, DNS is managed by network manager.
In these systems, you need to run the command:

sudo service network-manager restart

In other systems, you can use the following command:

sudo service networking restart

2.ubuntu

In Ubuntu 12.04, you can try the following command:

sudo kill -HUP $(pgrep dnsmasq)

In Ubuntu 16.04 and Debian 9, you can refresh DNS with the following command

sudo systemd-resolve --statistics

On Ubuntu 17.04 and later, use the following command:

sudo systemd-resolve --flush-caches

3 /etc/ resolv.conf

/etc/ resolv.conf This file stores the address of the DNS server. Directly modifying this file can only temporarily change the default DNS address and restore the settings after restart.
You can modify this file directly for temporary use

# Dynamic resolv.conf(5) file for glibc resolver(3) generated by resolvconf(8)
#     DO NOT EDIT THIS FILE BY HAND -- YOUR CHANGES WILL BE OVERWRITTEN
nameserver 8.8.8.8

After modification, Ping or NSLOOKUP again will get the IP address from the new DNS server
for the method of permanent modification, see how do I add a DNS server via resolv.conf?

4.nscd

NSCD is a good DNS caching service. Use the following command to install it:

apt install nscd  #debian ubuntu

If you have NSCD installed on your Linux system, you can use the following command to refresh DNS:

sudo /etc/init.d/nscd restart

Resources:
How do I clear the DNS cache?
flush DNS cache in Ubuntu

Read More: