我有一个Java应用程序,它具有一个计时器,该计时器执行一个需要提升权限的单独应用程序。我一直在这样做:

String command = "gksudo /home/bob/sensor";
Process child = Runtime.getRuntime().exec(command);


问题是,每当计时器启动时,它都会要求输入密码,所以我每两秒钟得到一次。有没有办法只请求一次密码然后保持提升权限,以便只要Java应用程序正在运行,它就不会再询问?

我尝试使用gconf-editor来更改apps / gksu / save-to-keyring选项,但这并没有改变任何东西,而且我认为它仍然可能是一个核选项。

最佳答案

我认为这实际上是不可能的,因为gksudo不允许您传递密码。但是susudo确实允许这样做(请参见How to pass the password to su/sudo/ssh without overriding the TTY?)。

您可以使用gksudo -p使用图形化身份验证窗口并检索密码。然后,下次您不再需要gksudo,因为无论如何您都不希望输入密码。

可能看起来像这样

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);
}


我怀疑安全专家讨厌我建议这样做(存储密码),因此请谨慎使用。

10-06 14:44