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 really 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

    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.

    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.
    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
    1. Since it’s a sudo command, it’ll ask you to enter your password. Enter the password and continue.
    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 localhost.

    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 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 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.
    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, which is Wikipedia’s IP address.
    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 here.
    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
    1. Open a browser, type, 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 with the website of your choice.

    1. It’ll display an IP address on your screen. That’s what you can use in the hosts file.

    Block Websites By Editing The Hosts File On Mac

    Check out our YouTube video

    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 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
    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

    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.