How to Edit Hosts File in Mac OS X using the Terminal

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:          localhost    broadcasthost
::1                localhost
fe80::1%lo0        localhost

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:

Screen shot 2011 03 14 at 4 06 43 AM

  • 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 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( 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.

2 thoughts on “How to Edit Hosts File in Mac OS X using the Terminal”

  1. I cannot find the host files on my mac,nothing comes up?? I Had jb 4.1 and then i went to 4.3.1 and cannot get a jb for this os that works. Can you help me out.I did save my shsh.

  2. Hi,

    I’ve been struggling with altering my hosts file for over a week now and tried everything I can find on the web, but can’t seem to be able to do it.

    I’ve tried the sudo nano method, but when I come to save I get a “Operation not permitted” error.

    Can you help at all?

Comments are closed.