202

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.

Superbest
  • 5,045
  • 3
  • 14
  • 11
  • 22
    Also, as someone posted elsewhere, don't use "nslookup" to test this because that command ignores the hosts file. Rather use "ping". – LatinSuD Jun 04 '14 at 12:04
  • Possible duplicate of [HOSTS file being ignored](https://serverfault.com/questions/50934/hosts-file-being-ignored) – 030 Jun 17 '19 at 07:37
  • 2
    I found solution at last.. In chrome, `chrome://flags/` ->Secure DNS lookups -> disable. – Ünsal Korkmaz Feb 05 '20 at 12:46
  • @ÜnsalKorkmaz As of April 2021 that flag seems to be unavailable, but `chrome://net-internals/#sockets` -> Flush Socket Pools worked for me. (But I need to repeat it, or else restart Chrome, every time I change the hosts file.) – Phasma Felis Apr 11 '21 at 21:35

13 Answers13

290

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 ipv4 entry in your hosts for www.example.com, or an ipv6 entry in your hosts for ipv6.example.com):

ping -4 www.example.com -n 1
ping -6 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

NB: nslookup does not look at the hosts file. See NSLOOKUP and NBLOOKUP give one IP address; PING finds another

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 and example.com - safest to just add both)?

3. Whitespace

The format for each line is IP address, then a horizontal tab (escape code \t, ASCII HT, hex 0x09) or a single space (hex 0x20), then the host name, ie. www.example.com, then finally a carriage return followed by a line feed, (escape codes \r\n, ASCII CRLF, hex 0x0d 0x0a).

Sample entries, using Unicode control pictures to indicate control characters. (Don't copy and paste these into your hosts file!)

192.0.2.1␉www.example.com␍␊
2001:db8:8:4::2␉ipv6.example.com␍␊

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

Or, in a Command Prompt window, type:

reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters -v DataBasePath

which should display something similar to:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters
DataBasePath    REG_EXPAND_SZ    %SystemRoot%\System32\drivers\etc

5. Permissions

Sometimes there are issues with permissions on the file, the file attributes, and similar things. To recreate the file with default permissions:

  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 %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):

  1. Navigate to %SystemRoot%\System32\drivers\etc in Windows Explorer.
  2. If you can't see the hosts file, ensure you can see hidden and system files.
  3. Right-click on the hosts file and select Properties from the context menu.
  4. In the hosts Properties window, click on the Security tab.
  5. Examine the list of names in the Group or user names: box. If %COMPUTERNAME%\Users is present, click on it to view permissions.
  6. If Users is not present, or is present but does not have Read permission, click Edit....
  7. If Users is not present, click Add..., type Users, click Check Names, and click OK or press Enter.
  8. Select Users, and ensure Read & execute is checked in the Allow column. Click OK. If a Windows Security alert box pops up, choose Yes to continue.
  9. Click OK to close the hosts Properties window.
  10. Go up to section 1 of this answer and follow the directions to check if it's working now.

Or, in a Command Prompt window, type:

icacls %SystemRoot%\System32\drivers\etc\hosts

which should display something like:

C:\WINDOWS\System32\drivers\etc\hosts NT AUTHORITY\SYSTEM:(F)
                                      NT AUTHORITY\SYSTEM:(I)(F)
                                      BUILTIN\Administrators:(I)(F)
                                      BUILTIN\Users:(I)(RX)
                                      APPLICATION PACKAGE AUTHORITY\ALL APPLICATION PACKAGES:(I)(RX)
                                      APPLICATION PACKAGE AUTHORITY\ALL RESTRICTED APPLICATION PACKAGES:(I)(RX)

You should see an (R) after BUILTIN\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:

x.x.x.x myhost.local www.myhost.local

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:

1.2.3.4 host1.com host2.com host3.com host4.com host5.com host6.com host7.com host8.com host9.com 

Try this:

1.2.3.4 host1.com host2.com host3.com host4.com host5.com
1.2.3.4 host6.com host7.com host8.com host9.com
 
