How to edit the hosts file on a Mac

Like Windows computers, Macs have a hosts file that configures how your machine connects to websites on the Internet. This file contains references to web pages and IP addresses, and you can use it in a variety of ways on your Mac.

One of the reasons you might 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. A second usage could be to test your web pages locally on your computer. You can tell the hosts file to redirect your selected domain to the network path of local storage.

Mac hosts file location

Since editing the hosts file is a risky task, Apple has intentionally placed that file in a separate folder on your system. It is 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 way is /etc/hosts/ and you can access it using a Terminal window.

Edit the hosts file on Mac

It’s pretty easy to edit the hosts file on your Mac as there’s a built-in editor to do it. It’s located inside 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 that you are using an administrator account to perform the following steps.

  1. Click Launchers in the Dock, search Endand launch it.
Terminal in Spotlight Search
  1. Type the following command into Terminal and press enter. It will open the hosts file in the nano editor.

    sudo nano /etc/hosts

Terminal screen with command: sudo nano /etc/hosts
  1. Since this is a sudo command, it will ask you for your password. Enter the password and continue.
Password prompt in Terminal window
  1. The file will now be open on your desktop and you can start editing it.

Learn the Mac hosts file

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

One of the entries you will find in the file is localhost.

localhost on Terminal

The first part with the number is the local IP address for your Mac. The second part where it has the hostname is what you use to access that IP address.

The two above, when combined, redirect them all localhost IP address query When you enter localhost in your browser, your browser looks at the hosts file, gets the specified IP address and takes you to that IP address.

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

Set up redirects with hosts file

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

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

Let’s see how you can set up the redirect above using the hosts.

  1. While the file is open in the nano editor, use the arrow keys to take your cursor where localhost end. Then press enter to add a new line to your entry.
New line in terminal screen
  1. In the new line you just added, enter the IP address you want to redirect the source domain to. We will use the IP address of Wikipedia.
103.102.166,224 addresses entered in the terminal
  1. Press the button Navigation on your keyboard to access the source domain field.
  2. Here, enter the domain name that will be redirected to the IP address you entered earlier. We will use here. server in terminal window
  1. After the changes are made, press Ctrl + O on your keyboard to save the file.
  2. Press enter to confirm the action.
  3. Press the button Ctrl + X keys to close nano editor.
  4. Now you need to clear the DNS cache for the change to take effect. To do this, enter the following command in Terminal and press enter.

    dscacheutil -flushcache

Terminal window with the command dscacheutil -flushcache
  1. Open a browser, type facebook.comand 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 process, you need the IP address of the website you want to redirect people to. If you don’t already know, you can use a command in Terminal to find out the IP address of any website.

  1. In the Terminal window, type the following command and press enter. Guaranteed replacement with the site of your choice.


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

Block websites by editing hosts file on Mac

Watch our YouTube videos

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

  1. Launch the hosts file in the nano editor as shown above.
  2. Take your cursor where localhost End entry and press enter to add a new line.
  3. Enter IP address and press Navigation on your keyboard.
  4. Enter the domain name of the website you want to block. For example, if you want to block Instagram, type in a 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 clear the DNS cache.

    dscacheutil -flushcache

Command: dscacheutil -flushcache in terminal

Now every time you try to access the blocked website it will take you to the localhost which will show the error page.

The Mac hosts file gives you multiple ways to handle 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 is it for? Let us know in the comments below.

Leave a Reply

Your email address will not be published.