Networking

How-To

Windows: Troubleshooting a non-working Hosts file

Rando

What do you do when your Windows XP computer isn't recognizing the Hosts file? Here are a few possible solutions.

Recently, I ran into this devil of a problem. I wanted to do some internal testing of a Web site, from a browser on a Windows XP machine. So I added an entry in the hosts file on the XP machine that would redirect joabj.com to the internal IP address of the server--"192.168.0.33 joabj.com" in this case (Typically, in WinXP, the Hosts file was located in the C:\WINDOWS\system32\drivers\etc folder). Yet, the browser still returned errors!

Turns out, the browser kept consulting the external DNS service first, and returning the wrong page (my cable modem page in this case).

Most infuriatingly, Windows host file command line tools (namely, ping and SSH) recognized entries, but the browser did not!! If I ping'ed my domain name entered into the Hosts file ("ping -a joabj.com" in this case), it pinged the correct IP number ("reply from 192.168.0.33:" etc...).

Surfing the Web, I came across a number of different solutions to this problem:

*Reboot: Not only rebooting the machine (duh!), but emptying the browser caches, flushing the DNS (from the command line, type "ipconfig /flushdns").

*Extra empty characters in the Hosts file: Evidently, Windows doesn't like an empty space behind the entry, i.e. "192.168.0.33 joabj.com " rather than "192.168.0.33 joabj.com" --make sure you don't add in an empty space.

*Corrupt Hosts file: This could be the case even if it opens in Notepad o.k. Try replacing the existing Hosts file with a new one.

*Specify exact subdomain in Hosts file: This is the solution that ultimately worked for me, after trying all these other more complicated solutions, described below.

In a nutshell, if you plan on using the address "www.YOURDOMAIN.com" you should type "www.YOURDOMAIN.com" into the Hosts file, rather than just "YOURDOMAIN.com".

So, for me, once I replaced "192.168.0.33 joabj.com" with "192.168.0.33 www.joabj.com" then using http://www.joabj.com worked fine, whereas before it wouldn't.

*Editor adding extension to Hosts file name: Sometimes a text editor could add on the .txt to the file name during save, making it Hosts.txt rather than just Hosts. Of course, then Windows won't recognize the Hosts file, and Explorer won't show, by default, the suffixes of file names.

If perusing from Explorer, set the folder view options to show suffixes. From Explorer, go Tools-->Folder Options-->View and uncheck "Hide extensions for known folder types." If Hosts is a .txt, remove the .txt from the file name.

*XP's DNS Cache service taking priority: One troubleshooting site suggested this as a probable cause. It didn't make any difference in my case.

To disable this service, go Start-->Control Panel-->Administrative Tools-->Component Services-->Services(Local). Then search for DNS Cache and disable it. You could just stop the service to check if it has any affect, though it will start up again on reboot. The Manual setting just means that the service will start up, once a browser is fired up. The Disable option turns it off altogether, until you turn it back on again.

According to other people who've tried this, disabling DNS Cache should have no ill-effect on your DNSing.

*Reorder the DNS lookup sequence: Typically, Windows XP will consult the local Hosts file before checking with a DNS server to resolve domain name. But, sometimes not.

You change the order of the lookup in the registry. (STANDARD DISCLAIMER: DO not mess w/ registry until you know what you are doing).

To fire up the registry editor, do Start-->Run and put "regedit" in the box.

Once in regedit, go to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\ Services\Tcpip\ServiceProvider. Once there, you will see a number of entries, including "DnsPriority" "HostsPriority" "LocalPriority" and "NetbtPriority" --which are the entries for DNS-based lookup, Hosts file-based lookup, Computer-based lookup and NetBios-based look-up. (More info on that here).

In the data column for each you see a number in parenthesis. This number is the priority for that lookup. The lower the number, the earlier in the domain name resolution sequence it is consulted (evidently the range is between -32768 and 32767). If the DNS number is lower than the Hosts' number, then you want to give the Hosts number a lower number than the DNS number.

So if DnsPriority is 5000 and Hosts is 7000, you may want to change Hosts to, say, 4500

Keep in mind, that when you set the number, by right clicking on the entry and choosing "modify," you just can't add the number as is -- you will have to enter the new number in either hexidecimal or binary.

One easy way to convert a number into hexadecimal is to call up Windows calculator, switch the view from standard to scientific, then enter the number into the field for entering a number. After the number is entered, look for where "dec" is selected on top of the calculator, and switch that to "hex."

Back