/etc/hosts file on Mac OS X

November 29, 2011 — 7 Comments

The /etc/hosts file on the Mac can be hard to find. Here are some tips for finding and editing it, and what you can do with it.

Where is the /etc/hosts file in Mac OS X?

The hosts file used by Mac OS X is in /etc/. Windows users sometimes have problems finding it since there’s no %SystemRoot%\system32\drivers\etc folder in the Mac OS. UNIX users might not expect /etc/ to be hidden by the Mac OS X Finder, so they often look in /Library/Application Support/.

Accessing /etc/hosts

/etc/ is hidden by default in the Finder. The easiest way of getting to the mac hosts file is to open /Applications/Utilities/Terminal.app. Then type:
sudo nano /etc/hosts

…you will need to type in your password. Your hosts file will open in the text editor nano.

Why ‘sudo nano’?

The hosts file, in addition to being hidden, is also protected by the system. Using sudo makes the editor nano run as a super-user, which makes editing the file possible.

Adding entries to the /etc/hosts file on Mac

In nano, on a new line type the IP Address followed by at least one space or tab and then the hostname. Examples:
127.0.0.1 mydevsite.local
192.168.1.222 ads.shadycompany.biz

…hit ctrl + o to Save, and then ctrl + x to Quit the text editor.

 

Background: What is /etc/hosts used for?

The hosts file is a simple way of mapping IP Addresses to hostnames. It overrides mappings provided by the Domain Name System network. The hosts file is commonly used by network administrators to provide access to LAN resources when no name resolution is available. The other common usage is to block access to specified hosts by adding host entries resolving them to — for example — 127.0.0.1.

Another use for the hosts file is for easily allowing your Mac to run multiple virtual servers, mainly useful for web developers. See here for more about using /etc/hosts for Virtual Servers: Adding an entry to /etc/hosts on OS X.

More Resources:
http://en.wikipedia.org/wiki/Hosts_(file).

terminal, applescript primer

If you find this page useful, check out my ebook. It contains an overhauled version of the hosts file info here, plus useful stuff to do with the Mac Terminal, bash, AppleScript, and Automator. Get to grips with the Mac Terminal and scripting tools now. More Info >

By Jay Docherty

Posts

7 responses to /etc/hosts file on Mac OS X

  1. You also also do the following:

    1. On Finder, click on “GO” –> “Go to Folder”
    2. Enter /etc
    3. Hit Go
    4. Copy the hosts file to another folder (not in /etc)
    5. Amend and paste the hosts file back to /etc (need to authenticate)

  2. how can I find the IP address for RTVE.es ?????? to open it?????

  3. To lookup the IP Address type nslookup rtve.es into Terminal.app and hit enter. The IP address is near the bottom under ‘Address.’

  4. Excellent way of explaining, and good post to take facts regarding my presentation focus, which i am going to deliver
    in college.

  5. Thanks for this clear and simple explanation. It was a breeze to setup and test my new host.

Trackbacks and Pingbacks:

  1. Update your computer to reduce online ads and speed up your internet, if you must | Life and all its quirks - February 1, 2013

    […] check out this site on how to open and edit the “hosts” file, using the command “sudo nano […]

  2. No puedo acceder a mi propio sitio web desde mi casa pero el iPad puede - March 8, 2019

    […] una entrada para su sitio en su / etc / hosts archivo que apunta a la dirección IP incorrecta? Ver esta página para detalles sobre cómo verificar si no sabes […]

Leave a Reply

Text formatting is available via select HTML.

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> 

*