How to Edit the Hosts File

The hosts file is a system file that maps IP addresses to domain names. It can be useful for testing websites on a local server, blocking websites, or redirecting domains. In this guide, we’ll walk through how to edit the hosts file on both Windows and macOS.

What is the Hosts File?

The hosts file is used by the operating system to map domain names to IP addresses. For instance, you can set the domain example.com to point to a local server by assigning it a specific IP address.

How to Edit the Hosts File on Windows

  1. Open Notepad as an administrator:
    • Search for Notepad in the Start menu.
    • Right-click on the Notepad icon and select “Run as administrator.”
  2. In Notepad, open the hosts file by navigating to C:\Windows\System32\drivers\etc\hosts.
  3. Add the required entries in the format: IP address domain. For example:
    127.0.0.1 example.com
  4. Save your changes by selecting “File” → “Save.”

How to Edit the Hosts File on macOS

  1. Open Terminal (you can find it via Spotlight or in the Applications folder).
  2. Enter the following command to open the hosts file in the nano editor:
    sudo nano /etc/hosts

    You may be prompted to enter your administrator password.

  3. In the editor, add the necessary entries. Use the same format: IP address domain. For example:
    127.0.0.1 example.com
  4. To save, press Ctrl + O, then Enter, and exit the editor by pressing Ctrl + X.

Verifying the Changes

To ensure the changes take effect, you may need to clear your DNS cache:

On Windows:

ipconfig /flushdns

On macOS:

sudo dscacheutil -flushcache; sudo killall -HUP mDNSResponder

The hosts file is now updated, and the changes should take effect. You can verify this by opening the website you redirected in your browser.