Your mac has a hosts file which contains a list of IP and domain addresses. Whenever a URL is accessed on the Mac using the browser or other apps, the system first checks for entries in the hosts file and fetches the IP address if found. So one can edit Mac’s hosts file to block specific domains like those sending ads or malware. It can be used to block unsafe sites on a Mac or even help downgrade iPhones to older firmware.
Where is Mac’s Hosts File Located:
You can find the “hosts” file in private>etc folder on the active startup partition. In order to edit it and save changes you need the rights of a root user, as this is an important system file.
Default Content of Hosts File in Mac OS X:
There are four very important entries in the hosts file which should not be messed with. In case you did change something you shouldn’t have or wonder which entries existed from the beginning, here are the default contents:
As you can see for yourself, the entry on the left side is the IP address for the domain mentioned on the right hand side.
How to Edit Mac’s Hosts File:
- Launch Terminal from Applications > Utilities or Spotlight(Command + Space)
- Open hosts file by typing the command sudo nano /private/etc/hosts and press enter
- Add or delete entries as required. IP address on the left and domain name to the right, under the four existing entries.
- Save the hots file by pressing control+O followed by enter. Press control + X to exit the editor.
Now that you’ve learnt how to edit the hosts file let’s see how to apply it in a useful situation.
Blocking Access to a Website in Mac OS X:
Suppose I want to stop access to a website called www.notforkids.com. Here’s the line that needs to be added at the end of the hosts file.
Trick Mac by Redirecting to Another Site:
You might have come across this method while downgrading iPhone firmware. Since, Apple does not officially allow downgrading devices to older iOS firmware developers add the following line to the hosts file to trick iTunes into believing it is talking to Apple’s servers(gs.apple.com) while it is in fact talking to Saurik’s servers.
That’s all for this tutorial. If you face any difficulties, feel free to ask.