Table of Contents
#Why do you need to flush the DNS cache on Linux? " >#Why do you need to flush the DNS cache on Linux?
How to view the local DNS cache on Linux" >How to view the local DNS cache on Linux
View system-resolved DNS cache" >View system-resolved DNS cache
View the local DNS cache of nscd" >View the local DNS cache of nscd
Show DNS cache generated by dnsmasq
如何在 Linux 上刷新 DNS 缓存" >如何在 Linux 上刷新 DNS 缓存
使用 systemd 解析" >使用 systemd 解析
清除谷歌浏览器的DNS缓存" >清除谷歌浏览器的DNS缓存
Linux是学习网络的最佳操作系统" >Linux是学习网络的最佳操作系统
Home Operation and Maintenance Nginx How to view and refresh DNS cache on Linux

How to view and refresh DNS cache on Linux

Jun 29, 2023 pm 02:40 PM
linux

By flushing your local DNS cache, you can resolve HTTP errors and protect yourself from DNS spoofing. Here's how to do it on Linux.

When you visit a website using a domain name, your system sends a request to the DNS server to obtain the IP address for the domain. This domain IP address pair is saved in the DNS cache for later use, so you don't have to send a request to the DNS server every time to establish a connection.

But sometimes, the local DNS cache becomes corrupted and causes HTTP errors. Thankfully, flushing and rebuilding the DNS cache is very easy in Linux operating systems. Here's how it's done.

如何在 Linux 上查看和刷新 DNS 缓存

#Why do you need to flush the DNS cache on Linux?

There are several reasons why you might want to rebuild the DNS cache stored on your system. If your DNS record is out of date, you may want to re-obtain it from the DNS server. Additionally, if you are concerned that your system has been compromised, you may want to ensure that the DNS cache has not been tampered with, also known as DNS spoofing.

When you flush the DNS cache, the system must ping the DNS server again and obtain a new domain IP address record from it, removing any stale or corrupted data in the process.

How to view the local DNS cache on Linux

Before systemd, most Linux distributions did not have a system-wide DNS cache, unless something like dnsmasq or nscd The program is set up manually. systemd comes with systemd-solved, a service that resolves domain names to IP addresses and caches DNS entries.

The following sections will guide you on how to view the contents of the DNS cache generated by systemd resolution, nscd, and dnsmasq so that you can understand the cached data before you decide to flush the cached data.

View system-resolved DNS cache

To view systemd-resolved cache records, you need to temporarily stop the service and then export its logs to a file.

First send the SIGUSR1 signal to terminate the systemd parsed service:

linuxmi@linuxmi ~/www.linuxmi.com % sudo killall -USR1 systemd-resolved[sudo] linuxmi 的密码: 
Copy after login

Use the journalctl command and the standard output operator to save the output to a text file:

linuxmi@linuxmi ~/www.linuxmi.com % sudo journalctl -u systemd-resolved > ~/cache.txtlinuxmi@linuxmi ~/www.linuxmi.com
Copy after login

如何在 Linux 上查看和刷新 DNS 缓存

You can then view the contents of the file using a text editor such as Vim:

如何在 Linux 上查看和刷新 DNS 缓存

In the file, search for "CACHE:" by pressing Escape, Type "/CACHE:" and press Enter. All DNS records listed under "CACHE:" are included in the local DNS cache. If you are using Vim, press the n key to jump to the next set of DNS entries.

如何在 Linux 上查看和刷新 DNS 缓存

View the local DNS cache of nscd

To view the local cache generated by nscd, you need to use the string command to read the nscd host The contents of the database.

On Debian and Ubuntu based distributions, this file is located in /var/cache/nscd/hosts. Run the following command to view the file:

linuxmi@linuxmi ~/www.linuxmi.com % sudo strings /var/cache/nscd/hosts | uniq
Copy after login

如何在 Linux 上查看和刷新 DNS 缓存

To view general statistics about the nscd DNS cache, use the -g flag:

