Windows Hosts File and DNS Client Cache: Hunting Wrong-Site and Old-IP Bugs

Resolve-DnsName www.example.com returns 198.51.100.10, the browser opens a site from 2019, and the provider’s control panel insists the A record was changed an hour ago. In that situation one of two things is lying: a line in C:\Windows\System32\drivers\etc\hosts or a stale entry in the DNS Client cache. Both are local, both override DNS in ways people forget, and both produce exactly the “wrong site / old IP” bug that gets escalated to the network team. If you are standing up a fresh host to test this, see Windows VPS plans with RDP included and work through the sequence below on the box that shows the fault.

Read the hosts file before you blame DNS

The hosts file is checked before any DNS query is sent, and only the first matching line for a name is used. Read it raw, because explorer hides it and the extensionless filename breaks naive editors: Get-Content C:\Windows\System32\drivers\etc\hosts | Where-Object { $_ -notmatch '^\s*#' -and $_.Trim() }. Anything listed there wins over both the resolver and your new A record.

Common surprises: a migration script that pinned the old server, a line with the hostname in a comment position that still parses, and an entry with tabs instead of spaces that some tools accept and others ignore. Check the file timestamp with (Get-Item C:\Windows\System32\drivers\etc\hosts).LastWriteTime; a recent date on a server nobody admits to changing is a strong signal.

Confirm the client is actually using the entry

ping www.example.com resolves the hosts entry and labels it: the reply line includes [198.51.100.10], and when the name came from the hosts file the resolution note shows the address as a literal rather than a resolved name. Compare with Resolve-DnsName www.example.com -DnsOnly, which bypasses the client cache and the hosts file for the query itself, giving you the authoritative answer from the DNS server.

If Resolve-DnsName -DnsOnly gives the correct IP and the browser still loads the old one, the hosts file or the cache is the culprit and the DNS server is exonerated. If both give the old IP, the problem is upstream — a stale record on a secondary zone, a caching forwarder, or a public resolver still holding the pre-change TTL.

Flush the cache and understand what flush does not do

ipconfig /flushdns clears the client resolver cache, then ipconfig /displaydns should show no entry for the name. Note that flush does not touch the hosts file, does not clear the browser’s own cache, and does not clear the DNS server’s cache. Chrome and Edge keep their own resolution cache; restart the browser or open an InPrivate window to rule them out.

On a server acting as a DNS server itself, also clear the server cache: Clear-DnsServerCache -Force in PowerShell, or dnscmd /clearcache. If the host forwards to a public resolver, that resolver may still hold the old record until TTL expiry — check the TTL on the current record with Resolve-DnsName name -Type A | Select-Object Name,IPAddress,TTL before assuming anything is broken locally.

DNS suffix search order decides which name you actually asked for

An unqualified name like intranet is appended with each suffix in turn until one resolves. When the machine’s primary DNS suffix is corp.example.com but the record you want lives in example.com, you can reach the wrong host entirely. Inspect the order with Get-DnsClientGlobalSetting | Select-Object SuffixSearchList and the per-interface suffix with Get-DnsClient | Select-Object InterfaceAlias,DnsSuffix,ConnectionSpecificSuffix.

The per-interface suffix is appended first, then the global list, then the primary suffix. In a multi-homed VPS with a secondary NIC left at default, that ordering changes without anyone editing DNS. Fix it with Set-DnsClientServerAddress per interface and Set-DnsClientGlobalSetting -SuffixSearchList when you need an explicit order, then re-test with the FQDN to confirm the short name was the problem.

Rule out the other local override: NetBIOS and LLMNR

Short names can also resolve through LLMNR or NetBIOS, neither of which appears in ipconfig /displaydns output the way DNS entries do. A stray machine on the same subnet answering to the name will beat DNS for a short-name lookup. Test by disabling LLMNR on the client (HKLM:\SOFTWARE\Policies\Microsoft\Windows NT\DNSClient, EnableMulticast=0) and retrying — if the answer changes, you were never talking to DNS.

For Internet-facing names this is rarely the issue; for internal names on a Windows VPS with a flat network it is common. nslookup name bypasses much of the client stack and will return the DNS answer, which makes it a useful contrast against ping and against what the application actually connected to.

Prove the fix from the application, not the shell

A corrected hosts file and an empty cache still leave the application layer. For IIS, check that the site binding host header matches the name being requested, and that no HTTP redirect rule in web.config points at the old hostname. For .NET, a hard-coded base URL in appsettings.json survives every DNS fix you make.

Confirm with a request that shows the peer: Test-NetConnection www.example.com -Port 443 prints RemoteAddress, and curl.exe -v https://www.example.com/ -o NUL shows the resolved IP followed by the SNI name. If the resolved IP is right but the certificate name is wrong, you are hitting the new server on an old binding — an IIS problem, not a resolver problem.

Keep a short, auditable workflow

The sequence that closes these tickets fastest: read the hosts file, run ipconfig /displaydns and grep for the name, run Resolve-DnsName -DnsOnly, compare the three answers, then fix whichever layer disagreed. Document any deliberate hosts entry next to it in the file itself, so the next admin does not delete it or spend an hour on a stale one.

On recurring “wrong site” reports, the hosts file is usually the result of an old migration and the cache is a symptom of a short TTL, not a bug. Review the full Windows Server specs and pricing if you are replacing host that has accumulated years of them, and rebuild with a clean resolver configuration.

Leave a Comment