Hosts File

From Lankyland

Jump to: navigation, search

Contents

What is the hosts file

The hosts file is a computer file used by an operating system to map hostnames to IP addresses. This method is one of several methods used by an operating system to locate network nodes on a computer network. On many operating systems, the host file content is used preferentially over other methods, such as the Domain Name System (DNS). Unlike DNS, the hosts file is under the control of the local computer's administrator.

Here is an example of a hosts file from a Windows Vista computer

Why use it?

You can use the hosts file for a multitude of purposes. The most common is to block malicious websites and ad servers.

Speeding Up Website Access

When you enter a domain name into a browser there can be a delay in the loading of the content because the following process happens for most home and business users.

Enter Address in Browser -> Browser Queries hosts file -> If no results found it then queries the Network router -> If no results found it then queries the ISP's Nameservers -> If no results found it then queries the Root Nameservers. At this point if no result is returned then we can assume the domain does not exist.

As you can see, it is a fairly lengthy procedure. By adding records to the hosts file we cut out a lot of that messing around.

It is not possible to do this for some sites. Sites like Google use load balancing technology which means that you can browse to google.com and it will actually have multiple IPs. In that case using the hosts file can actually slow down the process.

Warning: if the site you add to your hosts file changes its IP address at all then you will not be able to access it until you either update the IP address in your hosts file or remove the entry in the hosts file.

Sandbox Development

If you have a development server and you are wanting to test it out using your domain name you have two options. You can modify your DNS Records and point your domain name at your dev server or you can modify the hosts file so that when you enter the domain name into a browser on your computer you get the dev server but everyone else on the internet still sees your production server.

This also works if you do not have a domain name registered yet.

The process is simple. Determine the IP Address of your dev server and then add a record for it in your hosts file.

 192.168.0.100 domain.com

Any requests for domain.com on the computer with the modified hosts file redirects all requests to your dev server.

Granted it is possible to work on the dev server by using its IP Address directly but some testing may require using the actual domain name.

Blocking Websites

To block a website we tell the hosts file that all requests for the domain should be directed to 127.0.0.1

 127.0.0.1 adserver.com

That makes it so that if there are any links for content on the adserver.com address will actually try to request the data from 127.0.0.1 and not the actual server. This will also speed up the loading of the page as your browser doesn't have to wait around for the adserver.com content to be downloaded.

Blocking Facebook

 127.0.0.1 facebook.com
 127.0.0.1 www.facebook.com
 127.0.0.1 apps.facebook.com

If you want to block just the Advertising on Facebook you can add this line to your Hosts_file:

 127.0.0.1 creative.ak.facebook.com
 127.0.0.1 ads.ak.facebook.com

Windows

Windows 2000

  1. Open My Computer
  2. Browse to C:\WINNT\SYSTEM32\DRIVERS\ETC
  3. Right-click on 'hosts' and Click Open
  4. Select Notepad to Open the file
  5. On a new line at the bottom of the file add the info in this order: IP Domain
  6. Close and Save when prompted


Windows XP

  1. Open My Computer
  2. Browse to C:\WINDOWS\system32\drivers\etc
  3. Right-click on 'hosts' and Click Open
  4. Select Notepad to Open the file
  5. On a new line at the bottom of the file add the info in this order: IP Domain
  6. Close and Save when prompted

Windows Vista

  1. Browse to Start -> All Programs -> Accessories
  2. Right click "Notepad" and select "Run as administrator"
  3. Click "Continue" on the UAC prompt
  4. Click File -> Open
  5. Browse to "C:\Windows\System32\Drivers\etc"
  6. Change the file filter drop down box from "Text Documents (*.txt)" to "All Files (*.*)"
  7. Select "hosts" and click "Open"
  8. On a new line at the bottom of the file add the info in this order: IP Domain
  9. Make the needed changes and close Notepad. Save when prompted.

Mac OSX

  1. Browse to Finder -> Applications -> Utilities -> Terminal
  2. Type in sudo nano /etc/hosts
  3. Enter your Root (Admin) password
  4. On a new line at the bottom of the file add the info in this order: IP Domain
  5. Hit Control + O and then hit Enter to save with the existing filename
  6. Hit Control + X to close the file

Linux

  1. Open a Console
  2. Type in sudo nano /etc/hosts
  3. Enter your Root (Admin) password
  4. On a new line at the bottom of the file add the info in this order: IP Domain
  5. Hit Control + O and then hit Enter to save with the existing filename
  6. Hit Control + X to close the file

Sites you can put in your hosts file

Follow the instructions above and on a new line at the text in this format

Example:

 96.57.182.142 4thkingdom.com
 67.205.53.106 lankyland.com
Personal tools
Donate
Donate towards my web hosting bill!