22

I'm having a hard time geting Windows to take into account a new entry in my hosts file.

I tried adding this line:
199.229.249.151 models.db

To the hosts file found here:
c:\windows\system32\drivers\etc\hosts

When I save the file and try to reach the host with a browser, I'm getting a "host not found" error. I tried setting the "read-only" file attribute to the hosts file -- same result. I tried flushing the DNS cache, but nothing changes.

It seems Windows is not reading my modified hosts file at all, or at least, not taking in account my new entry.

What am I forgetting? What else could I try?

Are there specific requirements or rules to follow to ensure that Windows can always properly detect a change to the hosts file, read and parse its contents, and immediately take into account changes when using a browser or ping to test via the command-line?

fstarnaud
  • 315
  • 1
  • 10
Almaron
  • 3,947
  • 4
  • 22
  • 45
  • Is 199.229.249.151 pingable? Is it on your network? – rene Jul 24 '13 at 19:09
  • The server is on the web. The ip is pingable as you can see yourself. – Almaron Jul 24 '13 at 19:45
  • Works for me...did you start a fresh cmd prompt after saving the hosts file? – rene Jul 24 '13 at 20:02
  • 2
    Is there some firewall on your windows machine? because some firewall may lock the hostfile – camino Jul 24 '13 at 20:18
  • There is DrWeb Firewall that blocks changing the hosts, but I turned that lock off and managed to edit the file. – Almaron Jul 24 '13 at 21:01
  • rene: did. I have even restarted the PC. Nothing's working – Almaron Jul 24 '13 at 21:25
  • Open Notepad > Start Open Notepad > Open as administrator. Save it at some location as ANSI hosts file (Not .txt extension, select all files and name it as hosts) Copy all the hosts files entries and save it. Finally copy the hosts file copied at the desired location say : C:\tempfolder\hosts to c:windows\system32\drivers\etc folder. – RCR May 09 '16 at 04:35

6 Answers6

17

I ran into same issue and after checking lot of things, the issue ended up being the line endings, I had change the line endings to Windows format and it worked.

user3558019
  • 186
  • 1
  • 2
  • 3
    I found that I had to use a tab to separate the IP address and Domain name [111.222.333.444 www.example.com]. Spaces didn't work. I edited with Notepad++. – Anthony Nov 15 '17 at 16:19
  • 3
    What an obscure problem, I had the exact same issue, ended up copying the example line included in the host file, pasting it on a new line and changing the values to preserve whatever spacing was needed. Thanks for the answer. – Nick Sep 13 '18 at 18:58
  • same problem here... i thought everything was well formatted until i opend the file with VS CODE. my ip was in the line above the host name, all spaces where wrong. Dang! come on win!! why are you the way you are :( – Fo Nko Oct 18 '18 at 23:57
  • Not only do the line endings need to be Windows format, but you also need a linebreak after the last entry, or it wont register the last one. (Win10) – Kelly Bang Nov 29 '18 at 03:41
  • For me the problam was that I added "http://" for example: 127.0.0.1 http://example.com when I removed the "http://" it works! 127.0.0.1 example.com – Danny Michaeli Jul 29 '19 at 18:42
  • in my case tabs don't work. I copy the existing example on the host file and remove the first # character. and it works.. – Inovramadani Aug 20 '19 at 09:21
10

I ran into this problem once, The problem is Windows ignored the host file and I fixed it by:

  1. Copy the hosts file from C:\Windows\System32\drivers\etc to somewhere like Desktop
  2. Remove the hosts file there
  3. Copy the copied hosts file back to C:\Windows\System32\drivers\etc

I don't know why but it's fixed.

Hengly
  • 111
  • 1
  • 5
  • 1
    worked like charm. thanks a lot. – Siva-Dev-Wizard Mar 09 '19 at 16:16
  • Don't know why but it worked. Thank ya. – Thiha Sep 09 '19 at 02:14
  • My guess would be permissions -r-xr-xr-x 1 Dec 8 13:32 c:/Windows/System32/drivers/etc/hosts vs -rwxrwxrwx 1 ccang ccang 1050 Dec 8 13:32 hosts* – Chai Ang Dec 08 '20 at 02:38
  • So this was strange, I never saw this before. I added a host to the hosts file. It was perfectly well formed, correct with tab, correct ANSI file type, correct crlf end of line. But the new host entry was not being used, windows was using the outside dns for this one single host but all other hosts in the hosts file, it was using the hosts file! Weird! I did just as Hengly recommended, saved the hosts file on my desktop and then manually moved it back in Explorer and to my amazement that new hostname now works. whew! – Michael Grant Jan 28 '21 at 23:58
  • Same here, @MichaelGrant. I moved it out and back in and it fixed it! – fstarnaud May 13 '21 at 17:55
6

Automatic proxy server configuration scripts override the hosts file. To disable the automatic configuration script:

  1. Press Windows key and type Configure proxy server
  2. Click LAN settings
  3. Uncheck Use automatic configuration script

LAN settings

AndrewRalon
  • 424
  • 1
  • 9
  • 21
3

Open Notepad > Start Open Notepad > Open as administrator.

Save it at some location as ANSI hosts file (Not .txt extension, select all files and name it as hosts)

Copy all the hosts files entries and save it.

Finally copy the hosts file copied at the desired location say : C:\tempfolder\hosts to c:windows\system32\drivers\etc folder.

RCR
  • 449
  • 8
  • 15
2
  1. Try ping localhost.

    if it works, then something wrong with ip or your entry. If it does not, hosts file is bad. Pay attention where it goes. It might try ipv6. That still means that hosts file is broken.

  2. Remove everything from it and leave only your entry or localhost. Single line only, nothing else at all! Not even line breaks. Just stash it aside somewhere until problem is resolved.

    If it works, then there's an entry that breaks things. Try converting line endings to windows format, might help. Usually it's white space that messes with things because it's hard to notice.

Community
  • 1
  • 1
clorz
  • 1,003
  • 2
  • 14
  • 27
0

I encounter the same issue, and find my host is unicode, after change it to ANSI, the issue is fixed