Messed Up Hosts? How to Restore the Original Default /etc/hosts File in Mac OS X

The hosts file is included on every computer and used by Mac OS to map IP addresses to host names. Because users may choose to adjust, change, or otherwise edit the hosts file for a variety of reasons, it can be easily subjected to user error, leading to a variety of undesirable network problems ranging from inaccessible network locations, network failures, web sites blocked or otherwise unable to load, even to failed iOS updates and various iTunes errors like the frequently encountered 17 and 3194 errors because the Apple servers have been blocked.

Fortunately, restoring the original default /etc/hosts file back to normal is pretty easy, and the best way to get back the original untouched default file is to simply overwrite the existing damaged hosts file with a new clean version that is a copy of what comes default in Mac OS X. An example of that has been included below for convenience, but you can retrieve it from another Mac if need be as well. No additional entries or modifications are included in the version below, which is a direct replica from that found in OS X Mavericks, making it safe to return to if you accidentally messed up the important hosts document during a change or adjustment.
You’ll want to copy the text below and paste it into a plain text file, stored at the /etc/hosts path. If you’re not sure how to do that, you can follow these instructions to learn how to edit the file the proper way from the command line and simply overwrite it with the hosts block below, or use TextEdit and save over the messed up version, which is what we’ll walk through below.

The Default & Original /etc/hosts File in Mac OS X Looks Like This

Contained within the code block is the original hosts file and the four default entries. Just copy and paste this over an existing hosts file then save it as plain text to restore it.

##
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost
fe80::1%lo0 localhost

Those familiar with the command line shouldn’t have a problem with this, but if you’re not quite sure what to do you can also complete the process from the TextEdit app as we’ll describe below:

Restore an Unmodified Original Hosts File to Mac OS X

TextEdit is the simple text editor bundled with every Mac, you’ll also need administrator access to complete this task since the hosts document is a system file in a restricted directory.

Open TextEdit and paste the above code block into a new empty blank file

Select All text and choose “Format > Make Plain Text” and click “OK

Choose “File > Save As” and uncheck the box for “If no extension is provided use txt” – this is important, DO NOT INCLUDE A FILE EXTENSION

Hit Command+Shift+G to bring up the “Go To Folder” window, now type in /etc/ and go

Name the file ‘hosts’ and save, you’ll need to enter an administrator password to be able to write to this directory

Now to confirm the hosts file saved properly, go to the Terminal app and type the following:

cat /etc/hosts

That command should report the file to look like this:

If it doesn’t look like the sample hosts file above, you did something wrong. The most common problems are usually not saving the file as plain text, accidentally adding the file extension, or naming it incorrectly, so double-check that. If you’re still having problems, you may not have overwritten the file properly.

You’ll probably want to flush DNS cache or just reboot the Mac for changes to take effect system-wide and have the hosts file restored.

This is really the easiest way to restore the hosts file if you have messed it up, if it has become overly cluttered with tons of entries, or somehow other rendered the hosts database completely unusable. You certainly don’t need to restore an entire Mac from a Time Machine backup or reinstall the OS to accomplish this.

Enjoy this tip? Subscribe to the OSXDaily newsletter to get more of our great Apple tips, tricks, and important news delivered to your inbox! Enter your email address below:

This method changes the permissions on the hosts file!
You have to use Nano (in Terminal) or Textwrangler to avoid that. I am not aware exactly what the vulnerabilities are of changed permissions on the hosts file, but you should NOT change them.

If you cat it:
##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting. Do not change this entry.
##
127.0.0.1 localhost
255.255.255.255 broadcasthost
::1 localhost

So, basically you just need to remove the current hosts file and copy the hosts~orig into /etc/hosts.

N.B : this won’t work on OSX El Capitan since the /etc/ folder is considered a restricted zone and thus every file in it is unmodifiable.
instead you’ll have to edit the hosts file using terminal and thats what i did.