When we browse the internet, it may seem like a fleeting moment for a browser to open a webpage, but behind the scenes, multiple complex network requests and resolution steps are involved. A crucial step is DNS resolution. To improve access efficiency, both the system and the browser store a local "DNS cache" record. However, if the cache is incorrect or expires, the website may not open, redirect unexpectedly, or even be hijacked.
Ⅰ. How DNS Cache Works: An "Accelerator" for Faster Domain Name Resolution
DNS is responsible for converting the domain name we enter (e.g., *www.example.com*) into the corresponding IP address (e.g., 93.184.216.34). This process requires a query and response from a DNS server. To avoid repeated queries for each visit, the system and browser temporarily store the resolution results—this is known as DNS caching.
1. How Cache Works
When you first visit a website, your computer requests a DNS server for resolution. After obtaining the IP address, the resolution record is stored in the local cache and assigned a "Time To Live" (TTL). If you visit the same website again within the TTL period, the system will directly access the local cache instead of requesting the external DNS server, significantly improving access speed.
2. DNS Cache Hierarchy
The DNS cache isn't stored in a single location, but rather in a hierarchical structure:
- Browser cache: The browser's own cache stores the resolution records, providing the fastest response times;
- Operating system cache: Maintained by the operating system's DNS service;
- Router or network device cache: Some routers also cache DNS records;
- Upstream DNS server cache: For example, an ISP or public DNS server.
This hierarchical mechanism improves overall performance, but it also means that if a cache error occurs in one layer, the problem can propagate through the cache layers, causing access errors.
Ⅱ. How to clear the DNS cache in the operating system
Different operating systems manage the DNS cache in different ways. The following describes the clearing methods for Windows, macOS, and Linux systems.
1. Windows system
Windows maintains the local cache through the "DNS Client Service" service. The clearing method is as follows:
(1) Command line method
Open the "Command Prompt" (Run as Administrator) and enter:
ipconfig /flushdns
After execution, the system will prompt:
Successfully flushed the DNS Resolver Cache.
This means the DNS cache has been cleared. You can then try to access the target website again and the system will request the latest DNS records again.
(2) View the current cache
You can view the DNS cache contents using the following command:
ipconfig /displaydns
This command will display detailed information such as the domain name, record type, and TTL in the cache, making it easier to analyze.
(3) Additional Tips
In Windows 11, you can also execute the following command in PowerShell:
Clear-DnsClientCache
The effect is the same, but it is more suitable for scripted operations or batch cleanup tasks.
2. macOS system
macOS uses the mDNSResponder or dscacheutil process to maintain the DNS cache. The cleanup commands vary slightly between different system versions.
(1) General cleanup command
Open "Terminal" and enter:
sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder
After entering the password, the cache will be cleared. This command is applicable to macOS Mojave, Catalina, Big Sur, Monterey, and Ventura.
(2) Check DNS status
You can use the following command to view the current DNS resolution configuration of the system:
scutil --dns
This command displays all DNS servers, search domains, and interface information, which helps troubleshoot resolution anomalies.
(3) Additional refresh for Safari
macOS's Safari browser sometimes caches DNS. You can refresh it synchronously by clearing your browsing history or selecting "Clear Cache" in the "Developer" menu.
3. Linux system
There are many Linux distributions, and the DNS caching mechanisms in different environments are different.
(1) Using systemd-resolved
In newer Ubuntu and Debian systems, you can execute:
sudo systemd-resolve --flush-caches
或:
sudo resolvectl flush-caches
To view cache status:
sudo systemd-resolve --statistics
(2) Using dnsmasq or nscd
If the dnsmasq service is enabled on your system:
sudo killall -HUP dnsmasq
If using the nscd service:
sudo /etc/init.d/nscd restart
These commands will immediately flush the local DNS cache, updating the resolution results.
Ⅲ. How to Clear the DNS Cache in Your Browser
In addition to the system-wide DNS cache, modern browsers also maintain independent DNS caches to speed up web page loading. Here's how to clear each browser.
1. Google Chrome
Chrome has a built-in DNS cache system, which can be cleared using the following steps:
In the address bar, enter:
chrome://net-internals/#dns
Click the "Clear host cache" button. To ensure a thorough refresh, enter:
chrome://net-internals/#sockets
Then click "Flush socket pools."
This clears all of Chrome's DNS and TCP connection caches, often used to resolve issues like "websites can't load" or "redirect errors."
2. Microsoft Edge
Edge is based on the Chromium core, so the cleanup method is the same as Chrome. Enter:
edge://net-internals/#dns
Then, click "Clear host cache" as usual.
3. Firefox
Firefox has its own DNS cache, which can be refreshed as follows:
In the address bar, enter:
about:networking#dns
Click the "Clear DNS Cache" button in the upper-right corner.
In addition, clearing your browsing data (especially "Cached Web Content") will also clear your DNS records.
4. Safari Browser
Safari's DNS cache is typically synchronized with the macOS system-level cache. You can refresh it in the following ways:
From the Safari menu, select "Preferences," then "Privacy" → "Manage Website Data," and select "Remove All." Alternatively, open the "Developer" menu (which must be enabled in Settings) and select "Clear Cache" to clear both the DNS and web caches.
Understanding the DNS cache is the first step to maintaining network stability. While the DNS cache may seem simple, it plays a crucial role throughout the entire network communication process. Properly understanding and managing it not only allows you to quickly identify issues when a website crashes, but also ensures a smoother and more secure online experience in daily use. Clearing the DNS cache is more than just a simple "computer repair" trick; it's a skill that demonstrates mastery over the underlying logic of your network. Whether you're a user, developer, or network operator, understanding how the DNS cache works and how to clear it is crucial to improving network quality and ensuring data security.
CN
EN