linuxmi@linuxmi ~/www.linuxmi.com % sudo nscd -g
Copy after login

如何在 Linux 上查看和刷新 DNS 缓存

Show DNS cache generated by dnsmasq

Getting the exact records is not easy because dnsmasq stores the DNS cache in memory. You can send a kill signal to dnsmasq and log its output to get the number of DNS queries processed.

To do this, first, make sure dnsmasq is up and running using the systemctl command:

linuxmi@linuxmi ~/www.linuxmi.com % sudo systemctl status dnsmasq
Copy after login

如何在 Linux 上查看和刷新 DNS 缓存

If the status says "Active", run the following command to terminate the service:

linuxmi@linuxmi ~/www.linuxmi.com % sudo pkill -USR1 dnsmasq
Copy after login

如何在 Linux 上查看和刷新 DNS 缓存

使用 journalctl 命令,提取 dnsmasq 日志并将它们保存到文本文件中:

linuxmi@linuxmi ~/www.linuxmi.com % sudo journalctl -u dnsmasq > ~/cache.txt
Copy after login

最后,使用文件查看实用程序(如 cat 或更少)查看文件的内容:

linuxmi@linuxmi ~/www.linuxmi.com % cat ~/cache.txt
Copy after login

如何在 Linux 上查看和刷新 DNS 缓存

如何在 Linux 上刷新 DNS 缓存

刷新 DNS 缓存意味着从计算机中删除缓存的 DNS 记录。这将迫使它向DNS服务器发送请求,以获取新的DNS条目。

以下是在 Linux 上刷新 DNS 缓存的方法:

使用 systemd 解析

您可以使用 resolvectl 命令刷新 systemd 解析存储的 DNS 缓存:

linuxmi@linuxmi ~/www.linuxmi.com % sudo resolvectl flush-caches
Copy after login

如何在 Linux 上查看和刷新 DNS 缓存

如果您运行的是 Ubuntu 17.04 或 18.04,请使用 systemd 解析的命令刷新缓存:

sudo systemd-resolved --flush-caches
Copy after login

在 Linux 上刷新 nscd DNS 缓存

删除 nscd 的 DNS 缓存的最便捷方法是重新启动服务。您可以通过运行以下命令来执行此操作:

linuxmi@linuxmi ~/www.linuxmi.com % sudo /etc/init.d/nscd restart
Copy after login

如何在 Linux 上查看和刷新 DNS 缓存

如果这不起作用,首先,检查存储在PC上的本地缓存是否持久。您可以使用 -g 标志进行验证:

sudo nscd -g
Copy after login

如果是这种情况,请使用带有 nscd 命令的 –i 标志来清除记录(i 代表 invalidate):

linuxmi@linuxmi ~/www.linuxmi.com % sudo nscd -i hosts
Copy after login

删除 dnsmasq DNS 缓存

刷新 dnsmasq 生成的 DNS 缓存很简单。由于缓存存储在内存中,因此重新启动服务会删除所有存储的条目。

要重新启动 dnsmasq,请运行以下 systemctl 命令:

sudo systemctl restart dnsmasq
Copy after login

或者,发出以下命令:

service dnsmasq restart
Copy after login

如果出现提示,请输入管理员密码。现在要重新启动 dnsmasq,所有缓存中存在的 DNS 条目将被清除。

建议刷新 DNS 缓存后,检查本地缓存条目以确认数据已经成功删除。您可以使用 Linux 命令之一的 dig 来进行网络故障排除,并查看输出中的“查询时间”值。假如超过 0 毫秒,则说明缓存已经成功清除(若为 0 毫秒,则表示域记录仍存在于缓存中)。

dig google.com
Copy after login

清除谷歌浏览器的DNS缓存

您经常使用的 Web 浏览器也会缓存 DNS 记录。输入 URL 时,系统会在本地浏览器缓存中搜索缓存条目。如果未找到,它将检查本地系统缓存中的记录。清除 Web 浏览器的 DNS 缓存非常重要,因为它优先于系统范围的缓存。

