How To Edit The Mac OS X Hosts File (And Why You Might Want To)

mac os x terminal transparent 300   How To Edit The Mac OS X Hosts File (And Why You Might Want To)The hosts file is used by your computer to map hostnames to IP addresses. By adding or removing lines to your hosts file you can change where certain domains will point when you access them in a browser or using other software.

This is an important file and one that is under the computer administrator’s control, so you’ll need an account with full privileges to make any changes. On Mac OS X the process is a little more complicated than simply opening a text file, adding some lines and saving it again; but it’s still easy enough for even complete newbies to do with little trouble.

If you’re running Windows you can find instructions for your operating system here.

What It Does

If you’ve landed on this page from a search, there’s a good idea you already know why you want to change the hosts file (and can go ahead and skip down the page to the “Making Changes” section of this article). Still reading? I’ll assume you’re not familiar with this file then.

hosts finder   How To Edit The Mac OS X Hosts File (And Why You Might Want To)

The hosts file is used to route hostnames including website addresses to IP addresses. If an existing domain is added to this file along with an IP, it will call on that IP rather than where the domain name normally points. There are a range of uses, though the vast majority involve security, blocking hostnames and preventing connections being made.

As an example, web developers often have to use this file to access developer servers which aren’t tied to a domain. By pointing a domain or sub-domain like “” to the IP at which the development site is located it is easier to access the site. This also helps prevent the rest of the web accessing that server easily.

Another example would be to block access to a domain, so when a website redirects you to an adserver or partner site, you could block that site by adding a line in your hosts file which redirects the IP to your local machine ( Of course, this only provides a loose safety net as advertisers, spammers, malware distributors and anyone else you might want to keep out are wise.

hosts locked   How To Edit The Mac OS X Hosts File (And Why You Might Want To)

Your computer (be it Windows, Mac or Linux) will always check for the hosts file on boot, and you won’t need to do anything to enable it. It’s already there. If you’re reading this article you’re probably using a Mac, and you should know that making changes to this portion of the disk will require administrator access.

By far the easiest way of making changes to your hosts file is by using the Terminal app, rather than the Finder.

Making Changes

This tutorial is for Mac OS X Lion 10.7 and later unless otherwise specified. In order to change the hosts file you should first open the Terminal app. At the prompt type the following:

sudo nano /etc/hosts

When prompted, type in your administrator password and hit Enter.

Users on Mac OS X 10.6 or earlier will find the file in the /private/etc/hosts location instead.

terminal   How To Edit The Mac OS X Hosts File (And Why You Might Want To)

To give you an idea of what’s going on here, the sudo command provides temporary root-level access, while nano is the name of the program used to make the changes to the file, and /etc/hosts is the location of the file.

Once you’ve entered a password and loaded nano, you’ll see a window that looks roughly like the screenshot below.

hostsfile   How To Edit The Mac OS X Hosts File (And Why You Might Want To)

You’ll have to use the arrow keys to move the cursor around as your mouse pointer will not work here. Pay attention to the comments, which are signified by “#” symbols. If a line starts with a # it’s ignored, so you can use these lines to help keep the file tidy with descriptions of what each addition does. Similarly, you can use comments to quickly enable or disable changes without removing the rule entirely. This is known as “commenting out”.

Rules should be added in the following format: <IP address> <hostname>, for example: adding “” would redirect all requests (but not requests) to your local machine, essentially blocking your machine from accessing Google’s servers.

Once you’ve added a commented-out description, an IP and a domain you should save the file using the keyboard shortcut Control+O which calls the WriteOut function. You will be asked for a location and file name, but seeing as you’re overwriting a file all you need to do is press Enter. Remember you’re only able to do this because you used the sudo command to gain admin privilege, otherwise you would not have permission to overwrite what the system considers to be a very important file.

writeout   How To Edit The Mac OS X Hosts File (And Why You Might Want To)

Once you’ve hit Enter nano will report how many lines were written, and the changes will be saved. You can quit nano using Control+X to return to the prompt, the changes should be instant.

In the event that your changes don’t register immediately you can flush your DNS by opening Terminal and entering the following:

sudo killall -HUP mDNSResponder

For users on OS X 10.6 or earlier the following Terminal command is used to flush DNS instead:

dscacheutil -flushcache

All Done!

And that’s it, you’ve now changed your Mac OS X hosts file and can confidently do it again whenever you want using a few Terminal commands.

Let us know what you’ve been doing to your hosts file in the comments, below.

The comments were closed because the article is more than 180 days old.

If you have any questions related to what's mentioned in the article or need help with any computer issue, ask it on MakeUseOf Answers—We and our community will be more than happy to help.


0 votes


Good article. Is there a way to have one centralized host file? Lets say i’m moderating 10 macs either on 1 network or in different parts of the country, can i have all macs draw their host file from 1 server?

I use it to block access to known bad hosts used in malware but haven’t done much on it since i started using Little Snitch.

0 votes

Tim Brookes

Thats a really good question, and I have no idea what the answer is! You could probably replace all target hosts with a “master” file via SSH with root access, but beyond that I don’t know how you’d actually do it.

Feel free to ask this on MakeUseOf Answers though: :)

1 votes


Lars van de Kerkhof created a Preferences Pane which does all this seamlessly. (Shame Apple blocked him from releasing it on the App Store.)

0 votes

Tim Brookes

Thanks for the link, Luke – that does indeed look like a very useful shortcut!

0 votes


Great with the backup of original and the history of changes.

0 votes


After editing host files on two Macs I will just say By, by terminal and Welcome Hosts.prefpane from specialunderwear!

0 votes


0 votes


I accidentally deleated my hosts file. How can I get it back?
Do I need to reinstall everything?

0 votes


Thanks for this – I trawled around hopelessly for an hour trying to find a way to prevent Hotmail/Outlook sending my wife and son to MSN on log out. Job done by assigning the necessary urls to – Cheers!