The Mac Hosts File: How to Modify /etc/hosts in Mac OS X with TextEdit
The Mac hosts file is a system level file located at /etc/hosts which maps IP addresses to host names for Mac OS X networking. Many users edit and modify the hosts file so that they can point a domain to a different IP address, whether for the purpose of local development, blocking sites, or simply to access alternate servers from various apps and system level functions. Most advanced users will edit the hosts file from the Mac OS X Terminal using nano or vim, but for those who prefer to stay within the Mac OS GUI, you can also modify the Mac’s hosts file through TextEdit, or even a third party app like BBEdit or TextWrangler. This offers a more user friendly option compared to going through the command line.
If you don’t have a specific reason to modify the Mac hosts file in macOS / Mac OS X, you should not do so. An incorrectly formatted hosts file or improper entry can lead to DNS issues and problems with various internet services. This is for advanced users.
How to Modify the Mac Hosts File at /etc/hosts with TextEdit Mac OS
This approach to changing /etc/hosts with TextEdit works with any version of Mac OS X. For Mac users who are running MacOS X 10.11 or later releases, you must first disable SIP protection however, otherwise the Mac /etc/hosts file will be locked when attempting to access it from TextEdit.
- Quit TextEdit if it is currently open
- Launch the Terminal application in Mac OS X, found within /Applications/Utilities/
- Enter the following command exactly to open the Macs hosts file within the TextEdit GUI application
- Hit return and enter the admin password for Mac OS X when requested to authenticate the launch through sudo
- The /etc/hosts file will launch into TextEdit as a plain text file where it can be edited and modified as need be, when finished use File > Save or hit Command+S as usual to save the changes to the hosts document *
- Quit out of TextEdit, then quit out of Terminal when finished
sudo open -a TextEdit /etc/hosts
* If the hosts file shows as “locked” and won’t save changes despite being launched through sudo, it’s likely because you didn’t disable SIP as mentioned in the introduction. You can turn off SIP in Mac OS X with these instructions, which requires a reboot of the Mac. This is necessary for modern versions of Mac OS X, though you can choose to edit the hosts file using the command line with nano as described here without adjusting SIP.
It’s good practice to make a duplicate of the hosts file so that if you break something you can easily fix it, though we’ve got the original default hosts file here in case you need to restore it. It’s also a good idea to set plain text mode as the default for TextEdit.
You’ll likely want to clear out your DNS cache after modifying the hosts file, here’s how to flush DNS in Mac OS X El Capitan and modern versions Mac OS and how to do the same in prior releases.
Users can also choose to modify Mac OS X’s /etc/hosts with TextWrangler, BBEdit, or another third party application. The trick is largely the same as Text Edit, still requiring the use of sudo, but changing the specified application name as follows.
Opening /etc/hosts with TextWrangler:
sudo open -a TextWrangler /etc/hosts
Or launching /etc/hosts into Bbedit:
sudo open -a BBEdit /etc/hosts
While the aforementioned approaches work in all modern versions of Mac OS X, earlier versions of Mac OS X can also launch the TextEdit binary with hosts directly from the command line with the following syntax:
sudo ./Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts
That method will not work in the latest releases, however, thus you’ll want to rely on the open command instead.
Know of another trick to modify the Mac hosts file in an easy fashion through TextEdit or another GUI app? Let us know in the comments.