Superbest
  • 5,045
  • 3
  • 14
  • 11
  • 3
    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 !!!!! –  Jun 26 '14 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 02 '14 at 08:21
  • 1
    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 '14 at 14:36
  • 4
    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 '14 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 '14 at 22:56
  • V.nice. One could expand with a small section on the function of the NBT node type for netbios enabled computers. Specifically the order of processing of name resolution mechanisms within each node type could be informative, as they each include hosts file processing down the line: http://www.networkuptime.com/archives/2001/08/troubleshooting.html – ErikE Apr 24 '15 at 19:22
  • In my case domain.dev or domain.local would work, but not domain.com or domain.in or domain.org etc. :-) – pinkal vansia Jun 08 '15 at 11:08
  • Regarding "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\" the problem for me was that not only "DataBasePath" was missing, but several other entries too. Copied those from another machine and the problem was fixed. – Larsen Nov 20 '15 at 11:38
  • Under Win10 I had the problem that I copied the new hosts file over the old one. So the permissions were different. I copied another file from this folder, renamed it and it works! – Ron Nov 27 '15 at 15:54
  • 1
    #5 (creating a new file and copying it) did the trick for me. Win7 can be funny with permissions. – chris Jun 10 '16 at 09:48
  • I tried creating a new one and copying in place of the old as described above, but my Windows 10 Pro still didn't pick up the HOSTS file. I fixed by: 1) Disable virus protection. 2) Copy the contents of the existing HOSTS file, store somewhere safe. 3) Open "cmd" as Administrator. 4) del %SystemRoot%\System32\drivers\etc\hosts 5) notepad %SystemRoot%\System32\drivers\etc\hosts and put in some entries 6) When saved, it might append .txt to the file. So, rename %SystemRoot%\System32\drivers\etc\hosts.txt %SystemRoot%\System32\drivers\etc\hosts 7) Confirm works. 8) Re-enable virus protection. – Crash Override Jun 10 '16 at 17:01
  • #5 Copy/Replace - the check marks were there, but grey in the old file, The new file had black checks, and actually worked. – tahwos Apr 01 '17 at 19:34
  • Proxy was the issue for me. Well documented troubleshooting guide/steps. Thanks – Tinashe Chinyanga Jun 27 '17 at 10:43
  • 1
    I edited the hosts file under Cygwin using vim, I think this caused the issue for my hosts file to stop working. I had to create a new file and copied the contents of the previous hosts file and that fixed it. – markphd Aug 21 '17 at 13:08
  • 3
    You can add to this that there is a line length limit. I'm not sure what the limit is, but my line was 275 characters long (16 hostnames) and the last few hostnames were not registered. When I split them into two lines, they all registered correctly. – Courtney Miles Nov 20 '17 at 05:45
  • My gotcha: i had `[name] [ip]` rather than `[ip] [name]` – Isaac Mar 31 '18 at 09:48
  • If I don't have the right to change hosts file, what should I do ? Change `/etc/hosts` doesn't work at all. – Mithril Oct 17 '18 at 08:39
  • As a bottomline - you may check value of registry key from command line using the next command: `reg query HKLM\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\ -v DataBasePath ` – dmikam Feb 08 '19 at 10:50
  • I added item #10 about it not work with IPs that won't route. That was super annoying. I really don't care if it routes, but don't give me an error that you can't resolve the alias! I'll deal with unreachable errors later once you've verified that you're (Windows) giving me the IP I mapped out for you. – sabujp Mar 25 '19 at 06:26
  • Item #8 worked for me, in spite of what's been said in comments. My Hyper-V vEthernet (Default Switch) DNS settings were set to manual, but empty values. After I changed them to 127.0.0.1 and 1.1.1.1, ping started to respect my hosts file. And event after I changed the DNS settings back to empty values, ping continued to work correctly. Probably changing the DNS settings forced something to be reset (but ipconfig /flushdns did not work initially). – Alex Che Sep 16 '20 at 13:43
  • WIWLF(What I was looking for) – Timo Nov 04 '20 at 11:53
  • A similar issue to proxies are VPNs. – Nosajimiki Feb 22 '21 at 15:11
  • After wasting 4 hours of my life on this, #5 was the one to solve it – dimplex Apr 10 '21 at 21:09
  • Thanks very useful answer, However I think Item #6 is not correct. Please refer https://stackoverflow.com/a/70155449/2513010 . – fliedonion Nov 29 '21 at 13:32
  • Thanks, thorough answer. No. 6 did the job for me. FYI in Powershell from an elevated PS prompt you can `cat $env:SystemRoot\System32\drivers\etc\hosts|out-file c:\tmp_hosts -encoding utf8` then copy it back over the original. – Adam D. Mar 30 '22 at 00:41
  • Thought No. 6 did the job for me but actually it was because I was using WSL to test lol. It didn't work actually, I figured out how to fix it using prefix policies and added it in an answer below. – Adam D. Mar 31 '22 at 12:32
  • No. 6 is incorrect. `UTF-8 without BOM` is probably invalid. use `ANSI` or `UTF-8 WITH BOM`. Here is my verification result. https://stackoverflow.com/a/70155449/2513010 – fliedonion Apr 04 '22 at 14:30
