15

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.

6 Answers 6

16

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"
7
  • Thanks, it worked! sudo /Applications/Textedit.app/Contents/MacOS/TextEdit then just open the file from TextEdit
    – Znarkus
    Aug 5, 2011 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, 2013 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.
    – cregox
    May 2, 2013 at 11:40
  • 2
    What a horrible user experience! Isn't there an easier way?
    – Marc
    Jun 4, 2013 at 13:48
  • 3
    Well, on my Yosemite 10.10.2 the sudo -b "/Applications/Textedit.app/Contents/MacOS/TextEdit" doesnt work :( Apr 6, 2015 at 0:23
8

BBEdit is the free tool to do this in 2019 and it replaces all functionality of TextErangler and you can download it from the App Store and the developer web site.


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.

4
  • emacs has a nice colour-coded conf mode, too.
    – msanford
    Jan 27, 2012 at 15:02
  • 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 East
    Sep 22, 2012 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! Sep 23, 2012 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! :-)
    – cregox
    May 2, 2013 at 11:28
6

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.

2
sudo chmod +w name_of_the_file
sudo open -a TextEdit name_of_the_file
sudo chmod -w name_of_the_file

Should work

4
  • 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, 2011 at 13:06
  • 1
    Things have changed in Lion, I guess it's the sandboxing Aug 5, 2011 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, 2011 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, 2012 at 15:11
2

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?

2
  • Do you have suggestions on apps that does have that functionality? Preferably free :)
    – Znarkus
    Aug 5, 2011 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, 2011 at 19:18
-5
sudo nano /etc/hosts 

works for Lion

1
  • 2
    The question is about TextEdit, not nano.
    – gosmond
    Feb 21, 2013 at 7:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .