In the tech world, āhackingā is often referenced with derision ā I, myself, am guilty of doing so here at Bits & Bytes ā but really itās just a term for getting a computer or a system or a network to do something it wasnāt initially intended to do. Hacking isnāt always done with an evil sneer and a sinister twirling of the mustache; there are simple, mostly benign hacks that anyone can do, and they usually come in the form of neat little tricks. For the next few months, Bits & Bytes will be sneaking ābehind the curtainā to show you how some of these tricks are done in a new series of articles titled āYou, Too, Can Hack.ā
You, Too, Can Hack (Part 1): The Windows Hosts File
Nestled quietly in an obscure folder on your computerās hard drive sits the hosts fileā a simple text file lets you control how your computer finds websites on the internet. Thereās rarely a reason for anyone to ever interact with this file, but for those who know what theyāre doing, the hosts file can do some neat things like block distracting sites, test local webpages, and even improve security. Or, if you happen to have access to a loved oneās computer, it can lead to some pretty good pranks. So Iāve heard, anyway (ahem).
What Is the Hosts File?
You can think of the hosts file as a phone book for your computer. But instead of phone numbers, it contains a list of domain names (like www.example.com) and their matching IP addresses. When you type a websiteās name into your browser, Windows first checks this file before asking your internet service for directions. If it finds a match, your computer connects directly using the provided IP address instead of the IP it should be using. This feature gives you the ability to override normal internet lookups with your custom settings, which can come in handy in certain situations.
On a Windows machine, the hosts file is located at:
C:\Windows\System32\drivers\etc\hosts
Because itās a critical system file, youāll need administrative privileges to edit it. Most text editors like Notepad can work, but remember to "Run as Administrator" for any changes to stick. And just as a heads up, if youāre interested in playing around with the hosts file, Iād suggest making a copy of the original hosts file before making changes just in case. Changing your hosts file can have handy benefits, but overdoing it might lead to unexpected results, so keep it neat and well-documented.
How Does It Work?
The hosts file operates as a set of rules, with each line in the file containing an IP address and a domain formatted like so:
127.0.0.1 www.example.com
Note that there should always be at least one space or tab between the IP address and domain on each line.
In this case, 127.0.0.1 is a special IP address that always points back to your own computer. When you set this rule, any time you then try to visit the web site www.example.com, your computer will find the IP address in the hosts file and loop back to itself instead of fetching the real site; this essentially blocks access to that website.
Thus, one common use for the hosts file is blocking websites that distract you or that you feel could compromise your productivity (or even your security). For instance, if youāre looking for a way to prevent yourself from falling down a social media rabbit hole during work hours, you can redirect those sites to a harmless local address.
How to Block a Website
Once youāve opened the hosts file as an administrator, add one new line to the bottom of the file for each site you want to block. For example, to block Facebook and Reddit, you would add the following (remembering to put a space or tab between the IP address and domain):
127.0.0.1 www.facebook.com
127.0.0.1 www.reddit.com
After saving your changes, you can flush your local DNS cache to make sure that Windows uses your updated hosts file immediately. To do this, open a Command Prompt as an administrator (type ācmdā into search and select āRun as administratorā) and then use the following command:
ipconfig /flushdns
Now, when you try to visit these sites, your browser will be unable to connectāgiving you a productivity boost without any extra software.
Other Uses
If youāre a developer, you can use the hosts file to map a custom domain name to a local server address. This can make testing local web projects easier before they actually go live.
You can also preemptively block hosting sites known for malware or phishing attempts by simply redirecting them to 127.0.0.1.
Iāve also heard that if you create a dummy site for a well-known search engine that only returns search results with your momās name and picture, you can use the hosts file to make sure she gets redirected to that dummy site no matter what browser sheās using. Supposedly, that kind of thing can really freak people out, so I donāt recommend trying it out. Indeed, definitely do not edit your momās hosts file to make her think Google is taking over her computer.
Iāll be back next month with Part 2 of āYou, Too, Can Hackā. Stay out of trouble.
Steve Shannon has spent his entire professional career working in tech. He is the IT Director and Lead Developer at PromoCorner, where he joined in 2018. He is, at various times, a programmer, a game designer, a digital artist, and a musician. His monthly blog "Bits & Bytes" explores the ever-evolving realm of technology as it applies to both the promotional products industry and the world at large. You can contact him with questions at steve@getmooresolutions.com.