Let’s get started making some edits to /etc/hosts!
- Launch Terminal, found in /Applications/Utilities/ or launched through Spotlight
- Type the following command at the prompt:
sudo nano /private/etc/hosts
- Enter the administrator password when requested, you will not see it typed on screen as usual with the command line
- 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
- 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
- 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.
Source: http://osxdaily.com/2012/08/07/edit-hosts-file-mac-os-x/