How to Edit the Hosts File on Mac.
Just like Windows computers, Macs have a hosts file to set up your computer’s connection to websites on the Internet. This file contains links to websites and IP addresses, and you can use it in different ways on your Mac.
One of the reasons you might want to edit the hosts file on your Mac is to block websites. This is a really great way to block access to certain websites on your computer. The second possible use is to test your own websites locally on your computer. You can have the hosts file redirect your chosen domain name to the network path of your local storage.
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. This is done so that users cannot change it without proper knowledge and damage the entire system.
For curious users, the path is / etc / hosts / and you can access it through a terminal window.
Edit Hosts File on Mac
Editing the hosts file on your Mac is pretty easy as there is a built-in editor for that. It is located inside Terminal and is called the nano editor. You can use it to open and edit any text file, including the hosts file on your computer.
Make sure you are using an administrator account to complete the following steps.
–
- Click Launchpad in the Dock, find Terminal and launch.
- Type the following command into Terminal and press Enter. It will open the hosts file in nano editor.
sudo nano / etc / hosts
- Since this is a sudo command, it will ask you for a password. Enter your password and continue.
- The file should now be open on your screen and you can start editing it.
Learn Mac Hosts File
If you haven’t edited the hosts file before, you may find it a little difficult to work with. However, editing it isn’t as difficult as it sounds.
One of the entries you’ll find in the file is 127.0.0.1 localhost.
The first section with numbers is the local IP address of your Mac. The second section where the hostname is listed is what you use to access that IP address.
The two parts above, when combined together, redirect all localhost requests to the IP address 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 change them in any way you want to achieve the desired results.
Set up redirects with the Hosts file
One of the things you can do with the hosts file is set up redirects. You may have a domain name that points to something completely different than what it should point to.
For example, if you’re trying to curtail your social media usage, you can use domains like facebook.com to redirect your browser to sites like, say, Wikipedia. You can use almost any domain and IP address.
Let’s see how you can set up the above redirection using the hosts file
- While the file is open in the nano editor, use the arrow keys to move the cursor to where localhost ends. Then press Enter to add a new line for the entry.
- On the new line you just added, enter the IP address you want to redirect the source domain to. We will use 103.102.166.224, which is the IP address of Wikipedia.
- Press the Tab key on your keyboard to navigate to the source domain field.
- Here enter the domain name that will be redirected to the IP address you entered. earlier. We will be using facebook.com here.
- After making your changes, press Ctrl + O on your keyboard to save the file.
- Press Enter to confirm the action.
- Press Ctrl + X to close the nano editor.
- Now you need to clear the DNS cache for the changes to take effect. To do this, enter the following command in Terminal and press Enter.
dscacheutil -flushcache
- Open a browser, type facebook.com and press Enter. You will find that it opens Wikipedia, not Facebook.
Tip: how to find the IP address of a website
As you can see in the above procedure, you need the IP address of the site you want to redirect people to. If you don’t already know it, 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. Be sure to replace wikipedia.org with the website you want.
ping wikipedia.org
- The IP address will be displayed on the screen. This is what you can use in your hosts file.
Block Websites by Editing the Hosts File on Mac
The great thing about the hosts file is that it allows you to block sites without requiring you to install any third-party applications on your Mac. You can add an entry to a file and all connection requests to that entry will be denied.
- Run the hosts file in the nano editor as shown above.
- Bring position the cursor where the localhost entry ends and press Enter to add a new line.
- Enter the IP address 127.0.0.1 and press Tab on your keyboard.
- Enter the domain name of the site you want to block. For example, if you want to block Instagram, enter instagram.com.
- Press Ctrl + O to save your changes.
- Press Ctrl + X to close the file.
- Type the following command and press Enter. to clear the DNS cache.
dscacheutil -flushcache
Now every time you try to access a blocked site, it will redirect you to the localhost, which will display an error page.
The hosts file for Mac 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 a hosts file on your Mac before? If so, why? Let us know in the comments below.
–