Think Like a Computer
No ads, no affiliate links, no software recommendations you did not ask for.

The Windows Hosts File: Name Resolution Before DNS

How the Windows hosts file maps names to addresses before DNS is queried, where it lives, how to edit it, and why changes sometimes appear not to take effect.

The hosts file is a plain-text list of name-to-address mappings that Windows consults before it sends any query to a DNS server. When an application asks the resolver to turn a name into an address, the resolver reads the hosts file first. If a matching entry exists, that address is returned and no DNS traffic is generated for that name. If no entry matches, resolution proceeds to the configured DNS servers in the usual order. The file is a static override, not a cache and not a zone: it holds only what someone has typed into it, and it is read fresh each time the resolver consults it.

The file lives at a fixed path on every Windows installation:

C:\Windows\System32\drivers\etc\hosts

That path is the same on 32-bit and 64-bit systems; the 32-bit view of the file system is redirected to the same file, so a 32-bit application reading the hosts file sees the same contents as a 64-bit one. The directory also contains lmhosts.sam, a sample for the legacy NetBIOS name resolution mechanism, and networks, a list of network names. Only hosts affects ordinary name resolution.

What the resolver does with the file

The order of operations matters for predicting behaviour. When a name is resolved, Windows checks, in sequence, whether the name is the local machine name, then the hosts file, then the DNS client cache, then the configured DNS servers. The hosts file is consulted before the cache, so an entry in the file takes precedence over an address that was previously cached from DNS. A change to the file therefore does not require a cache flush to take effect for names that are listed in it; the file is read on each lookup.

A cache flush matters in the opposite direction. If an entry is removed from the hosts file, the name falls through to DNS, and the DNS client cache may still hold an address learned earlier. Clearing the cache with ipconfig /flushdns removes those cached records so the next lookup goes to the DNS server. The same command is used when a DNS record has changed on the server and the client is still using the old address. The hosts file itself is not cached, so flushing does not change what the file says.

Editing the file as an administrator

The file is protected by an access control list that permits standard users to read it but not to write it. Opening it in Notepad without elevation produces a save error or a prompt to save elsewhere. To edit it, start an elevated editor. From an elevated Command Prompt or PowerShell window, the editor inherits the elevated token:

notepad C:\Windows\System32\drivers\etc\hosts

Saving from that instance writes to the protected location. An alternative is to copy the file to a working location, edit it there, and copy it back from an elevated prompt, but the direct edit is shorter and avoids leaving a modified copy in a user profile.

Before editing, note that the file has no extension and no formatting. It is read line by line. A mistake in the file does not produce a visible error; the affected lines are ignored or the whole file is treated as unparseable, and resolution falls through to DNS as if the entry were absent.

The format of an entry

Each line contains an address, whitespace, and one or more names. The address must be an IPv4 or IPv6 literal. Names are matched case-insensitively. A comment begins with # and runs to the end of the line. Blank lines are ignored.

# address        name
127.0.0.1        localhost
192.168.1.10     fileserver fileserver.corp.example.com
::1              localhost

A single line can carry several names, all mapped to the same address. The first name on the line is the canonical one for some tools, but the resolver treats all names on the line as equivalent mappings. Entries are matched against the exact name the application requested; a mapping for fileserver does not answer a request for fileserver.corp.example.com unless that name is also listed.

The default file shipped with Windows contains commented-out examples and the localhost entry. The comments are not required and can be removed, but leaving them makes later edits easier to read.

When an entry appears not to work

A hosts entry that is correctly formatted and saved can still fail to affect a particular application. The reason is that the hosts file is consulted by the Windows resolver, and not every program uses the Windows resolver. Some applications implement their own name resolution, either to control the order of lookups, to use a different protocol, or to support features the system resolver does not expose. A browser configured to use a proxy, for example, may send the name to the proxy rather than resolving it locally, so the hosts file is never consulted for that request. Applications that use their own DNS library, or that are configured with an explicit server address, likewise bypass the file.

