How to Run GUI Apps as root in Mac OS X
Those familiar with the command line know that running things with super user privileges is typically just a matter of using the sudo command. That still holds true with launching GUI apps into the OS X with root privileges, but it’s not just a matter of prepending sudo to the otherwise useful open command, because ‘open’ launches apps as the original user, with or without sudo. The solution instead is to use sudo pointing directly at the executable contained within a given applications package file.
Launching OS X GUI Apps as root user
The command syntax is as follows:
sudo /Path/To/Application/ApplicationName.app/Path/To/Executable
In most cases, that will be applications stored in the /Applications/ directory, and the executable is almost always stored in Package/Contents/MacOS/ as whatever the applications name is:
sudo /Applications/ApplicationName.app/Contents/MacOS/ApplicationName
For example, this command runs the familiar TextEdit app as root:
sudo /Applications/TextEdit.app/Contents/MacOS/TextEdit
To launch TextEdit as a background app, meaning it wont close if you close the terminal window, apply the -b flag to sudo:
sudo -b /Applications/TextEdit.app/Contents/MacOS/TextEdit
You can confirm the the application is running as root by using the ps command with grep, again using TextEdit as an example:
ps au|grep TextEdit
Alternatively, you can look at the OS X process management app Activity Monitor and find the application running there as ‘root’ user, as demonstrated in the screenshot up top and the short video below:
If you intend on running a particular app frequently as root, you might consider placing an alias in .bash_profile to shorten the command string.
Despite running as root, not all system files may be modifiable and some may be marked as “Locked” when opened in some apps like TextEdit. That issue can often be resolved by enabling the root user if you have’t done so yet, but not all apps will have that limitation. Nonetheless, for certain tasks like editing the hosts file you’re still better off sticking to the command line and a text based editor, or using an app like BBEdit or TextWrangler.
how can I use spiffy osx tools to have the windows equivalent mouse context menu run as admin?
sudu command is not for root, sudo is for admin.
su is for root.
Big difference,
Open as admin:
sudo open /Applications/TextEdit.app
Open as root
su open /Applications/TextEdit.app
There is no admin user. sudo runs commands as root. Read the man page. Try this:
sudo touch testfile
You get:
-rw-r–r– 1 root staff 0 Feb 7 11:30 testfile
root created and owns the file.
su only works with root user enabled, sudo runs the command as root without root user account enabled. Fairly important difference, though su does work.
Simpler: sudo open /Applications/TextEdit.app
`open` won’t run the app as root. It’s mentioned in the first paragraph.
So why might one need to do this?
Modifying hosts files, cron jobs, etc for the vi averse I suppose
As always, giant caveat on using root for stuff. Be sure you know what you’re doing. Back up files before making major modifications.
Absolutely. But one would hope they do know what they’re doing if they’re looking to do such a thing.