The Hosts File is a tool that helps in changing the way Mac computers view a website. Most users do not ever think of altering and editing their Hosts File, but web developers or programmers do. They might need to edit the file to view a new website that they are developing and how the site is functioning.
If you want to open up the programmatic side of your Mac truly, here’s everything you need to know about Hosts Files. Let’s go.
What is the Hosts File?
The DNS (domain name system) and the cache associated with it explains where the website is headed to. But this is not the only relevant file that developers use. There’s another file that is equally important, which is the Hosts File. This file overrides the DNS information.
Using the Hosts File instead of the DNS makes sense. For example, suppose you are testing a development server, and before deploying it, you want to ensure everything is functioning perfectly. During this process, it would be better to use the domain name instead of the IP address of the machine. Before the system is accessible to anyone using DNS, you can leverage the Hosts File instead. Type the machine’s IP address, and once the domain name is used, it will go to the device instead.
Another reason is to work around or block spyware. The Hosts File zeroes out the IP address of the malicious sites.
Where to Locate the Hosts File?
On your Mac, the Hosts File is located in the /private/etc/hosts, which is a protected part of your device, so you cannot open and edit it without following the proper steps.
You can locate and edit the Hosts File via the Terminal app on your device.
Steps to Editing Hosts Files
It is not difficult to change Hosts File Mac, but for obvious reasons, this file is hidden. You can use the Terminal application to edit this file. Select the app in the Finder or search for it using Spotlight.
- Launch the Terminal app
- Type sudo nano /etc/hosts and then hit return.
- Enter your administrator password and then hit return.
Now, you are within the Nano text editor, and this is where the actual editing of the Hosts File will take place. Here, you will already see some of the TCP/IP addresses against broadcasthost and localhost titles. Follow the same format to enter your TCP/IP:
- Press the Enter key to create some space below the existing records, and then enter the TCP/IP address.
- Press the Tab key on the keyboard and write the domain name.
- Press Control + O to save the file, and then press Control + X to exit.
- Type sudo killall -HUP mDNSResponder in Terminal and press the Enter/Return key to clear the DNS cache on your Mac computer. This will prevent clashes between the Hosts File and DNS.
The changes will immediately take effect. You can use Mac’s Hosts Files to input any TCP/IP you need.
Why would you want to Edit the Hosts File?
The reasons why you would want to change the Hosts file are:
- To block malicious websites and undetected add-ons by rerouting them so you are taken to a safer page
- To reroute the actual domain name to the development website to find out how users are experiencing it
- To block access to time-wasting websites so your team can focus
Steps to Reset the Hosts File on Your Mac
Do you want to undo the changes and restore the Hosts File’s route? You can do so by replacing the current file with a new set of terms. You can do the reset without using the Terminal app.
- Open Finder and click Go > Go to Folder
- Type /private/etc/hosts into the search field and click Go.
- Drag the Hosts File onto the desktop and open the file from your desktop. When you click the file, it will open to TextEdit by default.
- Remove the content of the Hosts File by deleting it.
- In the same file, paste “### Host Database## localhost is used to configure the loopback interface# when the system is booting.”
- Save the changes and drag the same file from the desktop into the same folder. Select Replace when prompted, and this will replace the file. When asked, enter the admin password.
- Finally, restart your Mac.
The changes will be reset on your Mac.
In summation, it is not very difficult to locate and edit the Hosts File on your Mac. You can make changes to control the websites other users will see. If the changes do not take place, you can restart your computer. Also, you must close all open browsers so the new changes can take effect.