How to Flush the DNS Cache in Linux

How to Flush the DNS Cache in Linux

Full Story: How to Flush the DNS Cache (macOS, Linux, Windows)

Unix Linux Windows How to Flush DNS Cache (macOS, FreeBSD, RHEL, CentOS, Debian, Ubuntu, Windows)

Please visit the original link: https://sysin.org/blog/how-to-flush-dns-cache/ to view the latest version. Original works, please keep the source for reprinting.

Author homepage: sysin.org


Refresh or clear the DNS cache, usually because there are outdated DNS records, and it is necessary to obtain updates from the server immediately, which is often used in scenarios such as security requirements or testing and debugging.

how-to-flush-dns-cache

1. Linux Flush DNS Cache General Reference

Linux can run dnsmasq, nscd, unbound, or systemd-resolved as the name service cache daemon (sysin).

dnsmasq

If your DNS server is implemented with dnsmasq, use the following command:

service dnsmasq restart

If the dnsmasq service does not exist, install dnsmasq first, the command is as follows:

  • RHEL and its compatible distributions:sudo yum install dnsmasq
  • Debian and its compatible distributions:sudo apt install dnsmasq
  • Or the package management commands corresponding to other distributions

Note: DNSmasq is a lightweight and easy-to-use DNS service tool. It can be used for IP address NAT conversion when connecting to the intranet and the Internet, and can also be used as DNS service for small networks.

nscd

If you want to clear the Cache on nscd, you can restart the nscd service to achieve the effect of clearing the DNS Cache:

service nscd restart
# 或是
service nscd reload

If the nscd service does not exist, install nscd first, the command is as follows:

  • RHEL and its compatible distributions:sudo yum install nscd
  • Debian and its compatible distributions:sudo apt install nscd
  • Or the package management commands corresponding to other distributions

unbuilt

unbound Use the unbound-control command to manage the DNS cache:

# 刷新所有缓存
unbound-control flush all
# 更多命令查看帮助
unbound-control -h

If unbound-control cannot be executed, first install unbound, the command is as follows:

  • RHEL and its compatible distributions:sudo yum install unbound
  • Debian and its compatible distributions:sudo apt install unbound
  • Or the package management commands corresponding to other distributions

systemd-resolved

Flush the DNS cache with the resolvectl command:

# Step 1. 查看 DNS 缓存状况
sudo resolvectl statistics

# Step 2. 清除 DNS 缓存,systemd-resolved daemon 默认在所有的 Ubuntu 系统上运行
sudo resolvectl flush-caches

# Step 3. 正在查看验证结果 (sysin)
sudo resolvectl statistics

If resolvectl cannot be executed, install systemd-resolved first, the command is as follows:

  • RHEL and its compatible distributions:sudo yum install systemd-resolved
  • Debian and its compatible distributions:sudo apt install systemd-resolved
  • Or the package management commands corresponding to other distributions

BIND (server side, different from client side DNS cache above)

If you are clearing the CACHE on the BIND server, use this command:

rndc flush

If rndc cannot be executed, first install bind, the command is as follows:

  • RHEL and its compatible distributions:sudo yum install bind
  • Debian and its compatible distributions:sudo apt install bind9
  • Or the package management commands corresponding to other distributions

The following is a separate description of several mainstream releases.

2. RHEL

Includes its compatible distributions: CentOS and AlmaLinux, Rocky Linux, Oracle Linux

RHEL and its compatible distributions do not enable DNS query caching by default.

参看:Best practice for DNS caching in RHEL

Common solutions:

dnsmasq

Use dnsmasq to enable dns caching:

yum -y install dnsmasq
systemctl enable --now dnsmasq

Clear the cache and restart the dnsmasq service:

systemctl restart dnsmasq

nscd

Use nscd to enable dns caching:

yum -y install nscd
systemctl enable --now nscd

Clear the cache and restart the nscd service:

systemctl restart nscd

3. Ubuntu

Ubuntu runs the systemd-resolved service by default for name service caching, use the resolvectl command to call systemd-resolved.service to resolve hostnames, IP addresses, domain names, DNS resource records and services.

Flush DNS Cache on Ubuntu

systemd-resolved.service is enabled by default:

systemctl is-enabled systemd-resolved.service
enabled

Flush the DNS cache:

# Ubuntu 22.04 示例

# Step 1. 查看 DNS 缓存状况
sudo resolvectl statistics

# Step 2. 清除 DNS 缓存,systemd-resolved daemon 默认在所有的 Ubuntu 系统上运行
sudo resolvectl flush-caches

# Step 3. 正在查看验证结果 (sysin)
sudo resolvectl statistics

Note: Ubuntu can also be configured to use nscd or dnsmasq.

Note: The resolvectl command used to be systemd-resolve in older versions, which is now deprecated. Command parameter parameters are slightly different.

# Ubuntu 20.04.5 同时支持 resolvectl 和 systemd-resolve

# Step 1. 查看 DNS 缓存状况
sudo systemd-resolve --statistics

# Step 2. 清除 DNS 缓存,systemd-resolve daemon 默认在所有的 Ubuntu 系统上运行
sudo systemd-resolve --flush-caches

# Step 3. 正在查看验证结果 (sysin)
sudo systemd-resolve --statistics

4. Debian

Debian does not have DNS caching enabled by default (base system). Can be configured using systemd-resolved.service to enable.

The following is an example for Debian 12.

Enable systemd-resolved.serivce:

sudo apt install systemd-resolved

Check that the service is enabled:

systemctl is-enabled systemd-resolved.service
enabled

Flush the DNS cache:

# Step 1. 查看 DNS 缓存状况
sudo resolvectl statistics

# Step 2. 清除 DNS 缓存,systemd-resolved daemon 默认在所有的 Ubuntu 系统上运行
sudo resolvectl flush-caches

# Step 3. 正在查看验证结果 (sysin)
sudo resolvectl statistics

Note: Debian can also be configured to use nscd or dnsmasq.


The above operations are usually also used in conjunction with the browser flushing the DNS cache.

Guess you like

Origin blog.csdn.net/netgc/article/details/131379981