Is there any graphical "sudo" for Mac OS X?
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
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.
Related videos on Youtube
![Johannes](https://i.stack.imgur.com/3Gx4I.jpg?s=256&g=1)
Johannes
Updated on April 17, 2022Comments
-
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 over 14 yearswhen 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 over 14 yearsit's supposed to be graphical ... =)
-
gregschlom over 12 yearsAwesome. 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 over 11 yearsThis is exactly what I am looking for, just spent hours on this problem... Thank you man.
-
Adam Batkin about 11 yearsWhat 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 over 10 yearsAny 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__ over 9 yearsSomehow 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 notsudo
ing as it complains:You need administrator access to run this tool... exiting!
-
Arash over 9 yearsThanks for your solution, is it possible to replace the "osascript" from the message "osascript wants to make changes"?
-
Arash over 9 yearsAlso 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 over 9 yearssorry @Arash but I do not know the answer of your two questions =/
-
cregox over 8 yearsThis isn't working anymore. Check my answer for updates or more options. Feel free to update yours, @ZJR! ;P
-
64bit_twitchyliquid almost 7 yearsThe question clearly asks for a graphical version of sudo (word number 4).
-
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)