<< Back To All Blogs
Using the Windows hosts file to your advantage
Saturday, June 14th, 2008
This post is a Windows-specific post, but the same concepts can also be applied to almost all distributions of Linux, Mac, and Unix.
First of all, let me say that if you don't understand what I'm talking about, please don't make changes to your hosts file. Doing so in an improper manner can significantly alter system performance and can break your internet connection all together.
As a basic rundown of a hosts file, it tells your computer how to resolve specific hostnames on the computer. This process is used before the computer will attempt to query DNS for a specific IP address, and is a very fast and brute-force way to force resolutions.
The layout of the hosts file is very simple:
Most hosts files will have localhost already defined:
You can also specify multiple hostnames on a single line:
127.0.0.1 localhost localhost2 myotherlocalhostname somedomain.com
Please note that, unlike DNS, the hosts file does not support wildcarding such as *.somedomain.com
The location of this file on most Windows systems is C:\WINDOWS\system32\drivers\etc\hosts (Note that hosts is actually a file, not a directory)
I want to go over 2 very useful uses of the hosts file:
Use 1: Block a computer from accessing specific hosts:
Blocking a computer from specific hosts is a great way to limit ads from annoying companies, or block a user from accessing specific websites, to do so, simple use the following format:
0.0.0.0 annoyinghost.com ads.annoyinghost.com
The format 0.0.0.0 is indeed not a valid IP address entry, but the network subsystem will recognize it as so and no longer attempt to resolve the address. Be careful with using 127.0.0.1 as your IP address for entries (as resolving blocked sites to your local machine will attempt to connect to a service on your local computer on that port). The safest bet is to always block things with 0.0.0.0
Use 2: Using hosts to develop and test network-applications and websites
This is another very useful concept of using host files, especially relevant to website developers. Say you are developing a website for hello.nerdyhearn.com, but you don't want to deploy to the server until the site is fully ready. You can add a resolution for hello.nerdyhearn.com to your hosts file, and have that address resolve to your local webserver for the time being. If you are using Windows Server you can specifically map that address to go to a specific virtual host, so that you won't see the entire wwwroot, such as on Windows XP (an annoying limitation of IIS, but nonetheless, it exists).
The entry would be as follows:
Please note that changing your hosts file is not an immediate change, you either have to reboot your computer, or use IPConfig to flush the dns, the command to do so using IPConfig is:
Hope this helped some of you out, this is a very easy and quick trick that can save you a ton of time!
Tom said on Sunday, June 15th, 2008 @ 8:52 PM
Thanks for the comment, a great suggestion!
ken said on Sunday, June 15th, 2008 @ 8:46 PM
It's no longer maintained, but it still works flawlessly on XP and Vista. Good for quick toggling or modifications.
Add A Comment
Email Address: (not public, used to send notifications on further comments)
Enter the text above, except for the 1st and last character: