Like Windows-based computers, Mac has a hosts file to configure how your machine connects to the websites on the Internet. This file contains references to websites and IP addresses, and you can use it in many different ways on your Mac.

One of the reasons you may want to edit the hosts file on your Mac is to block websites. It’s a great way to block certain websites from being accessed on your machine. The second possible usage is to test out your own websites locally on your machine. You can have the hosts file redirect your chosen domain name to the network path of your local storage.

Table of Contents
    How to Edit the Hosts File on Your Mac

    Mac Hosts File Location

    Since editing the hosts file is a risky task, Apple has intentionally placed it in a private folder on your system. That’s to prevent users from modifying it without proper knowledge and causing damage to the entire system.

    Go to Folder window

    For the curious users out there, the path is /etc/hosts/ and you can access it using a Terminal window.

    Edit Hosts File On Mac

    It’s fairly easy to edit the hosts file on your Mac since there’s a built-in editor to do it. It’s located inside the Terminal and is called nano editor. You can use it to open and edit any text file including the hosts file on your machine.

    Make sure you’re using an admin account to do the following steps.

    1. Click on Launchpad in the Dock, search for Terminal, and launch it.
    Terminal in Spotlight Search
    1. Type the following command into the Terminal and press Enter. It’ll open the hosts file in the nano editor.

      sudo nano /etc/hosts
    Terminal screen with command: sudo nano /etc/hosts
    1. Since it’s a sudo command, it’ll ask you to enter your password. Enter the password and continue.
    Password prompt in Terminal window
    1. The file should now be open on your screen and you can start editing it.

    Understanding The Mac Hosts File

    If you haven’t edited a hosts file before, you might find the file a bit complicated to work with. However, it’s not as difficult to edit it as it looks.

    One of the entries you’ll find in the file is 127.0.0.1 localhost.

    localhost on Terminal

    The first section with numbers is the local IP address for your Mac. The second section where it has the host name is what you use to access that IP address.

    The above two parts, when combined together, redirect all the localhost queries to the IP address 127.0.0.1. When you enter localhost in your browser, your browser looks into the hosts file, obtains the specified IP address, and takes you to that IP address.

    In a nutshell, the hosts file is nothing but a combination of IP addresses and domain names. You can modify these in any way you want to achieve desired results.

    Set Up Redirects With The Hosts File

    One of the things you can do with the hosts file is to set up redirects. You can have a domain name point to something completely different than what it’s supposed to point to.

    For example, if you’re trying to cut down on your social media usage, you can have domains like facebook.com redirect your browser to sites like, say, Wikipedia. You can use just about any domain and IP address you want.

    Let’s take a look at how you can set up the above redirection using the hosts file.

    1. While the file is open in the nano editor, use the arrow keys to bring your cursor to where localhost ends. Then press Enter to add a new line for your entry.
    New line in terminal screen
    1. In the new line that you just added, type the IP address where you want to redirect the source domain to. We’ll use 103.102.166.224, which is Wikipedia’s IP address.
    103.102.166.224 address typed in terminal
    1. Press the Tab key on your keyboard to get to the source domain field.
    2. Here, type the domain name that is to be redirected to the IP address you typed earlier. We’ll use facebook.com here.
    facebook.com host in terminal window
    1. Once the changes are made, press the Ctrl + O keys on your keyboard to save the file.
    2. Press Enter to confirm the action.
    3. Press the Ctrl + X keys to close the nano editor.
    4. You now need to flush the DNS cache to bring the changes into effect. To do it, type the following command into the Terminal and press Enter.

      dscacheutil -flushcache
    Terminal window with command dscacheutil -flushcache
    1. Open a browser, type facebook.com, and hit Enter. You’ll find it opens Wikipedia rather than Facebook.

    Quick Tip: How To Find a Website’s IP Address

    As you can see in the above procedure, you need the IP address of the site that you want to redirect people to. If you don’t already know it, you can use a command in the Terminal to find out the IP address of any website.

    1. In the Terminal window, type the following command and hit Enter. Make sure to replace wikipedia.org with the website of your choice.

      ping wikipedia.org
    ping wikipedia.org in terminal window
    1. It’ll display an IP address on your screen. That’s what you can use in the hosts file.
    IP address displayed in terminal screen

    Block Websites By Editing The Hosts File On Mac

    HOW TO BLOCK WEBSITES ON WINDOWS: Using The Host File

    The great thing about the hosts file is it lets you block sites without requiring you to install any third-party apps on your Mac. You can add an entry to the file and all the connection requests to that entry will be denied.

    1. Launch the hosts file in the nano editor as shown above.
    2. Bring your cursor where the localhost entry ends and press Enter to add a new line.
    3. Type in the IP address 127.0.0.1 and press Tab on your keyboard.
    4. Enter the domain name of the site that you want to block. For example, if you want to block Instagram, type instagram.com.
    instagram.com in terminal window
    1. Press Ctrl + O to save the changes.
    2. Press Ctrl + X to close the file.
    3. Type the following command and press Enter to flush the DNS cache.

      dscacheutil -flushcache
    Command: dscacheutil -flushcache in terminal

    Now each time you try to access the blocked site, it’ll take you to the localhost which will show an error page.

    The Mac hosts file provides you with many ways to play around with outgoing network requests, and you can block and unblock them as you wish. Have you used the hosts file on your Mac before? If so, what was it for? Let us know in the comments below.