21

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
slm
  • 7,355
  • 16
  • 54
  • 72
Gustaf
  • 211
  • 2
  • 2
  • 8
    I prefer to use a single line: `127.0.0.1 bo.dev www.bo.dev ftp.bo.dev` – Xhynk Mar 28 '14 at 16:59
  • 11
    Careful, the windows hosts file only allows NINE OR LESS entries per line! This bit me and took a while to figure out. – andrew Jun 05 '18 at 03:03
  • 5
    Do not use the `.dev` TLD locally thinking that you will get no collisions. It is a valid generic global TLD owned by Google, and you will get a lot of problems using it. See https://ma.ttias.be/chrome-force-dev-domains-https-via-preloaded-hsts/ – Patrick Mevzek Sep 05 '18 at 15:37
  • Rookie mistake, but it worked. Doh! had them backwards name then IP – ransems Jul 08 '19 at 18:11
13

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.

David Siviour
  • 131
  • 1
  • 2
  • 3
    That was it! I've been baffled by this problem for _years_. I added `(machine)\USERS` with **Read** and **Read & execute** permissions and after `ipconfig /flushdns` it worked. Thank you so much. I am very pleased to finally have my hosts file back. – Clement Cherlin Jun 10 '15 at 15:37
10

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.

Unick
  • 201
  • 2
  • 2
  • 2
    From https://support.microsoft.com/en-us/kb/309642/en-us, which is old so maybe no longer accurate: *"The Hosts.ics file is used by ICS to store information about dynamically configured clients. Avoid making changes to this file to prevent the loss of connectivity or data. The Hosts file (with no file name extension) is the file to which you add information about statically configured clients."* So, I'd NOT expect Windows to ignore the `Hosts` file just because `Hosts.ics` is present. Something else might be causing Windows to ignore your Hosts files, like maybe a wrong (but hidden) extension. – Arjan Jun 21 '15 at 13:07
  • 1
    I see this information. But I don`t know, why I was able to make hosts work only I edited hosts.ics. Before to edit hosts.ics, I tried all methods from this topic, but hosts does not work. – Unick Jun 22 '15 at 10:06
  • Thanks! You saved my life. Worked on Windows 10 – Xatenev Jan 31 '19 at 23:58
4

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.

  1. Open Windows Explorer as Administrator
  2. DELETE the hosts file
  3. Open notepad as Administrator and create a new hosts file
  4. Start from scratch and add entries.
  5. Ensure the hosts file doesn't have a .txt extension

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.

PSA4444
  • 191
  • 5
  • quick note - when modifying system files (such as the hosts file), it is prudent to create a backup first. To avoid loss of data, instead consider renaming the existing hosts file, like to `hosts.bak`, and then proceeding as you describe in step 3. – nshiff Jul 12 '19 at 15:06
1

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.

1

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.

user287479
  • 11
  • 1
1

Make sure that you have entered the values as IP NAME, not as NAME IP.

This can happen if there is, say, only one entry in the file, and when you ignore the comment-out text.

knb
  • 151
  • 5
0

Make sure that it whatever application is ignoring the hosts file is actually using Windows' DNS cache in the first place. For instance, the Chrome browser respects the hosts file, but has its own DNS cache that must be flushed separately from Windows' if the hosts file is updated.

Chris
  • 113
  • 4
0

Post-8.1 Windows defaults to localhost resolution within the DNS stack itself. The reason you get IPv6 is because MS implemented default address selection via RFC 3484.

You can override the preference for IPv6 via prefix policies.

Elevated PS> netsh interface ipv6 add prefixpolicy ::ffff:0:0/96 precedence=51 label=4 store=persistent

See my blog post: Defaulting Windows Loopback to IPv4

Update (04/01/2022): I originally posted "set" but you need to "add" the prefix policy first.

Adam D.
  • 119
  • 3
0

In windows, make sure HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters\DataBasePath is pointed to the directory of the hosts file.

Geln Yang
  • 101
  • 2
0

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.

Janos
  • 1
0

My hosts file is ignored until I have a network connection on my (Windows 7) laptop. Once I connect to a network, everything works as expected. Obviously, without a network I can't ping hosts etc. but I still expected Windows to get their IP addresses from the hosts file. It doesn't. Even after disconnecting it still looks up addresses OK, but between restarting the computer and first connecting to a network, it ignores the hosts file.

(Incidentally, tabs, spaces and no carriage returns are all irrelevant as far as I can tell).

Denis Howe
  • 121
  • 3