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.
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.
- Click Launchers in the Dock, search Endand launch it.
- Type the following command into Terminal and press enter. It will open the hosts file in the nano editor.
sudo nano /etc/hosts
- Since this is a sudo command, it will ask you for your password. Enter the password and continue.
- 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 127.0.0.1 localhost.
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 127.0.0.1. 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 facebook.com 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.
- 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.
- In the new line you just added, enter the IP address you want to redirect the source domain to. We will use 18.104.22.168is the IP address of Wikipedia.
- Press the button Navigation on your keyboard to access the source domain field.
- Here, enter the domain name that will be redirected to the IP address you entered earlier. We will use facebook.com here.
- After the changes are made, press Ctrl + O on your keyboard to save the file.
- Press enter to confirm the action.
- Press the button Ctrl + X keys to close nano editor.
- 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.
- 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.
- In the Terminal window, type the following command and press enter. Guaranteed replacement wikipedia.org with the site of your choice.
- It will display the IP address on your screen. That’s what you can use in the hosts file.
Block websites by editing hosts file on Mac
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.
- Launch the hosts file in the nano editor as shown above.
- Take your cursor where localhost End entry and press enter to add a new line.
- Enter IP address 127.0.0.1 and press Navigation on your keyboard.
- Enter the domain name of the website you want to block. For example, if you want to block Instagram, type instagram.com.
- Press Ctrl + O to save the changes.
- Press Ctrl + X to close the file.
- Type the following command and press enter to clear the DNS cache.
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.