Troubleshooting guide for Windows hosts file

This post was most recently updated on November 9th, 2022.

4 min read.

This post describes how to fix possible issues with the Windows hosts file. I’ll first document how to find the damn file, and then go through a bunch of options on what to check and how to fix different possible issues.

Opening the hosts -file

First of all, the host’s file location is usually: C:\Windows\System32\drivers\etc\hosts (link may or may not work depending on your security settings and browser version, but you can always copy-paste it to your text editor’s “Open”-dialog… :)

Editing the file requires elevated privileges for the text editor process, so you’ll need to run it as an administrator.

Troubleshooting

This has mostly been adapted from here: http://serverfault.com/questions/452268/hosts-file-ignored-how-to-troubleshoot.

Time needed: 15 minutes

How to debug hosts file changes not applying?

  1. Did you check that it works correctly?

    Changes to hosts should take effect immediately, but Windows caches name resolution data so for some time the old records may be used. Open a command line (Windows+R, cmd, Enter) and type:
    ipconfig /flushdns

    To drop the old data. To check if it works, use (assuming you have an entry in your hosts for www.example.com):
    ping www.example.com -n 1

    And see if it uses the correct IP. If yes, your hosts file is fine and the problem is elsewhere.
    Also, you can reset the NetBios cache with (open the console as an admin or it will fail):
    nbtstat -R

    You can check the current data in the DNS cache with:
    ipconfig /displaydns | more

  2. Basics

    Let’s check the basic stuff for issues next:

    1) Is your host’s file named correctly? It should be hosts and not host, etc.
    2) Is the extension correct? It should have no extension (hosts and not hosts.txt). Be careful if you have configured windows to hide known extensions, check the properties (right-click -> “Properties”) to be sure: The correct host file’s type will show up as just “File”.
    3) Did you follow the correct syntax?
    4) Did you accidentally prefix lines with a hash (#) which indicates comments?
    5) Did you take care of all variants/subdomains (www.example.com vs. example.com)? It is safest to just add both – really doesn’t hurt.

  3. Whitespace

    Be mindful of whitespace in the file!

    The format for each line is IP address, then a tab (\t or ASCII HT/0x09) or a single space (ASCII0x20), then “CRLF” – a carriage return character followed by a line feed character, so each line terminates with 0x0d0a.

    The individual bytes may be viewed in Notepad++ with the hex editor plugin. Notepad++ will also show special characters (View -> Show Symbol) so you can easily inspect the number and kind of whitespace characters.

    If you copied and pasted host entries from somewhere, you may end up with multiple spaces. In theory, the file supports multiple spaces separating the two columns, but it’s another thing to try if nothing else works.

    To be on the safe side, make sure all lines in your host’s file either use tabs or spaces, not both.
    Lastly, terminate the file with a blank line.

  4. Registry Key

    There is a registry key specifying the location of the host’s file. Supposedly, Windows doesn’t actually support putting the host’s file in other locations, but you might want to check. The key is:
    \HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\DataBasePath

    The value of the entry should be:
    %SystemRoot%\System32\drivers\etc

  5. Permissions

    Sometimes there are issues with permissions on the file, the file attributes, and similar things. The easy way to deal with this is:
    1. Create a new text file on your desktop.
    2. Copy and paste the contents of your current host’s file into this file in Notepad.
    3. Save the new text file and rename it to hosts.
    4. Copy (do not move) the file to your drivers/etc directory, and overwrite the old file.

    The last point is important: Copying works, moving doesn’t. By trying to open the file, you can verify you have at least the read access to it – that’s required in order for the file to work for you.

    To fix (some) permission issues, you can try to copy the contents of the file, completely remove the file, then create a new file (type “File”, without an extension) and paste the contents to it. That could fix some of the weirder permissions issues.

  6. Encoding

    The host’s file should be encoded in ANSI or UTF-8 without BOM. You can do this with File -> Save As.

  7. Proxies

    If you have a proxy configured to your local machine, it may bypass the host’s file. The solution is to not use the proxy or configure it to not do this.

    To check, go to your Internet Explorer -> Internet Options -> Connections -> LAN settings. If everything is blank and “Automatically detect settings” is checked, you aren’t using a proxy.

  8. DNS address

    Is it always DNS? It pretty often is.

    (This may also resolve proxy issues.)

    Go to your network connections properties, then TCP/IP settings, and change the first DNS server to 127.0.0.1 (localhost). The second should probably be your actual DNS’s IP.

    This is not necessary for the host’s file to work, but it may help in your case if something is configured strangely.

mm
5 2 votes
Article Rating
Subscribe
Notify of
guest

4 Comments
most voted
newest oldest
Inline Feedbacks
View all comments