How to edit the hosts file in Mac OS X

The hosts file is a plain text computer file used in an operating system to map hostnames to IP addresses.

Editing the hosts file, in Mac OS X, is a fairly easy process. The steps that need to be followed are listed below:

  1. Open Terminal by typing “Terminal” on the Spotlight.
  2. Open the hosts file by typing (on the Terminal):
    sudo nano /private/etc/hosts
    When prompted, type your password.
  3. Edit the hosts file by navigating using the arrow keys. You can either append new mappings under the default ones or edit the default values (i.e. localhost).
  4. Save the hosts file, when done, by:
    1. using the keyboard shortcut Ctrl + O to save the file
    2. pressing “Enter” key on the filename prompt
    3. using the keyboard shortcut Ctrl + X to exit the editor.
  5. Flush the DNS cache by typing (on the Terminal):
    dscacheutil -flushcache