Tuesday, April 3, 2018

Hosts File

When you try to access any website, your computer takes a look at your hosts file before making the request. A hosts file maps a hostname like www.google.com, to an ip address like 172.217.14.68. That ip address is necessary for a computer to access the correct servers and get the web page you are looking for.

A hosts file is a map of a hostname to ipaddress that you can modify yoursel. The ipaddress that is specified in your hosts file takes precedence over DNS lookups(another way in which your computer gets an ipaddress from a hostname). There are some pretty useful uses for this.
  1. Block unwanted connections (ads for example). This file can basically double as an adblocker. The ip address 0.0.0.0 is specifically designed to be an un-routeable address. This means that if you map a hostname to 0.0.0.0 you will not be able to go to that url in your browser. You will get a "This site cannot be reached" message from your browser. This also means that if you are surfing on a site and there are a bunch of requests to ad domains for example, they can be blocked as well with the Hosts file. You can use a currated list to add to your hosts file which blocks a bunch well known ad hosts. It has an equivalent effect of using an adblocker but without needing to install anything.
  2. You can take it further by blocking specific sites that you waste time on. I'm currently trying to be diligent about how I spend my time so I added this to my Hosts file: 
                   0.0.0.0  www.espn.com  espn.com

          I should note that yes, you do have to add subdomains for them to be blocked.

      3. I have my raspberrypi git server running with a static ip so I added this line:

            192.168.0.111 raspberrypi
          This lets me ssh to raspberrypi instead of 192.168.0.111. Its a lot easier to remember and type.

There's many more uses for this but this is what I am using it for for now. It's quite a powerful little tool. So keep it in mind if you are working around domains and ips.

Oh right, to access your hosts file:

Linux: /etc/hosts
Windows: C:\Windows\System32\drivers\etc\hosts
OSX: /private/etc/hosts