Is there any graphical "sudo" for Mac OS X?

18.1k Views Asked by At

I'm designing a little software in Java. I don't know the term/definition to what I'm doing, but I'm prompting commands from Java to the terminal. Something like this:

Process process = Runtime.getRuntime().exec("command");

I've done this before in Linux, and I used gksudo for commands that required the root password.

Is there any gksudo in OS X? Any graphical popup asking for root password?

9

There are 9 best solutions below

0
On

Following ZJR's answer, I've made this into automator, so you can use it as a Service or whatever:

on run {input, parameters}
    do shell script "sudo open \"" & (POSIX path of input as string) & "\"" with administrator privileges

    return input
end run

Or, maybe you just think his answer is outdated and still want an AppleScript, just write this single line in Script Editor:

do shell script "[[your command]]" with administrator privileges

Which you can then make into an app and use it as a Service or whatever.

0
On

There seems to be a lot of wrong information in these answers. To save other people some time, I post my own findings:

First of all, like the poster of the question, I also have the situation that I need to elevate permissions from within a Java application.

I have split this up into two scripts. The first script is executed from Java with some command line parameter. The second script performs all steps that need root privileges. The idea is of course to use cocoasudo in the first script to perform the second script with root privileges.

I have confirmed via extensive logging into separate files that the scripts indeed do what I intended. And they work fine when launched manually (with normal user privileges of course) from the command line.

When launched from the Java app, I do get the cocoasudo prompt, but nothing happens. Not even the first logging output from the second script appears.

When I change the first script to use osascript, again with confirmation that everything is correct as far as the script goes, I don't even get a prompt when it runs from within Java.

This is all on OS X Mountain Lion. As if Apple build in some safe guards that prevent scripts being executed with root privileges from within Java.

Since cocoasudo itself actually runs, I am inclined to think the solution is to code something similar to cocoasudo, but performing all the rest of the required actions using Cocoa API calls. This can then be code-signed as well.

0
On

One should use the native OS X authorization services instead of looking at sudo and/or a graphical interface to it.

Ref:

[I know it's a late answer ...]

4
On

If you are using a terminal, then just use "sudo" instead, which will prompt for the user's password in the terminal itself (as opposed to gksudo which I believe uses a graphical popup). Sudo works on both Linux and OS X.

0
On

gksudo is the GTK+ version of sudo.

You can use this clone for it especially for OS X.

1
On

I found the cocoasudo doesn't work if you are running a shell script that calls other commands. You would have to use cocoasudo in all sub-commands also which would pop up a prompt for each call.

The osascript solution seems to work better, but I needed to tweak it to work with commands involving paths containing spaces.

#!/bin/sh
export bar=""
for i in "$@"; do export bar="$bar '${i}'";done
osascript -e "do shell script \"$bar\" with administrator privileges"
4
On

make the follows, in this example I go create a folder /var/lock and set your permissions to 777:

String[] command = {
        "osascript",
        "-e",
        "do shell script \"mkdir -p /var/lock && chmod 777 /var/lock\" with administrator privileges" };
Runtime runtime = Runtime.getRuntime();
try {
    Process process = runtime.exec(command);
    BufferedReader bufferedReader = new BufferedReader(
                new InputStreamReader(process.getInputStream()));
    String line;
    while ((line = bufferedReader.readLine()) != null)
            System.out.println(line);
} catch (IOException e) {
    e.printStackTrace();
}

on linux maybe you can make this with gksudo but I not test it, after I go testing and post here the results.

1
On

This also looks promising: cocoasudo

enter image description here

It uses the OSX native Authorization Services API:

For Mac OS X Cocoa-based apps, there is analagous ability to sudo provided via the Authorization Services API. Use of the API allows you to prompt the user for their username and password requesting the ability to escalate privileges.

For that case, I’ve written a small utility that I’ve dubbed cocoasudo. Use cocoasudo in much the same way you’d use sudo. However, instead of users being prompted for their password in a Terminal window, they’ll get a dialog prompt via the Authorization Services API.

2
On

You can more ore less manage to write your own with an AppleScript shell script:

#!/bin/sh
osascript -e "do shell script \"$*\" with administrator privileges"

cocoasudo looks aesthetically more pleasing, but this is already deployed.