Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

The hosts file on Windows computers is used to bind certain name strings to specific IP addresses to override other name resolution methods.

Often, one decides to change the hosts file, and discovers that the changes refuse to take effect, or that even old entries of the hosts file are ignored thereafter. A number of "gotcha" mistakes can cause this, and it can be frustrating to figure out which one.

When faced with the problem of Windows ignoring a hosts file, what is a comprehensive troubleshoot protocol that may be followed?


This question has duplicates on SO, such as hosts file seems to be ignored, HOSTS file being ignored, /etc/hosts file being ignored as well as numerous discussions elsewhere.

However, these tend to deal with a specific case, and once whatever mistake the OP made is found out, the discussion is over. If you don't happen to have made the same error, such a discussion isn't very useful. So I thought it would be more helpful to have a general protocol for resolving all hosts-related issues that would cover all cases.

share|improve this question
4  
Also, as someone posted elsewhere, don't use "nslookup" to test this because that command ignores the hosts file. Rather use "ping". –  LatinSuD Jun 4 at 12:04

4 Answers 4

up vote 24 down vote accepted

Based on my own experience and what I encountered while Googling, here are some things to try:

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

  • Is your hosts file named correctly? It should be hosts and not host, etc.
  • Is the extension correct? It should have no extension (hosts not hosts.txt) - be careful if you have configured windows to hide known extensions, check the properties to be sure: The correct hosts file's type will show up as just "File".
  • Did you follow the correct syntax? Did you accidentally prefix lines with a hash (#) which indicates comments?
  • Did you take care of all variants (www.example.com vs. example.com - safest to just add both)?

3. Whitespace

The format for each line is IP address, then a tab (\t or ASCII HT/0x09) or a single space (ASCII 0x20), 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 hosts entries from somewhere, you may end up with multiple spaces. In theory hosts 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 hosts 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 hosts file. Supposedly, Windows doesn't actually support putting the hosts file in other locations, but you might want to check. The key is:

\HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\DataBasePath

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 hosts 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.

Last point is important: Copying works, moving doesn't.

6. Encoding

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

7. Proxies

If you have a proxy configured, it may bypass the hosts 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

(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 hosts file to work, but it may help in your case if something is configured strangely.

share|improve this answer
    
THANK YOU SO MUCH SUPERBEST ! I'm almost crying, so many ours going crazy for a SPACE at the beggining of a blank entry between 2 lines. Thank you Thank you Thank you Thank you Thank you !!!!! –  Anonymous Jun 26 at 14:13
    
OMG!!! THANKS A TON!!! Been looking for a solution for 3 days now. I added example.com in my hosts file, but it didn't work. When I added www.example.com, it worked instantly! This point may have been the last of the basics but it's the top one in my view. Can't thank you enough for posting this answer. Appreciate it!!! –  Devner Aug 2 at 8:21
    
Another gotcha is that 64-bit versions of Windows redirect the System32 folder for 32-bit applications (such as Notepad++) so when you save the file shows up in the SystemWOW64 folder instead of System32. The best workaround is just use the Windows version of Notepad which is 64-bit. Second best is connecting to the administrative share (\\yourmachinename\c$\Windows\System32\drivers\etc) to open the file if you do use a 32-bit editor. –  Tim Lewis Sep 19 at 14:36
1  
Good post, but with a couple errors. 1) "Automatically detect settings" in IE can lead you to (even unnknowingly) use a proxy, if your network does actually provide proxy autoconfiguration. 2) Changing your DNS servers in the network settings hasn't anything to do with how/when/if the hosts file is used, and changing it to 127.0.0.1 is effectively an error, unless you're actually running a DNS server on your computer. –  Massimo Sep 24 at 21:19
    
@Massimo, Re #1: You're right. Re #2: I actually added due to some personal experience, where a computer with a bunch of strange configurations could be made to use the hosts file with that method. I didn't investigate to see if the DNS address was definitely the problem - but I decided to leave that step in with a note. Feel free to edit the question if you like. –  Superbest Sep 24 at 22:56

Make sure you have put the ipaddress first and then the "domain" like this:

127.0.0.1   bo.dev
127.0.0.1   www.bo.dev
share|improve this answer
    
I prefer to use a single line: 127.0.0.1 bo.dev www.bo.dev ftp.bo.dev –  Xhynk Mar 28 at 16:59

In my cases I tried Windows 7 created file: C:\Windows\System32\drivers\etc\hosts.ics.

hosts.ics:

# This file has been automatically generated for use by Microsoft Internet
# Connection Sharing. It contains the mappings of IP addresses to host names
# for the home network. Please do not make changes to the HOSTS.ICS file.
# Any changes may result in a loss of connectivity between machines on the
# local network.

As the result, Windows ignores settings in C:\Windows\System32\drivers\etc\hosts and uses it from hosts.ics.

share|improve this answer

Please add checking file permissions. I found that even though I had Local Admin rights to the computer and thus to hosts. It was not until I added local users to the hosts file permissions with Read and Read & Execute and then a ipconfig /flushdns That the hosts became active.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.