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.

  • mori

    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.

    • http://blogs.tech-recipes.com/davak/ davak

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

  • Hank Z

    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

    • Gerry

      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.

      • dfloyd

        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?

  • Anonymous

    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”

  • http://kyle.palaniuk.net/ Kyle Palaniuk

    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!

  • Ivancho

    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.

  • http://tokao.com d a n i : )

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

    • Anonymous

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

    • ercasul

      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.

      • Anonymous

        Very cool. Thanks for the additional information.

        • Hoax

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

  • Elixir

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

  • Lance

    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.

  • A337991

    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

  • ones

    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

  • http://www.meltonmowbray.net Phil

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

  • http://www.facebook.com/profile.php?id=1352316639 Victoria Lorelli Lessell

    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?

  • Sean Jost

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

  • Hydramatic

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

  • Greg

    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.

  • Anon

    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.

  • Paloma Jiménez

    thanks!!!!!

  • x

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

  • Steve

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

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

  • http://www.pvcfonster.org James

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

  • Nareille

    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?!

  • ethomas

    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.

  • http://www.research2zero.com Heuristo

    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.

  • Rat

    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

  • Luke

    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.

  • Tom

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

  • Susan

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

  • Dave

    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