Take the 2-minute tour ×
Ask Different is a question and answer site for power users of Apple hardware and software. It's 100% free, no registration required.

How do I open a file as root, in TextEdit? I've tried these commands as root, but TextEdit always say it's locked:

open -e /etc/apache2/httpd.conf
open -e -F /etc/apache2/httpd.conf
open -e -F -W /etc/apache2/httpd.conf

And of course sudo !! makes no difference.

share|improve this question

6 Answers 6

up vote 11 down vote accepted

You can open up a textedit instance as root by entering the full path to the actual executable :

sudo -b "/Applications/Textedit.app/Contents/MacOS/TextEdit"

Once your root instance is open you could browse to the file you need or do this from the command line :

sudo su - root -c "open -e /etc/apache2/httpd.conf"
share|improve this answer
    
Thanks, it worked! sudo /Applications/Textedit.app/Contents/MacOS/TextEdit then just open the file from TextEdit –  Znarkus Aug 5 '11 at 17:30
    
If you don't want to type that out every time you can create an alias by going to your home directory in Terminal, then pico .bash_profile, add alias sudotext='sudo -b "/Applications/Textedit.app/Contents/MacOS/TextEdit"', save file, restart Terminal. Now you can just type sudotext to start TextEdit as root. –  webbiedave Mar 4 '13 at 22:24
    
As other answers pointed out, to do it in 1 liner the only real option is having a 3rd party editor that supports it. Then just type open -t "/etc/hosts" for instance. With TextWrangler you can simply type edit /etc/hosts. –  Cawas May 2 '13 at 11:40
2  
What a horrible user experience! Isn't there an easier way? –  Marc Jun 4 '13 at 13:48
    
Why the hell have they made it so difficult for me to do things dam it! –  Jamie Hutber Mar 8 at 10:55

TextEdit isn't really the right tool for editing config files -- use TextWrangler instead. It's free, has built-in capability to edit files with root access from an admin account, as well as things like opening invisible files and directories easily editing files over SFTP, etc.

share|improve this answer
    
emacs has a nice colour-coded conf mode, too. –  msanford Jan 27 '12 at 15:02
    
Much better solution, cheers for this. –  Swader Jun 17 '12 at 15:09
    
This doesn't seem to work for me in Mountain Lion. I get an error when trying to save system config files in TextWrangler. Has this changed? –  Simon Sep 22 '12 at 15:21
    
@Simon: Sounds like you have the version of TextWrangler from the App Store, rather than the direct download from barebones.com. If so, it's had several features (including this one) removed to comply with App Store security policies; so dump it and get the direct version instead! –  Gordon Davisson Sep 23 '12 at 4:45
    
What if you're on a foreign Mac without internet? The 2 hacking terminal lines accepted solution is still generally better. But for practical day-to-day usage, I go with 3rd parties all the way! :-) –  Cawas May 2 '13 at 11:28

Here's a way to avoid running TextEdit as root:

EDITOR='open -Wne' sudo -e /etc/apache2/httpd.conf

You will need to quit the copy of TextEdit after editing the file.

sudo -e, sometimes known as sudoedit but not on OS X, makes a temporary copy of the file with write permission for the current user, invokes an editor on it in the usual Unix fashion, and then copies it back.

The options to open: -W waits for TextEdit to quit, so sudo knows when to copy the file back. -n ensures that it's waiting on a separate instance of TextEdit, not one you already have open which you might not want to quit. You can also substitute -t instead of -e if you have a favorite text editor other than TextEdit.

If you already have an EDITOR variable set to use a graphical editor with its own wait-capable command line tool (such as TextMate or BBEdit), then you don't need any of these tricks and can just use sudo -e <file> directly.

share|improve this answer

Since the root user is disabled, the only way to force an arbitrary OS X app to have root permissions is to enable root and log in as root.

Apps can of course use API to ask the system for authentication and pop up the dialog you expect when asked for an administrator user and password. TextEdit doesn't have that function so you have to work around the file permissions before and after you open and write the files desired.

Have you tried using sudo open -a textedit to open the app?

share|improve this answer
    
Do you have suggestions on apps that does have that functionality? Preferably free :) –  Znarkus Aug 5 '11 at 17:26
3  
This isn't quite accurate. In Terminal.app I run sudo su - to start a shell as root and then I can run open "/Applications/Textedit.app/Contents/MacOS/TextEdit" successfully. I do not have the root user enabled nor have I logged in as the root user. –  EmmEff Aug 5 '11 at 19:18
sudo chmod +w name_of_the_file
sudo open -a TextEdit name_of_the_file
sudo chmod -w name_of_the_file

Should work

share|improve this answer
    
Okay, so it's not possible to launch TextEdit as root? I'd really like to avoid the extra hassle of changing permissions back and forth. –  Znarkus Aug 5 '11 at 13:06
1  
Things have changed in Lion, I guess it's the sandboxing –  Paul Eccles Aug 5 '11 at 13:09
    
The point is that the file you are trying to open are not writable to root because of their permission. It would be the same for one of yours files (actually it is the same, except for the fact that TextEdit is "intelligent" for Users' files, so it changes the permission by asking you). –  Gio Aug 5 '11 at 13:12
    
@Gio Indeed, it's due to Lion's sandboxing: /etc/apache2/httpd.conf has default permissions 0444. I've been trying all morning to edit that file (with emacs remotely) but never thought to check the (new Lion) permissions. –  msanford Jan 27 '12 at 15:11
sudo nano /etc/hosts 

works for Lion

share|improve this answer
1  
The question is about TextEdit, not nano. –  gosmond Feb 21 '13 at 7:19

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.