So it has been well documented that GUI applications (like gedit or textedit) should NOT be run with sudo. Ubuntu et al get gksu and gksudo (and the like) so question: what do WE (Mac users) get? Given that the Darwin kernel is built on some *BSD code, I assume the same issues apply, but how do we go around this?

  • 1
    This question seems to rather broad right now, can you be a bit more specific about which real-world problem you are trying to solve here? – nohillside Jun 16 '14 at 12:36
  • @patrix, I'm asking only because this thought came into my head. A year ago I had to edit a couple hosts files, so I su-ed into root and ran TextEdit from there. I was wondering if there is a better way. – agentroadkill Jun 16 '14 at 16:30

To edit /etc/hosts with Sublime Text:
sudo /Applications/Sublime\ Text.app/Contents/MacOS/Sublime\ Text /etc/hosts

If you have to do this on the regular basis, you can add this snippet to your ~/.bash_profile

#   sudoapp: Runs .app with root privileges
#   Usage: sudoapp /Applications/Name.app /etc/hosts
#   --------------------------------------------------------------------
    sudoapp () {
        sudo "$1/Contents/MacOS/$(defaults read "$1/Contents/Info.plist" CFBundleExecutable)" $2

Apps running with root privileges will use /private/var/root as home folder, thus all config and temporary files owned by root that will be created in the process will stay where they should be - in the root home directory.
This is the same as logging in as root and running the app, but without the hassle of user switching.

This method works on 10.6 — 10.11

Update: Apple's own TextEdit refuses to start if run as root in 10.11 and newer, so I changed my example to use Sublime Text instead

| improve this answer | |
  • So this will preserve my current config files, and create other ones owned by root? That seems to be the biggest danger in running GUI applications with sudo. – agentroadkill Jun 16 '14 at 16:38
  • Nope, this is correct behavior, because having random root-created files in your ~/Library would be real PITA. – Sergei Jun 16 '14 at 18:40
  • But won't those files then be owned by root when I quit the root-owned Finder instance? I don't really want to go chowning lots of config files every time I do this. – agentroadkill Jun 16 '14 at 20:13
  • Don't need to chown anything, GUI app respects user directory. All config/tmp files created by root GUI app will stay in /private/var/root and will be owned by root - just as it should be. This is the same as logging in as root and running the app, but without the hassle of user switching. – Sergei Jun 16 '14 at 22:56
  • 1
    Somehow I misread your first comment and thought you were saying that preserving your current config files is a problem. Sorry about that. – Sergei Jun 17 '14 at 7:58

While is it possible to launch a graphical application as the root user, it is not recommended. It may work, most of the time, but avoid relying on this behaviour.

Avoid root

Running an application as root is not recommended because it dramatically increases the risk of causing problems with your Mac. The use of root should be limited to the smallest possible piece of code with strict controls in-place.

Applications are increasingly moving towards a fragmented design to avoid exposing too much power to code that does not require it.

  • A mistake in code running with root permissions is a security risk.
  • A mistake in code without root permissions is much less capable of causing serious problems.

There are edge cases but these are increasingly rare. The introduction of sandboxing and XPC are part of Apple's efforts to reduce the need to provide excessive authority to processes running on OS X.

Command Line Tools

If you need to work with files as root user, use command line tools such as vim, emacs, or nano. These tools do not rely on the WindowServer and can happily be launched as root within another user session:

sudo nano <path to edit>

Graphical Tools

If you prefer graphical editors, use an editor that works with the design of Mac OS X. BBEdit is an excellent editor that will correctly handle editing root owned files.

When you edit a root owned file with BBEdit, a second process is used to bridge the permissions gap between you and the owner of the file. This process passes through Apple's sanctioned paths and thus ensures a predictable experience - hopefully across multiple major versions of Mac OS X.

Why? WindowServer Limits and Design Scope

There are subtle technical problems with launching a graphical application within another user session.

The underlying technical problems stem from one user wanting to launch a graphical process within another user's session. Mac OS X's WindowServer was never designed with this as a goal. User sessions are extremely difficult to break out of even as root user – all for desirable security reasons.

Apple has dramatically improved the WindowServer design in the last few major versions of Mac OS X. It is now possible to have multiple users logged into different graphical sessions on one Mac through Screen Sharing. This seemingly simple improvement relied on a huge amount of behind the scenes effort from Apple's engineers.

However, Apple is unlikely to provide an easy way to cross launch applications as different users from within a single graphical user session. How would this benefit their customers?

If you want to explore this topic further, look for questions involving launchctl and running applications in other active user sessions.

| improve this answer | |
  • JFYI I've been using the method described in my answer since OS X 10.6, and never had a single issue. Was WindowServer designed this way deliberately or by accident is beyond my knowledge, but it handles sudo apps perfectly fine. I guess the whole "not recommended" story is blown out of proportions (unless you can actually provide any proof why it can cause problems) – Sergei Jun 16 '14 at 13:27
  • So the dance here seems to be that the application running with root privileges will create some config files (or whatever else) that may be persistent, but owned by root. This can obviously be very bad, but happens fairly infrequently. Let's pretend for the sake of argument I want to open an instance of Disk Utility from the terminal, but in a graphical session. Should I just open it and wait for it to ask me for permission? – agentroadkill Jun 16 '14 at 16:36
  • In this instance, use the command line tool diskutil in place of the application: dssw.co.uk/reference/diskutil.html On Mac OS X, you should not need to run any graphical application directly as root. If additional rights are required, the graphical application is expected to organise these for you and have you authenticate against the appropriate right: dssw.co.uk/reference/authorization-rights – Graham Miln Jun 16 '14 at 16:42
  • I often run X11 applications as root without problems. – bot47 Jun 16 '14 at 22:23

There are good reasons NOT to edit files as root. Why not just copy them to a temporary file, edit this and copy back.

You could use visudo although this requires some knowledge of vi, but is OK for making simple changes to /etc/fstab or similar.

You could try setting the EDITOR environment variable and running visudo although I have never tried this with a graphic editor.

| improve this answer | |
  • Can't see any connection to the question. – bot47 Jun 16 '14 at 22:22

Sergei's answer didn't work for me on OS X 10.8.5

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

I got a permissions error message


Since sudoing the binary first, then double-clicking the file in Finder worked, I came up with the following less simple command

$ sudo -b /Applications/TextEdit.app/Contents/MacOS/TextEdit && sleep .5 && open -a /Applications/TextEdit.app /etc/hosts

You can make a function of it like Sergei's, if need be.

| improve this answer | |
  • In new Mac OS versions Apple done something to TextEdit specifically to prevent it from being used with sudo. Other apps work. I changed my answer to use Sublime Text as an example instead. – Sergei Aug 10 '17 at 10:38
  • Thanks for the heads-up, but could you be more specific? Maybe check flags on TextEdit's binary to start with? – 1.61803 Aug 10 '17 at 20:20

You must log in to answer this question.

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