How to Edit Hosts File in Mac OS X

The Mac hosts file is an important text document on Mac OS X that allows the mapping of hostnames to specified IP address. Even though the Internet has both public and private DNS servers for mapping of IP address, the hosts file on Mac is a good way to override DNS servers. When you edit hosts file on Mac it allows users to manually direct a website address to a specific IP address or block access to a site altogether by pointing to an unused or internal IP address. The follow will teach users how to edit the Mac hosts file on OS X.

 

Edit Mac Hosts File with Text Edit

There are a couple of different ways to edit the hosts file in Mac OS X. One method is to use the default TextEdit feature for Mac host file editing. With the Mac hosts file being a text document, using TextEdit will be a good enough tool as a Mac hosts file editor. But it’s important to note that TextEdit can’t open the file directly and you will need to copy the file to an unprotected location, like the Desktop, edit it, and then copy it to TextEdit to edit hosts file on Mac.

To find the hosts file, open Finder and, in Finder’s menu bar, select Go > Go to Folder. In the box, type the following location and press Return.

/private/etc/hosts

edit-hosts-file-osx

A new Finder window will open and your Mac’s hosts file will be selected. Click and drag it out of the Finder window and drop it on your desktop. This will let us freely edit the file.

edit-hosts-file

When you want to open the Mac hosts file default, all you need to do is double-click and it will display the file’s contents in TextEdit. By default, the hosts file is simple since it only contains a number of lines of descriptive text which are “commented out” with the pound or number sign (#). The host file also has IP values for localhost and broadcasthost. To edit the file, you’ll add your own lines after broadcasthost.

edit-hosts-file

A great use for an edited hosts file on your Mac is to block access to specific websites. (How to Block A Website on Windows) It’s required to type the IP address that is wanted to be blocked and to assign followed by a hostname. In our case, we want to block YouTube, You’ll map www.youtube.com to 0.0.0.0 which, as an invalid IP address, will result in an error. Now, whenever anyone using the Mac tries to go to www.youtube.com, the Web browser will fail to load the page.

Another method is to type the IP address of a valid site instead of 0.0.0.0. To find a website’s IP address, you can “ping” the site via Terminal. Open Terminal and type the following command, replacing “www.website.com” with the website of your choice:

ping www.website.com

After the changes have been made to the host files on Mac, save it in its current location on the Desktop. Then, drag and drop the hosts file from your Desktop back to its original location at /private/etc. If the Finder window is closed, use the Finder > Go > Go to Folder command, to reopen it.

After the hosts file has been dropped to its original location, OS X will ask you what to do about the unmodified hosts file that’s already there. Choose “Replace” and then enter the administrative user password to authenticate the transfer.

Now open a Web browser to test the changes. The the correct changes have not occurred, it may be needed to clear the DNS cache. For OS X Lion and OS X Mountain Lion, open Terminal and use the following command. Note that you’ll need to enter your admin password to execute it:

sudo killall -HUP mDNSResponder

For OS X Mavericks, use this command instead:

dscacheutil -flushcache; sudo killall -HUP mDNSResponder

 

 

 

Disclaimer: Some pages on this site may include an affiliate link. This does not effect our editorial in any way.

Todays Highlights
Zoom How to Change Name