The hosts file is a small text file that is used by the TCP/IP stack of a computer operating system (OS) to configure how that OS resolves hostnames — like www.petri.com — to an underlying IP address (18.104.22.168). So what can you accomplish by editing a hosts file?
If you work in web development, proper editing of a hosts file allows you to redirect someone to a different URL. This might be useful is you have a website under development that exists at domain name that has the same name as an existing website. Some simple editing of the hosts file on a target computer will send the user of that PC to the correct website. I’ll show you the steps needed to accomplish this a bit later in this article.
You can also edit the hosts file to block users from visiting certain websites. Manually editing hosts files for hundreds (if not thousands) of machines isn’t an ideal solution, as there are more effective third-party tools available that can do that more easily. It’s a good skill to learn how to do manually, however, so I’ll show you how to do that as well.
What is in the hosts file?
A hosts file contains text information that maps IP addresses to desired host names. Regardless of whether the hosts file exists on a Windows, Linux, or Macintosh machine, the contents will be the same: IP addresses are listed on the left, followed by the host name that the IP address on that line should be mapped to. If you’re an overly-organized type you can neatly line up your columns of IP addresses and host names, but you don’t need to: As long as the IP address and host name are separated by at least one blank space the mapping will work. You can also insert comments into the hosts file using the ‘#’ symbol, which can be helpful in documenting why changes were made to the host file, especially for someone else who may not be familiar with why the changes have been made.
As an example, here’s a photo showing a sample local hosts file loaded into Windows Notepad on a Windows 7 PC.
Where is the hosts file?
In most Windows OSes, the HOSTS file is located in the following file directory found in the Windows\System32 folder:
Note: The etc folder may be hidden on your system, so you may need to show hidden files and folders. For information on how to do this with Windows 7, see our how-to article on how to display hidden files and folders in Windows 7.
How to edit the hosts file
The hosts file is a simple plain text file, so you can use the default Windows 7 Notepad application to edit the file. I’ll show you how to find, load, edit, and save the file in this section. Scroll down a bit farther in this article to see specific instructions on how to edit a hosts file to redirect URLs or how to block websites.
1. Click on the Start button, then All Programs, and then Accessories. Find the Notepad application.
2. Right-click on the Notepad application icon and select Run as Administrator from the menu that appears (see example below). You may get a User Account Control (UAC) warning after doing so; just click Yes on the windows that appears to accept changes.
Note: To disable UAC, see our article on how to disable UAC in Windows 7.
3. After Notepad has loaded, navigate to the C:\Windows\System32\Drivers\etc directory.
4. If you’re not seeing any files in the etc directory (see below), it’s because all of the files here don’t have any file extensions, and Notepad is only looking for files with .txt extensions. To fix that…
5. …select ‘All Files’ from the drop-down in the lower right corner of the open window. This will then display all the files in the directory, as shown below.
6. Double-click on the hosts file to load it into notepad.
7. Once you’ve made your edits, overwrite the existing hosts file and make sure that you haven’t accidentally added a .txt extension to the file. The hosts file needs to remain exactly that — it should keep with the hosts file name with no extensions whatsoever, or the OS won’t recognize the file.
Editing the HOSTS file to redirect to a specific URL
One of the most popular uses of editing a hosts file is to direct a user to a website that may have a web address that conflicts with an existing one. For example, let’s say you work at a company that makes petri dishes, and your web development team has an internal website that use the web address www.petri.com. Typing that address will normally take the user to the Petri IT Knowledgebase website. In order to redirect the user to your internal website when they type Petri.com, have the user load up the hosts file and make the following edits:
1. At the bottom of the hosts file, add the correct IP address for your internal site followed by at least one space, as shown below.
2. This one-line change will tell the user’s PC to use the new IP address mapping for the Petri.com web address, which will now be redirected to the correct site.
You can also edit a hosts file to give users access to a site that they can’t normally reach by typing in a URL. For example, you may have a beta.petri.com site that shows the design for your new website design, but you want to limit access somewhat. Adding the IP address followed by the beta.petri.com address will allow users who type in beta.petri.com to reach the correct site, but users who don’t have the updated host mapping won’t be able to find the site, unless they know the IP address. You can see that example below as well:
Once you’ve made the required changes, simply save the file (as described in the previous section), reboot the user’s PC, and when the user attempts to access both the normal and beta Petri sites they’ll be routed to the correct locations.
Editing the HOSTS file to block websites
Another popular use for editing the hosts file is to block the user from accessing specific sites. For example, if you want to prevent a user from accessing Facebook on their company-supplied PC, you could follow these steps:
1. Load up the hosts file of the target PC and add the localhost IP address of 127.0.0.1, followed by at least one space, and then the Facebook addresses you want to block, as shown in the example below.
Using the steps outlined here, you now have the knowledge necessary to both redirect someone to a different website and to also block their access to specific websites. We have a number of other how-to guides that cover the same topic for different platforms and operating systems, including articles on how to edit a hosts file using Windows 8 and how to edit a hosts file on a mobile device running Windows RT.