This is a property of the application, not a fault in the file. The way to confirm which behaviour is in play is to test the same name with a tool that uses the system resolver, such as ping or nslookup. If ping returns the address from the hosts file but the application does not, the application is resolving the name by another route. The hosts file is working; the application is not asking it.

A second cause of apparent failure is a syntax problem. An address with the wrong number of octets, a missing address, a name containing characters the resolver does not accept, or a line that begins with whitespace before the address can cause the line to be ignored. The file has no error reporting, so the only symptom is that the name resolves as it did before the edit.

A third cause is a stale entry elsewhere. If the name was previously resolved through DNS and cached, and the hosts entry is added afterward, the hosts file takes precedence and the cache is irrelevant. If the hosts entry is removed, the cached DNS address may still be returned until the cache is cleared or the record expires.

Malware that rewrites the hosts file

Because the hosts file overrides DNS without any network traffic, it is a convenient place for malware to redirect a name. A common pattern is a set of entries that point security-update hosts, antivirus vendor domains, or banking sites to an address controlled by the attacker. The entries look like ordinary lines and are not flagged by the resolver.

A hosts file that has been altered this way is usually recognisable by entries the user did not add, particularly entries for domains the user does not administer. The file should be compared against a known-good copy or reviewed line by line. Removing the entries restores normal resolution for those names; the underlying compromise, if any, is a separate matter. The file is not the mechanism of compromise, only a place the compromise is recorded.

What the file does not do

The hosts file maps names to addresses. It does not redirect ports, change protocol behaviour, or affect traffic that is already addressed to an IP literal. An application that connects to 192.168.1.10 directly never consults the file, because no name is being resolved. A service that listens on a name is reached at the address the file supplies, but the port is determined by the client’s request, not by the file.

The file also does not override a name that the application has been configured to resolve elsewhere, as described above. It is a system-wide default for applications that use the system resolver, not a universal interceptor.

Restoring a damaged file

If the file has been corrupted or replaced, the simplest recovery is to recreate it with the default contents: the commented header and the localhost line. The file is not protected by Windows File Protection in the way system binaries are, so there is no built-in repair command for it. Deleting the file and creating a new one with the default content restores normal behaviour, since an absent hosts file is treated as an empty one. Before deleting, copy the current file elsewhere if any of its entries are needed; the deletion removes every mapping in it.

Reading the file’s effect through the resolver

A quick check of what the resolver will return for a name is to query it with a tool that uses the system resolver:

ping -4 fileserver

The output shows the address the resolver returned. If it matches the hosts entry, the file is being consulted for that name. If it shows a different address, either the entry is not present, is malformed, or the name in the file does not match the name in the command. The -4 switch forces IPv4 resolution, which is useful when the file contains both IPv4 and IPv6 entries for the same name and the result would otherwise depend on the order the resolver tries them.

For a direct view of the DNS answer without the hosts file, nslookup queries the DNS server directly and does not consult the hosts file. Comparing the two outputs shows whether a name is being answered locally or remotely.

Common questions

Where is the hosts file in Windows?

It is at C:\Windows\System32\drivers\etc\hosts. The path is the same on 32-bit and 64-bit Windows. The directory also holds sample and legacy files, but only the file named hosts affects ordinary name resolution.

Why does my hosts file change not work?

The file is read by the Windows resolver, and some applications resolve names by their own method, such as through a configured proxy or their own DNS library, so they never consult it. A malformed line is also ignored without an error. Testing the same name with ping shows whether the system resolver is returning the entry.

Do I need to flush DNS after editing the hosts file?

Not for names that are listed in the file, because the file is consulted before the DNS client cache. A flush is needed when an entry is removed and the name should fall through to DNS, or when a DNS record has changed and the cached address is stale.

How do I edit the hosts file as administrator?

Open an elevated Command Prompt or PowerShell window and run notepad C:\Windows\System32\drivers\etc\hosts. The editor inherits the elevated token and can save to the protected location. Editing without elevation produces a save error.

Can malware change the hosts file?

Yes. The file overrides DNS without network traffic, so malware sometimes adds entries that redirect security, banking, or update domains to an attacker-controlled address. Entries the user did not add are the sign to look for; removing them restores normal resolution for those names.

Read next