NerdyHearn
Home
Blog

Contact
Mailing List

Software

Active Directory Products
Object Compare
Permission Compare

IPhone Products
Calls To Calendar
SMS To CSV
SMS To Gmail
Voicemail To Gmail

Sites
DocuTerminal
How Long For Me
My Music To Me
SaveMySerials
TypeCount

Blog
Twitter

NerdyHearn - Blog


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

[IPAddress] [hostname(s)]

Most hosts files will have localhost already defined:
127.0.0.1 localhost

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:
127.0.0.1 hello.nerdyhearn.com

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:
ipconfig /flushdns

Hope this helped some of you out, this is a very easy and quick trick that can save you a ton of time!

Tom Out.

Comments

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

http://accs-net.com/hosts/HostsToggle/

It's no longer maintained, but it still works flawlessly on XP and Vista. Good for quick toggling or modifications.

Add A Comment

Name:


URL:


Email Address: (not public, used to send notifications on further comments)


Comments:



Enter the text above, except for the 1st and last character:


NerdyHearn - Latest tech news relating to C#, ASP.NET, SharePoint, PHP, general development, and more. DocuTerminal - Online Filing Cabinet solution. Scan, search and archive your paper documents. SaveMySerials - Protect yourself from theft, fire, natural disasters and more by recording your serial numbers My Music To Me - Stream your subsonic music collection to your Sonos wireless system TypeCount - Count how often you type across different computers! ServrTrackr - Monitor your SSL certificates, website uptime, and DNS configurations