Every Mac stores a hosts file in the hidden /etc folder, which is used by the operating system and your web browser to map particular hostnames to certain IP addresses. When you enter a website URL address into the Address Bar of Safari, for example, OS X will first check for a corresponding mapping in the hosts file, before moving on to the DNS server to find the required IP address.
For the vast majority of Mac OS X users, the hosts file is something they will never be required to touch. However, for more advanced users, the hosts file can have two particular uses.
Firstly, if you are involved in web programming, using the hosts file can allow you to redirect a domain to a local address on your Mac, where you can test out code “offline” before applying it to the actual website.
The second instance where you may want to edit the hosts file is because it is a highly effective means of blocking known suspicious sites that may attempt to deliver unwanted advertising to disrupt your surfing experience, or even download viruses or other malware to your computer.
Editing your hosts file incorrectly can have adverse effects on your Mac. You should only attempt to edit the hosts file if you have a clear purpose for doing so and are confident in your understanding of Terminal commands.
Editing the Hosts File in Mac OS X
1.) To begin editing the hosts file, you will need to use a Terminal window. Open one either by clicking on Applications, then Utilities and then Terminal, or by pressing Cmd-Spacebar to open Spotlight, typing Terminal and pressing Return. The Terminal window should look like this:
2.) We now need to open the hosts file for editing in the Terminal. We do this be entering the following command (or Copy and Paste from below) at the Terminal prompt:
sudo nano /private/etc/hosts
Press Return. Terminal will ask you to enter your Administrator Password:
Do so and again press Return.
3.) The hosts file will open in the Terminal window for editing:
To navigate the hosts file, use your arrow keys to move up and down. The lines beginning with the hash (#) symbol are comments and should not be edited. There will also be some default mappings, such as the entry 127.0.0.1 for localhost.
4.) Use the ↓ Arrow Key to move the cursor to a blank space at the bottom of the hosts file, and enter your new mapping here. In the following example, we are adding an entry to prevent the Doubleclick Advertising server from sending files to our web browser:
5.) To save the hosts file after editing, press Ctrl-o. This will bring up a prompt for the file name to write to:
Press Return to save the hosts file. You can then exit editing the hosts file by pressing Ctrl-x. This will return you to the Terminal prompt:
6.) For the changes to your hosts file to take effect, you must first flush your DNS cache. To do so, enter the following command (or Copy and Paste from here) at the Terminal prompt:
Press Return. You can now exit the Terminal and your new host mappings will be in effect.
For more information on using the hosts file to prevent adware, spyware and other malicious software, we recommend visiting Blocking Unwanted Parasites with a Hosts File. The site tracks all reported problematic host names, and prepares a list of problem sites that is updated every few weeks.
The site is primarily Windows-focused, but the problem sites listed in their hosts .txt file can be used in the OS X hosts file by replacing each instance of “127.0.0.1″ with “0.0.0.0″. Please note this does not include the “127.0.0.1 local host” entry mentioned above, which should never be altered.
For advanced users, editing the hosts file can be very useful in certain situations. However, we once again emphasize that you should not attempt to do so unless you are confident in using the Terminal and have a clear reason to edit the OS X hosts file.