为了演示,让我们刷新谷歌浏览器中的DNS缓存。在其他浏览器上也有一些方法可以做到这一点,所以最好 Google 一下如何使用你使用的浏览器做到这一点。

首先,在URL栏中键入“chrome://net-internals/#dns”并按Enter键:

如何在 Linux 上查看和刷新 DNS 缓存

点击“清除主机缓存”按钮来清除谷歌浏览器中存储的DNS条目。

Linux是学习网络的最佳操作系统

Linux 乍一看可能看起来很复杂,但如果你花一些时间来学习它是如何工作的,你很快就会意识到它很棒,甚至可能比 Windows 或 macOS 更好。

大多数在线服务器都运行 Linux,如果您想学习网络或想知道计算机通常如何工作,这也是 Linux 理想选择的原因之一。

The above is the detailed content of How to view and refresh DNS cache on Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What computer configuration is required for vscode What computer configuration is required for vscode Apr 15, 2025 pm 09:48 PM

VS Code system requirements: Operating system: Windows 10 and above, macOS 10.12 and above, Linux distribution processor: minimum 1.6 GHz, recommended 2.0 GHz and above memory: minimum 512 MB, recommended 4 GB and above storage space: minimum 250 MB, recommended 1 GB and above other requirements: stable network connection, Xorg/Wayland (Linux)

Linux Architecture: Unveiling the 5 Basic Components Linux Architecture: Unveiling the 5 Basic Components Apr 20, 2025 am 12:04 AM

The five basic components of the Linux system are: 1. Kernel, 2. System library, 3. System utilities, 4. Graphical user interface, 5. Applications. The kernel manages hardware resources, the system library provides precompiled functions, system utilities are used for system management, the GUI provides visual interaction, and applications use these components to implement functions.

vscode terminal usage tutorial vscode terminal usage tutorial Apr 15, 2025 pm 10:09 PM

vscode built-in terminal is a development tool that allows running commands and scripts within the editor to simplify the development process. How to use vscode terminal: Open the terminal with the shortcut key (Ctrl/Cmd). Enter a command or run the script. Use hotkeys (such as Ctrl L to clear the terminal). Change the working directory (such as the cd command). Advanced features include debug mode, automatic code snippet completion, and interactive command history.

How to check the warehouse address of git How to check the warehouse address of git Apr 17, 2025 pm 01:54 PM

To view the Git repository address, perform the following steps: 1. Open the command line and navigate to the repository directory; 2. Run the "git remote -v" command; 3. View the repository name in the output and its corresponding address.

Where to write code in vscode Where to write code in vscode Apr 15, 2025 pm 09:54 PM

Writing code in Visual Studio Code (VSCode) is simple and easy to use. Just install VSCode, create a project, select a language, create a file, write code, save and run it. The advantages of VSCode include cross-platform, free and open source, powerful features, rich extensions, and lightweight and fast.

How to run java code in notepad How to run java code in notepad Apr 16, 2025 pm 07:39 PM

Although Notepad cannot run Java code directly, it can be achieved by using other tools: using the command line compiler (javac) to generate a bytecode file (filename.class). Use the Java interpreter (java) to interpret bytecode, execute the code, and output the result.

What is the main purpose of Linux? What is the main purpose of Linux? Apr 16, 2025 am 12:19 AM

The main uses of Linux include: 1. Server operating system, 2. Embedded system, 3. Desktop operating system, 4. Development and testing environment. Linux excels in these areas, providing stability, security and efficient development tools.

How to run sublime after writing the code How to run sublime after writing the code Apr 16, 2025 am 08:51 AM

There are six ways to run code in Sublime: through hotkeys, menus, build systems, command lines, set default build systems, and custom build commands, and run individual files/projects by right-clicking on projects/files. The build system availability depends on the installation of Sublime Text.

See all articles