Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

There are various tools for various desktops that aim to run a graphical application as root. gksudo, kdesudo, empower, etc...

But what if I'm writing a script that could be used on any Linux desktop? Is there an xdg-sudo of some sort..? A standard for getting sudo access in a desktop environment?

share|improve this question
2  
Why not pkexec? –  Tachyons Feb 9 '13 at 2:22
2  
@Tachyons You should write that as an answer and explain what it is, how it works, etc :) –  Oli Feb 9 '13 at 10:19
    
@Oli: Done, check my answer :) –  MestreLion May 2 '13 at 22:44

3 Answers 3

up vote 7 down vote accepted

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.
share|improve this answer

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.

share|improve this answer

No, there isn't any standard way of getting root (or any other user's) privileges.

If you want to be compatible with as much systems as possible don't assume any way but tell the user that she/he has to run your script as a specific user. Then it's up to the user/admin to configure the system so your script is run in a proper way.

Making some suggestion for how to use the most common authentication systems, like su, sudo and ConsoleKit, would be nice.

share|improve this answer
    
True for early 2009, but thankfully now polkit (formely PolicyKit) is widespread enough to be considered a standard. –  MestreLion May 2 '13 at 22:48

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.