© 2023 CoolTechZone - Latest tech news,
product reviews, and analyses.

If you purchase via links on our site, we may receive affiliate commissions.

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.

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 explaining how address resolution works in a PC

Image Source – bowerwebsolutions.com

There are a few benefits of maintaining or editing your hosts file manually.

  1. 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.
  2. 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.,
  3. 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.

Error Claiming Lack of Admin Access to Edit Hosts File

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:

  1. Open start menu
  2. Search for ‘Notepad’ in the search box
  3. When the notepad icon appears, right-click on it and select run as administrator

    Running Notepad with Administrator Access in Windows 10

  4. In notepad, go to File-Open or press Ctrl + O
  5. 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.

    Opening Hosts File from System Folder in Windows 10

  6. Make all necessary changes to this file
  7. New domain mapping can be added at the bottom of the file by specifying the IP address and custom domain name.

    Section to Create New Domain name and IP mapping in Windows 10 Hosts File

  8. Save the file and close the notepad

Windows 7 & Windows Vista

The steps here are almost the same as before with small changes

  1. Open Start menu
  2. Navigate to Accessories-Notepad
  3. Right-click on 'Notepad' and select run as administrator

    Searching for Notepad in Windows 7 Computer

    Image Source – ipserverone.info

    Opening Notepad with Administrator Access in Windows 7

    Image Source – ipserverone.info

  4. A dialog box pops up asking for admin permission; click yes here
  5. In notepad, go to File-Open or press Ctrl + O
  6. In the ‘Open’ dialog box, navigate to c:\Windows\System32\Drivers\etc\hosts
  7. You might have to change the file type to 'All Files’ instead of txt files in the dialog box
  8. Open the hosts file and make all necessary changes
  9. New domain mapping can be added at the bottom of the file by specifying the IP address and custom domain name
  10. 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.

  1. Launch a terminal on your Linux OS
  2. In the terminal window, open the hosts file with any available text editor (Vim, Nano, Emacs, etc.) using the sudo command
  3. The full path to the hosts file is /etc/hosts
  4. Enter the sudo password
  5. Scroll to the bottom of the text editor to add any new mappings to the hosts file or edit previous ones

    Editing Hosts File with a Terminal in a Linux OS

    Image Source – makeuseof.com

  6. The format for mapping can be copied from already existing lines
  7. Save changes to the file and press Ctrl + X to exit
  8. 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.

  1. Launch a new terminal
  2. In the terminal window, use the path /etc/hosts to open the hosts file with any text editor of your choice
  3. The sudo command is to be used while opening the hosts file
  4. Enter the sudo password

    Terminal Command to Access Hosts File in Mac OS

    Image Source – dalendesign.com

  5. As before, scroll to the bottom of the editor, add or remove lines for IP mapping.

    Editing Hosts File in Mac OS using the Nano Text Editor

    Image Source – dalendesign.com

  6. Save changes and quit the text editor
  7. 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

{} {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:

  1. The first section denotes the IP address to which the connection request will be directed to
  2. The second section is the hostname that initiates the redirect request
  3. 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:

  1. First, based on your OS, open the hosts file as listed in the previous sections
  2. 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.
  3. Now, blocking of a website is usually done by mapping its domain name to the localhost IP address (
  4. 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

    Blocking Facebook using Windows 10 Hosts File

  5. The # is used to write a comment about the new mapping, which is only optional
  6. Finally, once all websites to be blocked have been entered, save the file and exit
  7. 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

    Confirming Whether Website is Blocked using Ping in Windows 10 Command Prompt

  8. From figure 12, it can be seen that the OS is trying to ping, which means Facebook has been blocked successfully with the hosts file.


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!

Leave a Reply

Your email address will not be published. Required fields are marked

Cool Tech ZoneCyber Security Labs & News