Working with the Hosts File on Windows XP

You would be amazed at the number of things you can do with the Hosts file on Windows XP. This file is most commonly used to resolve IP addresses; from this ability springs a variety of uses. In this article we'll take a close look at what else the hosts file can do for you.

Contributed by
Rating: 5 stars5 stars5 stars5 stars5 stars / 11
August 13, 2009
Rate this Article:
MEH MEH++


SEARCH ASP FREE
TOOLS YOU CAN USE

advertisement

There are actions that we perform every day automatically when surfing the web, and very often we don't ask ourselves what we're actually doing. For example, we type the URL of our preferred website into the address bar of our browser, and if all goes well, the site is displayed on our computer's screen. But what's going on behind this process?

Well, what the browser does is attempt to establish a connection to the computer which is currently hosting the website that we want to access. But for this to be done successfully, behind the scenes our computer is running some tasks that are worth describing in more detail. In the previous example, assuming that we're performing a search through Google.com, our browser tries to connect to a host with the IP address 74.125.45.100 (assuming that the folks at Google haven't changed this by the time of this writing).  

Naturally, to resolve the domain google.com to this specific IP address, our computer first determines if the address is not its own local IP address (the so-called loopback, usually identified as 127.0.0.1). If this isn't the case, it attempts to perform the resolution process using a local text file named HOSTS, and then it looks at the DNS server of the ISP; finally, it looks sequentially at the DNS servers that are available online.

As I mentioned before, the earlier IP resolution process involves a local text file named HOSTS, which in the case of Windows XP is usually located under the C:windows system32driversetc folder. In simple terms, the HOSTS file acts like an address book in which it is possible to associate a domain name to a specific IP address, which can be used for multiple purposes, including blocking unwanted hosts, speeding up navigation across websites, and even creating neat shortcuts to them.

Of course, the HOSTS file can not be considered a panacea, but due to its considerable functionality, it deserves a deeper analysis. Therefore, in the lines that follow we will explore together the potential uses of this file in specific cases, so you can utilize it for fitting your own needs. Let's get started right now!

Mapping host names to IP addresses

 

As I explained in the introduction, we can access the HOSTS file on our computer very quickly and easily. But wait a minute! Where can this file be found? Well, in the case of Windows XP, it's usually located in the following directory: C:windowssystem32driversetchosts. Moreover, the HOSTS file is simply a text file without a extension, not hidden, and also writable, so we can easily edit it with any text editor. Basically, when you open this file, you'll see something like this:

# Copyright (c) 1993-1999 Microsoft Corp. # # This is a sample HOSTS file used by Microsoft TCP / IP for Windows. # # This file contains the mappings of IP addresses to host names. Each # Entry should be kept on an individual line. The IP address should # Be placed in the first column followed by the corresponding host name. # The IP address and the host name should be separated by at least one # Space. # # Additionally, comments (such as these) may be inserted on individual # Lines or following the machine name denoting by a '#' symbol. # # For example: # # # Source server 102.54.94.97 rhino.acme.com 38.25.63.10 x.acme.com # # x client host

127.0.0.1 localhost

 

As seen above, the HOSTS file contains a set of comments which show how to resolve a domain to a specific IP address. The syntax to perform this task is extremely simple to grasp. First, the IP address must be entered, followed by at least one blank space, and then the host name to which we want to hook.

So, when we code an entry in this file, what we're really doing is preventing our computer from wasting time by asking an external DNS server to solve a domain name to an IP address. Since this process is resolved in the HOSTS file on our local computer, the whole process is subtly faster.

Also, it's possible to assign multiple domain names to a single IP address. For instance, if we'd wanted to block a few unwanted domains, our HOSTS file would look as follows:

127.0.01 forbidden.domain1.com 127.0.0.1 forbidden.domain2.com 127.0.0.1 forbidden.domain3.com

 

In this particular example, if our computer sends out a request to any of the domains listed above, the request would be redirected straight to the local IP of our machine, thus blocking all of these domains.

In this case, the domains blocked are purely fictitious. However, it's feasible to block real host names. One possible use of this feature would be to implement a simple blocking mechanism that blocks unwanted advertising when surfing the web. In the next section we'll see an example showing how to do this in a simple way. 

Now, click on the link below and keep reading, please.

Blocking unwanted advertising

   

As we know, the Web is riddled with advertising, which is often troublesome and irritating. The question that comes up here is: how can we block unwanted advertisements through our HOSTS file? Well, it's very easy indeed. If we know the domains from which certain ads are served, then we could set up the file in question as follows:

127.0.0.1 ads.albawaba.com 127.0.0.1 ads.casinocity.com 127.0.0.1 ads.movieweb.com 127.0.0.1 promos.newsok.com 127.0.0.1 googleadsense.ya.com

 

In the above example, any request to the specified advertising servers will be redirected to our local machine. Of course, this technique has serious and obvious restrictions and limitations, but it clearly shows another possible use of the HOSTS file.

