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 的密码: 

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

如何在 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

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

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

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

如何在 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

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

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

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

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

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

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

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

linuxmi@linuxmi ~/www.linuxmi.com % cat ~/cache.txt

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

如何在 Linux 上刷新 DNS 缓存

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

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

使用 systemd 解析

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

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

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

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

sudo systemd-resolved --flush-caches

在 Linux 上刷新 nscd DNS 缓存

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

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

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

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

sudo nscd -g

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

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

删除 dnsmasq DNS 缓存

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

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

sudo systemctl restart dnsmasq

或者,发出以下命令:

service dnsmasq restart

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

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

dig google.com

清除谷歌浏览器的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

Undress AI Tool

Undress AI Tool

Undress images for free

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.

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 Article

Peak: How To Revive Players
1 months ago By DDD
PEAK How to Emote
3 weeks ago By Jack chen

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)

Postman Integrated Application on CentOS Postman Integrated Application on CentOS May 19, 2025 pm 08:00 PM

Integrating Postman applications on CentOS can be achieved through a variety of methods. The following are the detailed steps and suggestions: Install Postman by downloading the installation package to download Postman's Linux version installation package: Visit Postman's official website and select the version suitable for Linux to download. Unzip the installation package: Use the following command to unzip the installation package to the specified directory, for example /opt: sudotar-xzfpostman-linux-x64-xx.xx.xx.tar.gz-C/opt Please note that "postman-linux-x64-xx.xx.xx.tar.gz" is replaced by the file name you actually downloaded. Create symbols

Detailed introduction to each directory of Linux and each directory (reprinted) Detailed introduction to each directory of Linux and each directory (reprinted) May 22, 2025 pm 07:54 PM

[Common Directory Description] Directory/bin stores binary executable files (ls, cat, mkdir, etc.), and common commands are generally here. /etc stores system management and configuration files/home stores all user files. The root directory of the user's home directory is the basis of the user's home directory. For example, the home directory of the user user is /home/user. You can use ~user to represent /usr to store system applications. The more important directory /usr/local Local system administrator software installation directory (install system-level applications). This is the largest directory, and almost all the applications and files to be used are in this directory. /usr/x11r6 Directory for storing x window/usr/bin Many

Where is the pycharm interpreter? Where is the pycharm interpreter? May 23, 2025 pm 10:09 PM

Setting the location of the interpreter in PyCharm can be achieved through the following steps: 1. Open PyCharm, click the "File" menu, and select "Settings" or "Preferences". 2. Find and click "Project:[Your Project Name]" and select "PythonInterpreter". 3. Click "AddInterpreter", select "SystemInterpreter", browse to the Python installation directory, select the Python executable file, and click "OK". When setting up the interpreter, you need to pay attention to path correctness, version compatibility and the use of the virtual environment to ensure the smooth operation of the project.

After installing Nginx, the configuration file path and initial settings After installing Nginx, the configuration file path and initial settings May 16, 2025 pm 10:54 PM

Understanding Nginx's configuration file path and initial settings is very important because it is the first step in optimizing and managing a web server. 1) The configuration file path is usually /etc/nginx/nginx.conf. The syntax can be found and tested using the nginx-t command. 2) The initial settings include global settings (such as user, worker_processes) and HTTP settings (such as include, log_format). These settings allow customization and extension according to requirements. Incorrect configuration may lead to performance issues and security vulnerabilities.

The difference between programming in Java and other languages ​​Analysis of the advantages of cross-platform features of Java The difference between programming in Java and other languages ​​Analysis of the advantages of cross-platform features of Java May 20, 2025 pm 08:21 PM

The main difference between Java and other programming languages ​​is its cross-platform feature of "writing at once, running everywhere". 1. The syntax of Java is close to C, but it removes pointer operations that are prone to errors, making it suitable for large enterprise applications. 2. Compared with Python, Java has more advantages in performance and large-scale data processing. The cross-platform advantage of Java stems from the Java virtual machine (JVM), which can run the same bytecode on different platforms, simplifying development and deployment, but be careful to avoid using platform-specific APIs to maintain cross-platformity.

MySQL installation tutorial teach you step by step the detailed steps for installing and configuration of mySQL step by step MySQL installation tutorial teach you step by step the detailed steps for installing and configuration of mySQL step by step May 23, 2025 am 06:09 AM

The installation and configuration of MySQL can be completed through the following steps: 1. Download the installation package suitable for the operating system from the official website. 2. Run the installer, select the "Developer Default" option and set the root user password. 3. After installation, configure environment variables to ensure that the bin directory of MySQL is in PATH. 4. When creating a user, follow the principle of minimum permissions and set a strong password. 5. Adjust the innodb_buffer_pool_size and max_connections parameters when optimizing performance. 6. Back up the database regularly and optimize query statements to improve performance.

Experience in participating in VSCode offline technology exchange activities Experience in participating in VSCode offline technology exchange activities May 29, 2025 pm 10:00 PM

I have a lot of experience in participating in VSCode offline technology exchange activities, and my main gains include sharing of plug-in development, practical demonstrations and communication with other developers. 1. Sharing of plug-in development: I learned how to use VSCode's plug-in API to improve development efficiency, such as automatic formatting and static analysis plug-ins. 2. Practical demonstration: I learned how to use VSCode for remote development and realized its flexibility and scalability. 3. Communicate with developers: I have obtained skills to optimize VSCode startup speed, such as reducing the number of plug-ins loaded at startup and managing the plug-in loading order. In short, this event has benefited me a lot and I highly recommend those who are interested in VSCode to participate.

Comparison between Informix and MySQL on Linux Comparison between Informix and MySQL on Linux May 29, 2025 pm 11:21 PM

Informix and MySQL are both popular relational database management systems. They perform well in Linux environments and are widely used. The following is a comparison and analysis of the two on the Linux platform: Installing and configuring Informix: Deploying Informix on Linux requires downloading the corresponding installation files, and then completing the installation and configuration process according to the official documentation. MySQL: The installation process of MySQL is relatively simple, and can be easily installed through system package management tools (such as apt or yum), and there are a large number of tutorials and community support on the network for reference. Performance Informix: Informix has excellent performance and

See all articles