ubuntu清除dns缓存_如何在Ubuntu上清除DNS缓存

本文介绍了在Ubuntu操作系统中清除DNS缓存的重要性,当网站IP地址改变时,需要刷新DNS缓存以访问新地址。文章详细说明了如何通过运行特定命令来检查DNS服务状态、清除DNS缓存以及重启systemd-resolved服务来彻底清理缓存统计信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ubuntu清除dns缓存

Whenever we try to access a website, a DNS name resolution query is executed to resolve its IP address. The operating systems cache this data to avoid reaching out to name servers every time.

每当我们尝试访问网站时,都会执行DNS名称解析查询来解析其IP地址。 操作系统缓存此数据,以避免每次都与名称服务器联系。

为什么要清除DNS缓存? (Why to Clear DNS Cache?)

Sometimes a website IP address is changed when it’s moved to another server. In that case, if we hit the cached IP address, the website won’t work.

有时,网站的IP地址在移至另一台服务器时会更改。 在这种情况下,如果我们点击了缓存的IP地址,则该网站将无法正常运行。

DNS entries have the “Time To Live (TTL)” value associated with it, which tells the Operating System name resolution service when to invalidate the DNS Cache.

DNS条目具有与之关联的“生存时间(TTL)”值,该值告诉操作系统名称解析服务何时使DNS缓存无效。

But, if you want to reach out to the new IP address before TTL expires, the only solution is to flush the DNS cache.

但是,如果您想在TTL过期之前联系到新的IP地址,则唯一的解决方案是刷新DNS缓存。

Ubuntu服务进行DNS名称解析 (Ubuntu Service for DNS Name Resolution)

The systemd-resolved.service is used to run the DNS queries and maintain the DNS Cache.

systemd-resolved.service用于运行DNS查询和维护DNS缓存。

We can use the following command to check its status.

我们可以使用以下命令来检查其状态。


# sudo systemctl status systemd-resolved
Ubuntu Systemd Resolved Service Status
Ubuntu systemd-resolved.service Status
Ubuntu systemd-resolved.service状态

We can run the following command to check the DNS Cache statistics.

我们可以运行以下命令来检查DNS缓存统计信息。


# sudo systemd-resolve --statistics
Ubuntu Dns Cache Statistics
Ubuntu Dns Cache Statistics
Ubuntu Dns缓存统计信息

There is a slight difference in the service and command name. The command name doesn’t have the last character “d”.

服务和命令名称略有不同。 命令名称没有最后一个字符“ d”。

如何在Ubuntu上清除DNS缓存? (How to Clear DNS Cache on Ubuntu?)

We can run the following command to clear the DNS cache on Ubuntu.

我们可以运行以下命令来清除Ubuntu上的DNS缓存。


# sudo systemd-resolve --flush-caches

Then, check the statistics again to confirm that the “Current Cache Size” is 0.

然后,再次检查统计信息以确认“当前缓存大小”为0。

Ubuntu Dns Flush Cache
Ubuntu Dns Flush Cache
Ubuntu Dns刷新缓存

重新启动systemd解析的服务以清除所有DNS缓存统计信息 (Restarting systemd-resolved service to clear all DNS Cache Statistics)

The DNS flush command doesn’t clear the cache hits and misses statistics. If you want to clear all the Cache stats, then you have to restart the systemd-resolved service.

DNS刷新命令不会清除缓存命中和未命中统计信息。 如果要清除所有缓存统计信息,则必须重新启动systemd解析的服务。


# sudo systemctl restart systemd-resolved
Ubuntu Restart Systemd Resolved Service
Ubuntu Restart Systemd Resolved Service
Ubuntu重新启动Systemd解决的服务

结论 (Conclusion)

It’s very easy to flush cache on Ubuntu. The command to flush the DNS can be run by the root user or a user with sudo privileges.

在Ubuntu上刷新缓存非常容易。 刷新DNS的命令可以由root用户或具有sudo特权的用户运行。

翻译自: https://www.journaldev.com/40077/ubuntu-clear-dns-cache

ubuntu清除dns缓存

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值