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

16,201

Solution 1

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.

Solution 2

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.

Solution 3

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"

Solution 4

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.

Solution 5

gksudo is the GTK+ version of sudo.

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

Share:
16,201

Related videos on Youtube

Johannes
Author by

Johannes

Updated on April 17, 2022

Comments

  • Johannes
    Johannes about 2 years

    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?

    • Johannes
      Johannes over 14 years
      when I open mamp, I'm asked to give my password. Or when I install a pkg.. what is that pop-up box called in os x?
  • Johannes
    Johannes over 14 years
    it's supposed to be graphical ... =)
  • gregschlom
    gregschlom over 12 years
    Awesome. The only drawback is that the dialog reads "osascript wants to make changes", which may confuse the user since she may not know what osascript is.
  • antonio081014
    antonio081014 over 11 years
    This is exactly what I am looking for, just spent hours on this problem... Thank you man.
  • Adam Batkin
    Adam Batkin about 11 years
    What clown downvoted this? When I answered the question, the word "graphical" appeared nowhere in the question. And it's still a valid answer, even if it's not exactly what the original poster asked
  • Sabby
    Sabby over 10 years
    Any way to remember the entered password between calls? I need to make several calls to such a script in succession, and don't want require the user to enter their password multiple times.
  • h__
    h__ over 9 years
    Somehow this fails to work for me: I tried ./cocoasudo systemsetup and got the graphical prompt, however after entering my password, systemsetup still think I'm not sudoing as it complains: You need administrator access to run this tool... exiting!
  • Arash
    Arash over 9 years
    Thanks for your solution, is it possible to replace the "osascript" from the message "osascript wants to make changes"?
  • Arash
    Arash over 9 years
    Also is there a way to create like a session for that root credentials so it won't ask for username and password each time?
  • ademar111190
    ademar111190 over 9 years
    sorry @Arash but I do not know the answer of your two questions =/
  • cregox
    cregox over 8 years
    This isn't working anymore. Check my answer for updates or more options. Feel free to update yours, @ZJR! ;P
  • 64bit_twitchyliquid
    64bit_twitchyliquid almost 7 years
    The question clearly asks for a graphical version of sudo (word number 4).
  • Adam Batkin
    Adam Batkin almost 7 years
    @64bit_twitchyliquid But it didn't when I answered the question. Plus my answer isn't wrong, it's just not fully answering the question (i.e. it might still help someone else)