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 being ignored
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.
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:To drop the old data. To check if it works, use (assuming you have an ipv4 entry in your hosts for www.example.com, or an ipv6 entry in your hosts for ipv6.example.com):
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):
You can check the current data in the DNS cache with:
NB:
nslookup
does not look at the hosts file. See NSLOOKUP and NBLOOKUP give one IP address; PING finds another2. Basics
hosts
and nothost
, etc.hosts
nothosts.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".#
) which indicates comments?www.example.com
andexample.com
- safest to just add both)?3. Whitespace
The format for each line is
IP address
, then a horizontal tab (escape code\t
, ASCIIHT
, hex0x09
) or a single space (hex0x20
), then the host name, ie.www.example.com
, then finally a carriage return followed by a line feed, (escape codes\r\n
, ASCIICRLF
, hex0x0d 0x0a
).Sample entries, using Unicode control pictures to indicate control characters. (Don't copy and paste these into your hosts file!)
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:
The entry should be:
Or, in a Command Prompt window, type:
which should display something similar to:
5. Permissions
Sometimes there are issues with permissions on the file, the file attributes, and similar things. To recreate the file with default permissions:
hosts
.%SystemRoot%\System32\drivers\etc
directory, and overwrite the old file.Last point is important: Copying works, moving doesn't.
The local
Users
account must be able to read the hosts file. To make sure (in Windows 7):%SystemRoot%\System32\drivers\etc
in Windows Explorer.hosts
file, ensure you can see hidden and system files.hosts
file and selectProperties
from the context menu.hosts Properties
window, click on theSecurity
tab.Group or user names:
box. If%COMPUTERNAME%\Users
is present, click on it to view permissions.Users
is not present, or is present but does not haveRead
permission, clickEdit...
.Users
is not present, clickAdd...
, typeUsers
, clickCheck Names
, and click OK or press Enter.Users
, and ensureRead & execute
is checked in theAllow
column. Click OK. If aWindows Security
alert box pops up, chooseYes
to continue.hosts Properties
window.Or, in a Command Prompt window, type:
which should display something like:
You should see an
(R)
afterBUILTIN\Users
.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.
If you rely on a proxy to access the web and therefore don't want to disable it, you can add exceptions by going to Internet Explorer -> Internet Options -> Connections -> LAN settings -> Proxy Server / Advanced. Then add your exceptions to the Exceptions text box. e.g.
localhost;127.0.0.1;*.dev
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.
9. .local addresses
If you are using a .local domain entry in the form of myhost.local and it gets ignored please try the following:
even if the www.myhost.local does not exist. Windows somehow does not append its workgroup or localdomain.
10. Line / count limits
(added to this answer to make it visible as it's been mentioned a few times)
Windows hosts file seems to have a line or host limit. If you have more than 150 characters on a line or more than 8 hosts entries for an IP create a new line
e.g. instead of:
Try this:
Make sure you have put the ipaddress first and then the "domain" like this:
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.
In my cases I tried Windows 7 created file: C:\Windows\System32\drivers\etc\hosts.ics.
hosts.ics:
As the result, Windows ignores settings in C:\Windows\System32\drivers\etc\hosts and uses it from hosts.ics.
I was experiencing the same issue, hosts file entries being ignored. I tried everything in this and many other threads with no luck. I figured I'd post what worked for me in case someone else comes across this.
Note: simply opening the hosts file and deleting the content, verifying that it was 0kb and re-adding it did not work. I suspect a permissions issue on the file.
Have spent hours, probably days on this.
IBM Trusteer Endpoint Rapport security can cause this problem.
I had added mysite.com to its list of trusted sites.
I think as part of its security it prevents malware or viruses diverting you to alternative sites.
Took mysite of the list of trusted sites and it no longer checks and "corrects" my hosts file diversion.
Hope that helps somebody resolve their problem.
One problem missed in the discussion above is unqualified names (names that do not include a period) in the hosts file. Your network settings might kick in at that point and automatically add your own domain to the end of an unqualified domain. Consequently the name might not resolve, or worse yet, resolve to a completely different machine.
Make sure that you have entered the values as
IP NAME
, not asNAME IP
.This can happen if there is, say, only one entry in the file, and when you ignore the comment-out text.
In windows, make sure HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\DataBasePath is pointed to the directory of the hosts file.
On my case the issue was that I was copying a hosts file from an encrypted directory and overriding the etc/hosts with that. I had to look at properties and uncheck the "encrypt file" checkbox.