OS X: Edit a file using TextEdit as root or superuser

Editing some files on an OS X system requires superuser or root permissions. Typically, this is accomplished using sudo (which lets authorized users assume superuser powers, cape and tights optional) and vi. To the uninitiated, vi can cause intestinal distress and hair loss. An alternative is the use of TextEdit, the graphical text editor application, but under normal circumstances, you can open a system file like hosts but cannot save it. Following the steps in this recipe, you can edit a system file using TextEdit and put off learning vi for another day.


The sudo command line application allows a user to run another application as though they are another user. Most commonly (and by default) the other user is root, the superuser of the system who can do anything to any file on the box. In this case we want to launch the TextEdit application.

If you’ve never poked around at an application in OS X, it’s interesting to take a peek. When you look at the /Applications folder in a finder window, you’ll see the applications listed by name such as TextEdit. If you list the files from the command line ( ls -l /Applications ) they are actually directories, not simply files, and have a .app extension like TextEdit.app. You can change directory into one of these directories and look around. If you look in a few, you’ll see that there is some standard organization in them and that should make you feel a little warmer and possibly fuzzier about this whole situation. This directory structure allows the applications to have many supporting files and resources neatly contained within them. Of interest to us is the location of the actual executable file inside the app directory. In the case of TextEdit, the path to this executable is /Applications/TextEdit.app/Contents/MacOS/TextEdit

Based on this information, running TextEdit with sudo to edit the /etc/hosts file becomes a simple task (if a rather long command line, sorry):


sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts

In many systems this terminal command may work better:

sudo open -t /etc/hosts

When you run this command from a command line (in a Terminal window), you’ll be prompted for a password. The correct password is your own (the password of the user logged into OS X, also the password you provide when doing system updates and so on), not the root user’s password (which doesn’t exist by default, anyway).

Once you enter your password, a TextEdit window will open and the contents of the file you are editing will appear. Edit to your heart’s content and save normally when you are done. Just keep in mind that most system files are protected from writing for a good reason (like if you make a mistake editing them, bad things may happen) so edit at your own risk.

Update:

The versioning functions of OS X Lion will prevent this tutorial from working correctly. Even though you are running as a superuser, the file within textedit will be locked. Since Lion has rolled out, I have reverted back to the old standby of using nano to edit.

sudo nano /etc/hosts

Sure nano has that retro terminal feel, but it’s an easy enough method to make simple host file changes.

 

About Quinn McHenry

Quinn was one of the original co-founders of Tech-Recipes. He is currently crafting iOS applications as a senior developer at Small Planet Digital in Brooklyn, New York.
View more articles by Quinn McHenry

The Conversation

Follow the reactions below and share your own thoughts.

