You might have never heard of the hosts file — literally, that’s a file with “hosts” as the name. It has no extension that determines what type of file it is.
The file itself is inconsequential. However, when placed at a particular location within your computer, it can be an effective tool to control a computer’s access to the outside world, a.k.a the Internet. In this case, it’s referred to as the hosts file.
If you’re interested in a simple and effective way to block certain websites from a computer, you’re reading the right post. I’ll explain how to manage the host file on your computer, be it a Windows or a Mac.
What is the host file, and how to handle it
Every computer running Windows, macOS, or Linux has its hosts file. By default, this file is not in effect. It only has information that explains what it is and how to use it.
In other words, you can delete the default hosts file, and nothing will happen. We don’t need it. And we can always recreate it.
Understanding the Hosts file
When edited in a certain way, the hosts file works as the computer’s local DNS mapper. It’s a manual way to map a domain, such as facebook.com, to a particular IP address of your choosing.
Specifically, when you map a domain to a non-existing IP address or that of the computer itself, you effectively block the domain for the computer’s users.
The hash sign (#) in a hosts file signifies commenting.
In programming, commenting is a way to explain what a line of code does without affecting the code itself. The computer ignores the # symbol itself and the text that follows it on each line.
If a hosts file has # at the beginning of every line, which is the default, it’s as good as a blank file from the computer’s perfective.
Here’s an example of an entry in a hosts file. The format of the line is known as the command syntax. In this case, it’s in this format: IP address (space) domain:
(We only need a single space between the IP address and the domain, but for clarity, I hit the spacebar a few times — you can also use the Tab key on the keyboard.)
This entry points facebook.com to the IP of the localhost, the computer itself, and in effect, blocks the computer’s access to Facebook.
Alternatively, you can also use 0.0.0.0 as the IP address to get the same effect. And if you use the IP address of another website, the entry becomes a mean prank.
This mapping is in effect system-wide. As a result, every time you want to reach the domain in question using the computer where the host file resides, such as entering it on a browser, you’ll get to the IP address you’ve picked for it — the browser will not query a DNS server for that domain anymore.
Here’s a typical example of the role DNS plays:
Whenever you access a website, such as dongknowstech.com, which is a domain name, the browser — Chrome, Firefox, Edge, etc. — first query your DNS server, set by your home router.
This server then looks up the website’s domain name (a.k.a. web address or URL) and returns its IP address — a string of seemingly random numbers — to the browser. (Every website has an IP address.) The browser then follows that IP address to load the website.
This process is necessary because computers only understand numbers while humans are pretty bad at remembering them.
So a DNS server is similar to the (once existed) telephone directory service where you only need to remember a person’s name and not their phone number.
And you can manually map as many domains as you’d like — as long as you know what you’re doing and don’t abuse it. Within the hosts file, each mapping entry takes one line.
With that, let’s find where to find this glorious hosts file and how to edit it.
How to handle the host file on a Windows computer
On a Windows computer, the Hosts file always resides at this location:
(In the rare case where Windows is installed on another drive instead of c:, change the drive letter accordingly.)
You can copy and paste that line onto the address bar of Windows Explorer and hit Enter to quickly get there, as shown in the screenshot above.
Now you can manage the file however you do any file, including deleting, renaming, or making a copy of it. By the way, if you move the host file to another location, it no longer has any effect.
It takes a bit of work to open the host file for editing. Since it has no extension, the usual double-clicking won’t do.
In Windows, the best fastest way to open the hosts for editing is to copy and paste the command below on the address bar of Explorer, then hit Enter:
That will open the file in Notepad for you to make any edits. Once done, save the file (Ctrl+S or just close it and choose to Save as prompted.)
By the way, the host file shares the same syntax across platforms. Consequently, you can use the same file between Windows and macOS. Or you can copy the content of the file between them.
How to handle the host file on a Mac
The hosts file is located in the /private/etc/ folder on a Mac, which is generally hidden.
Here are the steps to locate the hosts file on a Mac, as shown in the screenshot above:
- Bring Finder to the forefront by clicking on a blank spot on the desktop, then choose Go -> Go to Folder…
- Enter /private/etc/ in the field and hit Go
- Locate the host file in the folder that appears.
But if you want to edit the file, you can skip the above.
Here are the step to open the host file on a Mac for editing
1. Run Terminal (use the Spotlight to search for it) and run the following command:
sudo nano /etc/hosts
You’ll be asked to enter the password for the account. Do it
2. The file will be open in the Nano app. You can now use the arrow key to move around and enter the information as needed.
After that, use Ctrl+0 to save the changes and Ctrl+X to exit.
The final thoughts
Using the hosts file can be a sure and straightforward way to block certain websites from a computer. However, be careful when using a pre-made hosts file that includes hundreds of domains to block all kinds of things, including online ads.
That’s because, in this case, the file certainly blocks more than what you’re aware of, including legit content.
Many domains are interconnected, and blocking one might cause others not to work properly. Blocking a wrong domain can also cause important functions of a computer, such as auto-updates or security/integrity checks, to stop working.
On top of that, a large hosts file can also cause performance issues.
That said, the hosts file is a good tool when you use it properly but can be a pain when you abuse it. In any case, it’s a good practice to be mindful of it, and use a blank (default) hosts file when troubleshooting your connection.