A Guide to Hosts File and Using it to Block Websites on Windows, Linux and Mac OS

Do you encounter a lot of ads and spam websites while browsing the internet? Do you want to block access to certain websites on your PC? The solution you’re looking for is called hosts file! In the below article, we will discuss the hosts file and how to edit it in Windows, Linux, and Mac operating systems.
A hosts file in any system is a file maintained by the system’s OS that links a hostname to its respective IP address. The hosts file helps address various nodes in the network by translating a numeric IP address into a human-understandable name. It usually is present as operating system implementation and is stored as plaintext or ASCII document.
Table of Contents
Why Would I Need to Edit My Hosts File?
Most basically, a hosts file can be used to override your DNS server, allowing a user to create a custom URL for any website of their liking. This custom URL can be mapped to the IP address of the website they want it to redirect to, making it possible to use alternate URLs for popular websites.
However, it is to be noted that hosts file only applies to your local machine, meaning that the custom URL will only work on your system whose hosts file was edited.
Image Source – bowerwebsolutions.com
There are a few benefits of maintaining or editing your hosts file manually.
- While not most important, to speed up the address resolution process of your web browser.
If you regularly visit a few websites, you can use the hosts file to speed up the loading time, although only by a few milliseconds. - The more important use of a hosts file would be to block ads and malicious websites.
These malicious websites could either contain malware, spyware or be simply annoying with their constant pop-ups. These websites can be blocked using the hosts file by merely mapping them to the loopback IP, i.e., 127.0.0.1. - Another primary use of the hosts file is to test a new domain that has been newly created or migrated from another server without altering the DNS server just yet. It is always advised not to make changes to the DNS until it confirms that the website is working perfectly.
So, a website test can be run locally by mapping the hosts file to the new server instead of altering the DNS immediately. Once the testing is finished and the website works properly, only then migrate the DNS information to the new server.
How to Edit Your Windows Hosts File
The hosts file is store as a txt file in the Windows operating system and can be found in the ‘System32’ folder in the C drive. In different versions of Windows, editing the hosts file is quite different. If using Windows XP or earlier versions, editing the hosts file was as easy as simply opening it in notepad, making changes, and saving it.
But in later versions, if you attempt to edit them similarly, you are bound to receive an error (see figure below) claiming that you don't have appropriate permission to do so. An additional level of permissions was added to the process requiring administrator access to save changes to a hosts file in these versions.
This was mainly done to prevent other users on the PC from making changes to the hosts file for their own benefits or accessing websites restricted by the administrator.
Windows 10 & Windows 8
Editing the hosts file in these two Windows OS versions is quite similar. The following steps have to be followed if you want to edit the hosts file on your windows 10 or windows 8 PC:
- Open start menu
- Search for ‘Notepad’ in the search box
- When the notepad icon appears, right-click on it and select run as administrator
- In notepad, go to File-Open or press Ctrl + O
- In the ‘Open’ dialog box, navigate to C:\Windows\System32\Drivers\etc\hosts and open the hosts file. You might have to change the file type to 'All Files’ instead of txt files in the dialog box.
- Make all necessary changes to this file
- New domain mapping can be added at the bottom of the file by specifying the IP address and custom domain name.
- Save the file and close the notepad
Windows 7 & Windows Vista
The steps here are almost the same as before with small changes
- Open Start menu
- Navigate to Accessories-Notepad
- Right-click on 'Notepad' and select run as administrator
Image Source – ipserverone.info
Image Source – ipserverone.info
- A dialog box pops up asking for admin permission; click yes here
- In notepad, go to File-Open or press Ctrl + O
- In the ‘Open’ dialog box, navigate to c:\Windows\System32\Drivers\etc\hosts
- You might have to change the file type to 'All Files’ instead of txt files in the dialog box
- Open the hosts file and make all necessary changes
- New domain mapping can be added at the bottom of the file by specifying the IP address and custom domain name
- Save the file and close the notepad
How to Edit Your Hosts File in a Linux OS
If you are using a Linux-based operating system, the process to edit your hosts file will be entirely different, obviously. The hosts file will again be stored in plain text in a Linux OS and located at /etc/hosts. You would also require either root access or sudo user privilege in order to be able to edit the hosts file.
The following are the steps to edit the hosts file in a Linux system and can be applied for all Linux distros as long as you have access to a terminal.
- Launch a terminal on your Linux OS
- In the terminal window, open the hosts file with any available text editor (Vim, Nano, Emacs, etc.) using the sudo command
- The full path to the hosts file is /etc/hosts
- Enter the sudo password
- Scroll to the bottom of the text editor to add any new mappings to the hosts file or edit previous ones
Image Source – makeuseof.com
- The format for mapping can be copied from already existing lines
- Save changes to the file and press Ctrl + X to exit
- Press Y when prompted whether changes should be saved
How to Edit Your Hosts File in a Mac Operating System
Even though most people use Macs for non-programming-based use, it becomes quite a powerful PC once that side of a Mac opens up. Even though easy to use, Macs are highly efficient machines mostly due to their Unix root, much like a Linux OS.
This also means that a system running Mac OS has a terminal making the process of editing the hosts file quite similar to that in the Linux system.
- Launch a new terminal
- In the terminal window, use the path /etc/hosts to open the hosts file with any text editor of your choice
- The sudo command is to be used while opening the hosts file
- Enter the sudo password
Image Source – dalendesign.com
- As before, scroll to the bottom of the editor, add or remove lines for IP mapping.
Image Source – dalendesign.com
- Save changes and quit the text editor
- You may sometimes have to flush your DNS cache for the changes to take effect or prevent clashes. To do this, type in the following command
sudo dscacheutil -flushcache
How to Block Websites Using the Hosts File
The basic format of a hosts file mapping looks as below
{127.0.0.1} {localhosts} {#loopback}
The curly braces are only meant as a separator for each section and aren’t part of the syntax. Each section is usually separated by a space or tab.
The use of each section is as follows:
- The first section denotes the IP address to which the connection request will be directed to
- The second section is the hostname that initiates the redirect request
- The third and final section is for comments about the mapping
We can add a website on the hosts file that we require to be restricted/blocked on the specific system. In order to block a website using the hosts file, follow the steps listed below:
- First, based on your OS, open the hosts file as listed in the previous sections
- At the bottom of the hosts file, you can see a section of IP addresses with a domain name. This is where new websites can be added that need to be blocked. Refer to step five from the previous chapter above.
- Now, blocking of a website is usually done by mapping its domain name to the localhost IP address (127.0.0.1)
- In the hosts file, enter a new line with the localhost IP and the site's domain name to be blocked. In our example, figure 11, we use the hosts file to block Facebook
- The # is used to write a comment about the new mapping, which is only optional
- Finally, once all websites to be blocked have been entered, save the file and exit
- To ensure that the website has been blocked, simply open the command prompt and ping the website. In our case, the command is
>ping facebook.com
- From figure 12, it can be seen that the OS is trying to ping 127.0.0.1, which means Facebook has been blocked successfully with the hosts file.
Conclusion
The hosts file exists in a PC to resolve and map domain names to IP addresses. Although most users might not bother maintaining the hosts file, doing so has many benefits provided by the OS. In this article, we discussed some of these benefits and looked at editing a hosts file in the Windows, Linux, and Mac Operating systems.
If you enjoyed reading this article, or if you have any shortcut methods to maintain a hosts file, please leave a comment below and let us know!
✔️ Advanced information to protect your Internet connection
Your email address will not be published. Required fields are marked