I have a java application that has a timer that executes a separate application that requires elevated privileges. I have been doing this:
String command = "gksudo /home/bob/sensor";
Process child = Runtime.getRuntime().exec(command);
The problem is that every time the timer k开发者_开发技巧icks off, it requests the password, so I get it every two seconds. Is there a way to only request the password once and then stay elevated so that as long as the java application is running it won't ask again?
I tried using gconf-editor to change the apps/gksu/save-to-keyring option, but that didn't change anything, and I think it might be a nuclear option anyway.
I don't think this actually possible, because gksudo
does not allow you to pass a password. However su
and sudo
do allow this (see How to pass the password to su/sudo/ssh without overriding the TTY?).
You can use gksudo -p
to use a graphical auth-window and retrieve the password. Then the next time you don't need gksudo, since you don't want to ask for a password anyway.
Might look something like this
String sessionPassword = null;
String command = "/my/elevated/app";
void executeStuff() {
// Get the password in case it does not exist yet
if( sessionPassword == null )
sessionPassword = exec("gksudo -p -m 'Please enter you password once.'");
// Construct the command
String elevatedCommand = "echo '"+ sessionPassword +"' | sudo -S " + command;
// Launch elevated
Process child = Runtime.getRuntime().exec(elevatedCommand);
}
I suspect security experts to hate me for suggesting this (storing a password), so use with care.
精彩评论