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

Over the pass week, We have received emails from subscribers on how to edit or modify the hosts file on a Mac?

This simple six step will walk you through a successful Mac Host file edit/modification.

You’ll find hosts in Mac OS X is stored at /private/etc/hosts but it can also be accessed at the more traditional location of /etc/hosts. That said, if you’re looking to edit hosts, you’ll want to target the file located in /private/etc/ though. We’ll walk through how to manually edit the hosts file in OS X Yosemite, OS X Lion, OS X Mountain Lion, and OS X Mavericks, this will be done with the command line using the simple text editor called nano. Don’t let the command line or Terminal sound intimidating though because it’s not, we’ll make the entire process super easy.

Let’s get started making some edits to /etc/hosts!

  1. Launch Terminal, found in /Applications/Utilities/ or launched through Spotlight
  2. Type the following command at the prompt:

sudo nano /private/etc/hosts

  1. Enter the administrator password when requested, you will not see it typed on screen as usual with the command line
  2. Once the hosts file is loaded within nano, use the arrow keys to navigate to the bottom of the hosts file to make your modifications
  3. When finished, hit Control+O followed by ENTER/RETURN to save changes to /private/etc/hosts, then hit Control+X to exit out of nano
  4. Quit out of Terminal when finished

You can verify your hosts modifications immediately with ping, Safari, or any other network app. Changes take effect immediately though some adjustments may need to be accompanied by a DNS flush which can be done with the following command in OS X 10.9:

dscacheutil -flushcache;sudo killall -HUP mDNSResponder

When flushing DNS cache with that command you will need to enter the admin password.


Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.