Recipe 13.11 Clearing the DNS Cache

13.11.1 Problem

You want to clear the DNS cache. The DNS cache contains resource records that are cached for a period of time in memory so that repeated requests for the same record can be returned immediately. There are two types of DNS cache. One pertains to the resolver on any Windows client (servers and workstations), and the other to the cache used by the Microsoft DNS server.

13.11.2 Solution

To flush the client resolver cache, use the following command:

> ipconfig /flushdns

To flush the DNS server cache, use any of the following solutions.

13.11.2.1 Using a graphical user interface
  1. Open the DNS Management snap-in.

  2. Right-click on DNS in the left pane and select Connect to DNS Server.

  3. Enter the server you want to connect to and click Enter.

  4. Right-click on the server and select Clear Cache.

13.11.2.2 Using a command-line interface

The following command will clear the cache on <DNSServerName>. You can leave out <DNSServerName> to run against the local server:

> dnscmd <DNSServerName> /clearcache
13.11.2.3 Using VBScript
' This code clears the DNS server cache on the specified server.
' ------ SCRIPT CONFIGURATION ------
strServer = "<DNSServerName>"   ' e.g. dc1.rallencorp.com
' ------ END CONFIGURATION ---------

set objDNS = GetObject("winmgmts:\\" & strServer & "\root\MicrosoftDNS")
set objDNSServer = objDNS.Get("MicrosoftDNS_Server.Name="".""")
set objDNSCache  = objDNS.Get("MicrosoftDNS_Cache.ContainerName=""..Cache""" & _
                              ",DnsServerName=""" & objDNSServer.Name & _
                              """,Name=""..Cache""")
objDNSCache.ClearCache
WScript.Echo "Cleared server cache"

13.11.3 Discussion

The client resolver cache is populated whenever a DNS lookup is performed on a workstation or server; for example, with the nslookup command.

<DeletedRepeatedText>

The second type of cache is only for Microsoft DNS servers. It is a cache of all DNS requests the server has made to resolve queries from clients. You can view this cache by browsing the Cached Lookups folder for a server in the DNS Management snap-in. This folder is not shown by default, so you'll need to select Advanced from the View menu.

With both the client and server cache, the records are removed from the cache after the record's TTL or Time To Live value expires. The TTL is used to age records so that clients and servers have to rerequest them at a later point and receive any changes that may have occurred.



    Chapter 3. Domain Controllers, Global Catalogs, and FSMOs
    Chapter 6. Users
    Appendix A. Tool List