In addition to blocking certain basic domain names, the HOSTS file has another interesting application. Since it's the first resource used by our Windows machine to resolve domains to IP addresses, this implies that it can be used to speed up the access to a specific host. For example, by placing in the file the IP addresses of sites that we visit on a regular basis, our computer won't have to ask a DNS server for the IP of those sites, thus accelerating the whole navigation process.

Below, we're going to develop an example that allows you to understand more easily how to configure the HOSTS file to speed up the whole web browsing process. 

 Another possible use of the HOSTS file: speeding up browsing the Web

As I already mentioned, when surfing the Web our local machine first tries to resolve a domain name to an IP address via the HOSTS file. So, by simply adding a number of entries to that file, we can preventing our computer from performing the extra step of trying to find the IP address in a remote DNS server. In this way, we'd be saving a few milliseconds when trying to access a particular web site.

The number of entries that can be added to the HOSTS file to map IP addresses is only limited by the size of the file itself, and obviously we should take into account that the IP addresses entered into the HOSTS file can become outdated, as some of the sites included in our list change them from time to time.

Quite possibly, the simplest way to verify that a site has not changed its IP address is by opening a command console in Windows and pinging that site. Not too practical, but definitively simple. For example, if we wanted to optimize the access to some of the sites of the Developer Shed network, as well as to some popular search engines, our HOSTS file could be configured as follows:

 

66.230.239.174 devshed.com 66.230.239.174 devarticles.com 66.230.239.174 codewalkers.com 74.125.45.100 google.com 209.131.36.159 yahoo.com

 

Really simple to code and read, isn't it? Besides, we can add all the entries we want to map, only limited by the size of the file. In addition, the HOSTS file can be used as an address book that contains abbreviations or shortcuts to specific IP addresses. For example, in the previous case we could specify the string "dev" to be resolved to the IP address 66.230.239.174, in this manner implementing a basic shortcut system.

Of course, the best way to understand how this shortcut system could work is through a practical example. So, in the latter part of this tutorial we're going to set up that example in a clear and simple manner. 

Now, go ahead and read the upcoming section.

Using shortcut navigation

As we know, typing the full addresses of sites that we visit is often tedious. The autocompletion features incorporated into modern browsers undoubtedly help to reduce this problem considerably, but the HOSTS file can complement these features quite decently. How can we save time and effort when writing the full name of a website over and over again?

We can do it simply by using abbreviations or reference names that map to IP addresses. To do that, we'd only open the HOSTS file and examine the entries we have included, finding the IP address that we're interested in mapping. Then, we'd change the domain name associated with that IPs by a string shorter and easier to remember.

For example: if we use google.com and do not want to write its full name, we can simply associate the word "gog" to its IP address. In this way, each time we enter that word in the address bar of the browser, Google will appear automatically on our screen. Here are some examples that show how to use the HOSTS file as a basic shortcuts book:

 

66.230.239.175 dev 74.125.45.101 gog 209.131.36.159 yah

 

Not too bad, eh? Of course, we can continue to associate words or abbreviations with certain IP addresses, which not only allows us to save time when typing URLs, but subtly increases the speed of navigation. Finally, it is valid to clarify that the association of IP addresses with certain shortcuts will work only with dedicated servers.

 Final thoughts

 In this tutorial, we've seen some simple uses of the HOST file in Windows XP, which can certainly be trivial for advanced users. However, it's fair to mention some of its benefits. The first one is that the file uses very few resources. It runs as an integrated function of our local computer, and therefore there's no need to use other programs that consume memory and processor cycles.  

The second benefit is that the HOSTS file maps IP addresses with any protocol that we're currently using, be it HTTP, FTP, and so forth, which makes it a pretty flexible tool as well. So, now that you have a clear idea of how to use the HOSTS file in Windows XP, go ahead and give it a try!

blog comments powered by Disqus
WINDOWS SECURITY ARTICLES

- Botnet Malware Sleeps Eight Months Activatio...
- Windows Media Player Vulnerability, PCAnywhe...
- Solera Networks Threat Predictions for 2012,...
- ESET Windows Predictions, January Patch Tues...
- Windows Store: A Closer Look
- McAfee Identifies Threats for Windows 8 in 2...
- Automatic Updates Coming to Internet Explore...
- Microsoft`s December Patch Tuesday Fixes 17 ...
- Symantec: Spam Hits Three Year Low
- Latest Microsoft Security Essentials Public ...
- 25 Passwords to Avoid to Thwart Hackers
- Microsoft Seeks Beta Testers
- Windows 8 Promises Less Painful Security Upd...
- How to Detect and Fix an Infected PC
- Windows 8 Security Flaws

ASP Web Hosting ASP.Net Web Hosting Windows Web Hosting
 
 
 

ASP Free Forums 
 RSS  Tutorials RSS
 RSS  Forums RSS
 RSS  All Feeds
Site Map 
Request Media Kit
Write For Us Get Paid 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Privacy Policy 
Support 


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap
Most Popular Topics
All ASP.Net Tutorials