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
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:
Below in the comments, dani : ) described an easier method:
sudo open -t /etc/hosts
Thanks for adding to the discussion!






mori said on December 2, 2008
I get this error trying this hint:
2008-12-02 22:13:54.216 TextEdit[1144:c17] *** CFMessagePort: bootstrap_register(): failed 1100 (0×44c) ‘Permission denied’, port = 0×4403, name = ‘com.apple.TextEdit.ServiceProvider’
See /usr/include/servers/bootstrap_defs.h for the error codes.
davak said on December 2, 2008
I get that error sometimes too. It shows up in the terminal after textedit is loaded. It still works fine though.
Hank Z said on June 10, 2009
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 said on June 11, 2009
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.
Anonymous said on September 11, 2009
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”
Kyle Palaniuk said on November 24, 2009
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 said on May 21, 2010
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.
d a n i : ) said on June 24, 2010
It is easier if you just do a
$ sudo open -t /whaeverfile
This will open it in textedit.
Anonymous said on June 24, 2010
Woah. I love it! Great information! Much better.
ercasul said on June 26, 2010
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 said on June 26, 2010
Very cool. Thanks for the additional information.
dfloyd said on July 8, 2010
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?
Elixir said on July 11, 2010
This method does not work for editing conf files such as ones for macports.
Lance said on July 13, 2010
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 said on December 25, 2010
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 said on December 31, 2010
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
Phil said on January 12, 2011
Thanks Lance – that was so easy! I didn’t really understand all the other stuff but your method works fine – thank you!!
Victoria Lorelli Lessell said on January 17, 2011
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 said on January 25, 2011
It seems that sudo is ignored in the command “sudo open -t /whateverfile”.
Hydramatic said on February 11, 2011
It’s much easier to just log on as ”root” and change anything right from the text editor.
Hoax said on April 23, 2011
does not let me save the changes due to restricted access…
Greg said on May 4, 2011
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 said on July 28, 2011
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 said on August 4, 2011
thanks!!!!!
x said on September 13, 2011
that short hand command “sudo open -t /etc/hosts” worked for me! THANKS :D
Steve said on September 17, 2011
This seems to be broken in Lion. Works for me in Snow Leopard
>sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit /etc/hosts
James said on September 27, 2011
Works for me (i user snow leopard)
just need to login before then edit the host file :)
Nareille said on September 30, 2011
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 said on October 7, 2011
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.
Heuristo said on December 28, 2011
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.