35 Responses to “OS X: Edit a file using TextEdit as root or superuser”

  1. December 02, 2008 at 9:14 pm, mori said:

    I get this error trying this hint:

    2008-12-02 22:13:54.216 TextEdit[1144:c17] *** CFMessagePort: bootstrap_register(): failed 1100 (0x44c) ‘Permission denied’, port = 0×4403, name = ‘com.apple.TextEdit.ServiceProvider’
    See /usr/include/servers/bootstrap_defs.h for the error codes.

    Reply

    • December 02, 2008 at 11:25 pm, davak said:

      I get that error sometimes too. It shows up in the terminal after textedit is loaded. It still works fine though.

      Reply

  2. June 10, 2009 at 8:42 pm, Hank Z said:

    Does this still work under Leopard???????

    I enter

    sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts

    Then my password and Text Edit does not open the HOSTS file.

    Is something Wrong

    Reply

    • June 11, 2009 at 2:19 pm, Gerry said:

      make sure ur password isn’t blank. Mine was and I had the same problem. But once I changed my password to sth else, it works perfectly.

      Reply

      • July 08, 2010 at 6:51 pm, dfloyd said:

        sorry but what do you mean by changing password to sth else (something else)? sorry for being a noob, why doesn’t my regular one work do I have to change it?

        Reply

  3. September 11, 2009 at 4:40 pm, Anonymous said:

    Very useful. I implemented this in my login shell so that I can call it with “textedit”
    Steps:

    1: open ~/.profile in textedit with terminal command “open -t .profile”
    2: insert the line “export PATH=”$PATH:/Applications/TextEdit.app/Contents/MacOS/”
    along with a commented (#) note why I was doing this
    3: save that text file and close down terminal
    4: open a new terminal window, enter “echo $PATH” to ensure that this new entry showed up somewhere in the path.
    5: I can now use textedit as a superuser with “sudo textedit”

    It saves about a few keystrokes in the long run if you like to edit previously uneditable files (but be careful!). Always save a backup of the file you are editing with the command:
    “cp foo.txt foo.backup”

    Reply

  4. November 24, 2009 at 10:07 am, Kyle Palaniuk said:

    Great explanation. I usually don’t write comments like this but really, nice job. Probably the most clear explanation you can get, and I successfully blocked what I had hoped to. Thanks!

    Reply

  5. May 21, 2010 at 11:43 pm, Ivancho said:

    this is why I like Ubuntu. You just type sudo gedit /etc/hosts, and voilá.

    Altough, I wonder if you can add textedit to the system path… you probably should be able to, because is *NIX.

    Reply

  6. June 24, 2010 at 7:28 am, d a n i : ) said:

    It is easier if you just do a
    $ sudo open -t /whaeverfile
    This will open it in textedit.

    Reply

    • June 24, 2010 at 4:53 pm, Anonymous said:

      Woah. I love it! Great information! Much better.

      Reply

    • June 26, 2010 at 3:02 am, ercasul said:

      For precision: sudo open -t /whateverfile “causes the file to be opened with the default text editor, as determined via LaunchServices” whereas sudo open -e /whateevrflle will explicitly open it in TextEdit. I’d guess most ppl still have TextEdit as default editor , so the difference is likely to be invisible to most, but thought I’d mention it.

      Reply

      • June 26, 2010 at 4:13 pm, Anonymous said:

        Very cool. Thanks for the additional information.

        Reply

        • April 23, 2011 at 9:17 am, Hoax said:

          does not let me save the changes due to restricted access…

          Reply

  7. July 11, 2010 at 9:19 pm, Elixir said:

    This method does not work for editing conf files such as ones for macports.

    Reply

  8. July 13, 2010 at 1:03 am, Lance said:

    Superuser has nothing to do with editing the Hosts file. Any admin user can edit it. Go > Go to folder > /etc. Drag the Hosts to the desktop to make a copy. Edit the copy in TextEdit. Drag the dopy back to the /etc window. Click the “Authenticate” button, type the admin password. Done.

    Reply

  9. December 25, 2010 at 3:47 am, A337991 said:

    You can also define an Alias to reduce your typing effort.
    Add the following line to your ~/.bash_profile file (or create one):

    alias edit=’sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit’

    After restarting the terminal you can easily edit files that requires root permission.
    Just like that:

    edit /etc/hosts

    Reply

  10. December 31, 2010 at 3:49 am, ones said:

    Thanks for your post, i like….
    I want to ask a question, when i want to save i found error message “The document “hosts” could not be saved. You don’t have permission.” To view or change permissions, select the item in the Finder and choose File > Get Info.

    Please helpme, because i’m a beginner….. Thanks before

    Reply

  11. January 12, 2011 at 8:33 am, Phil said:

    Thanks Lance – that was so easy! I didn’t really understand all the other stuff but your method works fine – thank you!!

    Reply

  12. January 17, 2011 at 12:15 am, Victoria Lorelli Lessell said:

    Is there someone out there that can help me? I need to edit a file in root ( whatever that means) and I am having SUPER trouble. Its my first MAC and I am having issues. Can someone please help?

    Reply

  13. January 25, 2011 at 7:38 pm, Sean Jost said:

    It seems that sudo is ignored in the command “sudo open -t /whateverfile”.

    Reply

  14. February 11, 2011 at 4:01 pm, Hydramatic said:

    It’s much easier to just log on as ”root” and change anything right from the text editor.

    Reply

  15. May 04, 2011 at 5:11 am, Greg said:

    Or you could learn a bit of emacs.

    sudo emacs /etc/hosts
    Then ^ N a few time down to the line you want to edit.
    ^ F to move forward and ^ B to move back.
    ^ = Control
    ^ P = previous line
    The to save the file ^ X followed by ^ S.
    To exit ^ X ^ C.
    It seem weird at first but it’s well worth the effort.
    You will find that many Cocoa apps use emacs keys but don’t advertise them.

    Reply

  16. July 28, 2011 at 8:13 am, Anon said:

    from the terminal:

    sudo nano /etc/hosts
    enter your PW and bam, you are now able to edit the file. to save changes hit “control+o” to write the output to the file, when asked for the file name just hit enter. “control+x” to close nano.

    Reply

  17. August 04, 2011 at 10:54 am, Paloma Jiménez said:

    thanks!!!!!

    Reply

  18. September 13, 2011 at 4:26 pm, x said:

    that short hand command “sudo open -t /etc/hosts” worked for me! THANKS :D

    Reply

  19. September 17, 2011 at 2:14 am, Steve said:

    This seems to be broken in Lion. Works for me in Snow Leopard

    >sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts

    Reply

  20. September 27, 2011 at 10:59 am, James said:

    Works for me (i user snow leopard)
    just need to login before then edit the host file :)

    Reply

  21. September 30, 2011 at 5:43 am, Nareille said:

    I think this command broke my xampp! Everytime ever since when i change something in my vhosts file and restart apache, it says there is another server running on port 80!
    The only way to “fix” it is to reboot.

    I wanna undo this step, but whenever I open the file, it is opened with sudo permissions. Even the icon changed. How can I undo it?!

    Reply

  22. October 07, 2011 at 8:11 am, ethomas said:

    I plugged in (sudo open -t /etc/hosts) into the terminal put in my password and it says there is an error in the textedit and then i go manually look at text edit and it says the “hosts” file is locked so i can’t do anything…what should i do, or can i do anything.

    Reply

  23. December 28, 2011 at 9:18 am, Heuristo said:

    Thanks very much for this information. Especially Greg for the elaboration on using emacs which was fun since I used it every day back in the early 1980′s and loved it. Cobwebs heavy over there but I love how snappy emacs is.

    Reply

  24. March 08, 2012 at 5:32 am, Rat said:

    I have a slightly different problem. I want to edit a crontab, but I want to use (say) TextEdit instead of the default editor.

    I’m sure I can export EDITOR to do this, but dont know the exact syntax… can you help

    Reply

  25. April 20, 2012 at 4:41 am, Luke said:

    Using a fresh install of Lion and when I use the “sudo open -t /etc/hosts” command to exit my hosts file textpad opens but it still has “locked” at the top and I don’t get prompted for my system password? Any idea what I’m doing wrong.

    Reply

  26. July 17, 2012 at 8:55 pm, Tom said:

    Simply just use TextMate App, once you’ll save the host file, it will ask for pw. No more woodoo with the terminal.

    Reply

  27. July 19, 2012 at 1:15 pm, Susan said:

    Tom is right, just use an alternative text editor to open the hosts file and save it normally.

    Reply

  28. July 21, 2012 at 6:07 am, Dave said:

    I was doing some fooling around when I shouldn’t have been and now when I run this, I get the following message:

    2012-07-21 00:05:01.798 TextEdit[11872:d07] Error loading /Library/QuickTime/DivX Decoder.component/Contents/MacOS/DivX Decoder: dlopen(/Library/QuickTime/DivX Decoder.component/Contents/MacOS/DivX Decoder, 262): no suitable image found. Did find:
    /Library/QuickTime/DivX Decoder.component/Contents/MacOS/DivX Decoder: no matching architecture in universal wrapper

    Anyone know how I can revert back?

    Thanks

    Reply

Leave a Reply