Edit Your Computer's HOSTS File

Posted under Computers on November 19, 2008.

Remove unnecessary DNS lookups, rename your localhost, or simply screw up your DNS. Do any of these easily by editing a text file found on all windows machines.

First thing's first. You have to locate and open up a file called HOSTS (no file extension) on your computer. Usually this file can be found below on your PC:

C:/WINDOWS/SYSTEM32/DRIVERS/ETC

Backup a copy of the file for safekeeping and open the original file up with your favorite text editor and you are ready to get go. The content of your hosts file should look 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 denoted by a '#' symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1 localhost

1. Remove Unnecessary DNS Lookups

Every time you visit a website your computer has to translate the website's domain name (EX: evanbyrne.com) into an IP address so it can start communications. But before your computer sends a query to a DNS (domain name server) asking for the appropriate IP address of the website it looks in your hosts file to see if a match is there first. If a match for that domain is found in your hosts file then your computer doesn't have to waste time and resources looking up that domain name on the internet.

To add the google.com domain's IP to your hosts file simply add this code to the file:

209.85.175.147 www.google.com

So now when you visit www.google.com in your web browser your computer will bypass DNS because it found google.com in the hosts file.

NOTE: www.google.com is considered different than google.com.

2. Rename Your localhost

If you code web applications on a regular basis you probably develop your applications and test them on your local machine. Ever wished you could test applications at a URL like http://www.somedomain.com rather than http://localhost? No? Well for some odd reason I have so I might as well tell you how I changed it.

Let's say you would much rather test your applications at the imaginary domain http://www.testing.com. Add the following code to your hosts file:

127.0.0.1 www.testing.com

Now if you visit www.testing.com it should bring up your localhost. The IP Address 127.0.0.1 is called your computer's loopback address. If you visit the IP Address 127.0.0.1 in your web browser it will bring up your localhost. Coincidentally, if you visit 127.0.0.1 on someone else s computer it will bring up his/hers localhost.

Last Thoughts

If you make www.testing.com your localhost then remember you can't visit the actual www.testing.com so keep that in mind. The hosts file controls domain name translations so if anything ever goes haywire with domain name translations that should be the first place to look for the problem. Conversely, it is the first place to go to screw up DNS too... but I wouldn't do that if I was you. ;-)

Comment

See what others have to say on this topic, or add your own two cents.