The Mac Hosts File: How to Modify /etc/hosts in Mac OS X with TextEdit

Feb 29, 2016 - 15 Comments

Edit the hosts file in TextEdit in Mac OS X

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.

  1. Quit TextEdit if it is currently open
  2. Launch the Terminal application in Mac OS X, found within /Applications/Utilities/
  3. Enter the following command exactly to open the Macs hosts file within the TextEdit GUI application
  4. sudo open -a TextEdit /etc/hosts

  5. Hit return and enter the admin password for Mac OS X when requested to authenticate the launch through sudo
  6. How to modify the hosts file in TextEdit of Mac OS X

  7. 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 *
  8. Quit out of TextEdit, then quit out of Terminal when finished

How to Edit the Mac hosts file in TextEdit for Mac OS X

* 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.

.

Related articles:

Posted by: Paul Horowitz in Mac OS, Tips & Tricks

15 Comments

» Comments RSS Feed

  1. This worked for me:
    sudo code /etc/hosts

  2. Jon says:

    Disabling SIP is an awful idea. Shame for encouraging it for something as simple as editing a hosts file.

    • Cabbage says:

      This is how you edit the hosts file Jon, a prerequisite is not encouraging anything. If you want to modify your hosts file, you have to disable SIP. That’s all there is to it. You can re-enable SIP afterwards if you want. If this is all over your head, perhaps you should not be editing a hosts file to begin with.

      Also SIP is fairly new, it did not exist in recent versions of Mac OS. I wouldn’t expect an ipad or windows user to know that however, you have to be well versed in the Macintosh. Complex stuff!

  3. Jan Steinman says:

    This is generally A Bad Idea™ to use for anything outside your LAN. It can badly and inexplicably mess up your Internet access.

    • Wall-E says:

      Editing the hosts file is fine if you know what you’re doing. If you don’t know why you’d want to edit the hosts file, you have no business editing the hosts file, and thus it could go wrong or inexplicably mess something up.

      It’s kept at /etc/hosts for a reason, it is not intended to be accessed by novice Mac users. Advanced Mac users only.

      I have seen many users who don’t know what they’re doing think they know more than they do and get themselves into trouble, applies to computers, phones, technology, motors, medicine, anything technical or complex really.

  4. LexS says:

    Textedit changes the permissions on the hosts file, I am not sure that is a problem. afaik textwrangler does not change the permissions. disabling SIP is not necessary.

  5. jan says:

    i´m still using gas mask app easy switching between different hostfiles .. editing .. adding combined hostfiles .. adway entries .. for me essential setup …

    greetings jan

  6. Tyler J says:

    I second that TextWrangler is best to edit hosts files, can edit the file directly without anything else, no need to mess with SIP etc. I use VIM personally but for those who want a GUI, editing hosts with TextWrangler is the way to go.

    Also, the ones posting hosts list are just outrageously huge, way too big to be usable or adjustable. Impossible to manage. Those type of lists are unusable if you’re in marketing, advertising, and/or sales, where you need regular access to various web analytic and ad platforms.

    IMO keep it simple if you’re going to edit your hosts file. Block the primary garbage purveyor:

    127.0.0.1 facebook.com

    Better yet, block facebook on your household router and prevent anyone in the house from using the rubbish service. I’m sure there are other websites you’d also like to never see again, but facebook is the leader of useless crud if you were to ask me.

    • Jan Steinman says:

      I don’t see how putting facebook.com in your /etc/hosts file does anything but keep you from going to FacePlant. This will NOT keep FacePlant from sending you email. /etc/hosts is only for resolving names to addresses from YOUR machine’s point-of-view.

  7. John says:

    Been using this for a while…

    Make The Internet Not Suck (as much)
    http://someonewhocares.org/hosts/

    Just download that file (save it as text) and I use a terminal command to put it in place.

    sudo cp ~/Downloads/hosts.txt /etc/hosts

  8. Maged says:

    I’ve been using TextWrangler for years to do this. I do NOT have SIP disabled and am running El Capitan 10.11.3, and have no problem to edit the hosts file.

    Another way to do this is open the hosts file (from Finder, Go->Go to Folder… then type /etc/hosts, then right click, open with… then select TextWrangler).

    As soon as you try to modify the file, it will ask you are you sure you want to unlock, then when done, and you go to save it, it will automatically prompt you for admin credentials, then saves it.

    Granted, the command line is actually easier, but it doesn’t actually perform the privilege escalation until it’s really needed, which is probably better for protection from self :)

    In fact this is the first thing I do when I setup a new Mac from scratch. Download TextWrangler, and (after backing up the default hosts file), update the hosts file to block many known malware, tracking, ad sites, etc., from here:
    http://winhelp2002.mvps.org/hosts.htm
    (even though the URL says 2002 this list is regularly updated, and no it’s not a single fool-proof solution, but I found it helps in parallel with other security measures.)

    Just copy and paste the list into the hosts file, after the existing entries. Some referral links may not work with this block list in place (since they typically chain tracking links), but you can selectively comment out (with a #) the sites you want to allow if/as you need them.

    Regardless of why you may need to edit the hosts file, I would recommend trying without disabling SIP as it is actually a good thing to have enabled. And if you have to disable it, best to re-enable it again.

    • Pablo says:

      I agree, TextWrangler is a much better choice for editing system files, hosts, any code, plain text, etc.

      TextEdit is basically Notepad for Mac, good for a pinch, but not for anything complex.

      • Doug says:

        I tried to open the hosts file with TextWrangler but it is gray and not selectable in the Open dialog window. TextWrangler does not have permissions to open the /etc/hosts file or /private/etc/hosts

        I was able to open /etc/hosts with sudo open -a TextWrangler /etc/hosts however so it appears you must launch the editing app as root user

  9. Wellington says:

    It’s best to edit hosts file using nano, vim, emacs and stay within the command line.

    Most Mac users are novices, and novice users have no business in hosts file at all. Adjusting such a file with TextEdit could lead to saving it as an RTF or some other format which would render it inoperable and cause problems.

    My 2 cents.

    • Todd Stein says:

      I use TextWrangler and BBEdit for syntax editing, highly recommended. Both work with Hosts file without disabling SIP or using sudo.

Leave a Reply

 

Shop on Amazon.com and help support OSXDaily!

Subscribe to OSXDaily

Subscribe to RSS Subscribe to Twitter Feed Follow on Facebook Subscribe to eMail Updates

Tips & Tricks

News

iPhone / iPad

Mac

Troubleshooting

Shop on Amazon to help support this site