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

Tim Brookes 31-05-2013

mac os x hosts fileThe hosts file What Is the Windows Host File? And 6 Surprising Ways to Use It The Windows Hosts file allows you to define which domain names (websites) are linked to which IP addresses. It takes precedence over your DNS servers, so your DNS servers may say is linked to... Read More 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 How To Block Websites On Your PC Without Using Software [Windows] Any website you visit potentially tracks your browsing habits, loads annoying ads, or distributes malicious software. Most of the time, common sense and standard malware protection software can help you steer clear of the worst... Read More .

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.

mac os x hosts file

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.

mac os hosts file

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 4 Cool Things You Can Do With The Mac Terminal The Terminal is the Mac OS X analogue of the Windows command prompt, or CMD. It's a tool, as you probably already know, that allows you to control your computer using text commands, as opposed... Read More , 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 13 Ways to Make Up Passwords That Are Secure and Memorable Want to know how to make up a secure password? These creative password ideas will help you create strong, memorable passwords. Read More and hit Enter.

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


mac os hosts file

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.

mac os hosts file


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.

mac os x hosts file

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.

Related topics: Internet Filters, IP Address.

Affiliate Disclosure: By buying the products we recommend, you help keep the site alive. Read more.

Whatsapp Pinterest

Leave a Reply

Your email address will not be published. Required fields are marked *

  1. lil swede
    April 17, 2017 at 10:22 pm

    You literally saved my life. Thank you so much!

    • Tim Brookes
      May 5, 2017 at 4:35 am

      Literally? ;)

  2. Karolis
    February 6, 2017 at 11:09 pm

    Hi Tim,
    You saved me from hours of searching online. Your instructions worked like a charm.
    I use MAMP Pro for localhosts but for some reason this one of my locahosts was not removed by MAMP even after completely removing MAMP. After commenting the host in the hosts file I can access the public host.
    Thanks a lot

  3. Paula
    October 27, 2016 at 2:51 am

    I have done this to run a wordpress site that is not yet live. It worked well but now I need to get back to the live site and the command won't delete out of the terminal... what to do? Also, does anyone know if I can run both the live site and the non live site from the same machine?

    • Scott
      May 21, 2017 at 7:56 pm

      Did you ever find an answer to this? I'm in the same boat. I can't see my live site becasue when I visit the URL I can only see the the WordPress test site.

  4. P2Vak
    May 24, 2016 at 7:11 pm

    Thanks for the article. It helped me block sites that are not blocked with Parental controls such as buzzfeed and youtube. The Mac parental control does not work on HTTPS sites. Can I have multiple host files for different users on a MAC EI Captain?

    • Tim Brookes
      May 25, 2016 at 1:51 am

      No, changing the way the OS handles DNS will affect the whole computer. One thing that might work however is redefining a hostname to an IP address only you know, and then accessing it that way.

      e.g. ""

      I've not tried it but it should work.

      And on a slightly unrelated note, YouTube is a pretty good educational/entertainment resource. Is there no way to force a browser to adhere to YouTube's content ratings, say perhaps with a Chrome extension? It seems like something Google should implement, if they haven't already.

    • P2Vak
      May 26, 2016 at 2:51 pm

      Thanks again I will try the new recommendation you have. There is content control in youtube but my kids got around it :(. I just don't want them to access it at all. Thanks again :)

  5. Anonymous
    September 11, 2015 at 6:58 am

    Thanks for good article. I did the same as you guided su but with little complex. Anyway finally its working fine great.

  6. Ted
    November 4, 2013 at 11:03 pm

    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!

  7. Frederik
    September 1, 2013 at 10:17 pm

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

  8. Luke
    May 31, 2013 at 6:49 am

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

    • Tim Brookes
      May 31, 2013 at 7:52 am

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

    • macwitty
      May 31, 2013 at 4:12 pm

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

      • macwitty
        June 1, 2013 at 6:03 am

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

  9. Jay
    May 31, 2013 at 3:17 am

    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.

    • Tim Brookes
      May 31, 2013 at 7:50 am

      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.