Is there a desktop-neutral gksudo?

Solution 1:

Following @Tachyons suggestion, I researched a LOT about pkexec and it is awesome!

  • It is part of PolicyKit, a widely used package.
  • Hosted and sponsored by freedesktop.org
  • Default in Gnome, Unity, MATE, Cinnamon, KDE, LXDE and XFCE, and possibly others
  • Works via D-Bus, so it's completely desktop-neutral, with a well-defined protocol.
  • In many DEs it is used in several services like login screen, shutdown/restart, disk management (udisks/automount) and so on.

So yes, it meets all requirements, and I guess it's the closest thing an "universal gksudo" can be.

That said, pkexecis the command-line front-end to PolicyKit. There are 2 basic modes of operation:

  • Simply run it as you would with sudo, and you get a nice GUI prompt:

    $ pkexec rm -rf /

pkexec GUI prompt

  • Or create a PolicyKit authorization file for you application, drop it in /usr/share/polkit-1/rules.d, and it will allow a great deal of customization and security-related options, for example root access for certain actions only, no need of password for others, access limited to certain paths, etc. You can fine-tune your settings with amazing granularity.

Solution 2:

A more general way would be to write a "helper program". Such a program would prompt the user for a password; if you are sure that, say, Zenity, is installed, you can use that, otherwise you'll need to write the helper program in Python or something else that can interact with the GUI.

Read the sudo manual for the -A ("askpass") and -S (stdin) options.

And remember that when you run sudo from your script, you need the -H (home) option to simulate gksudo.

This method works well, but you need to test the helper program thoroughly to ensure that it is bug-free.