I wanted to make a program (with quickly) that edits some root files.

My question: How can i ask for root permission (after a button is clicked) to execute a command (just like the software center does)?
I want it to be universal. For both gnome and kde


Such a popup would come up if you execute


system("gksudo <command>");


system("kdesudo <command>");
  • gksudo update-alternatives --remove-all java throws me an error. btw, i need something that also works with kde
    – mid_kid
    Oct 21 '12 at 18:34
  • 2
    This is valuable information and should be added to your original question.
    – cauon
    Oct 21 '12 at 18:52
  • 1
    here you go @cauon ;) It's up to mid_kid to create a variable for the gksudo/kdesudo difference.
    – Rinzwind
    Oct 22 '12 at 10:47

A lot has changed since I asked this question in 2012. These days, pkexec is the preferred way to run things as root, as many desktop environments will implement their own frontend (polkit agent) for it, meaning you won't have to bother with checking for different environments. Additionally, gksudo and kdesudo have been deprecated.

Since python 3.5, the proper way to do this would be:

import subprocess
subprocess.run(["pkexec", "<command>", "<arg1>", "<arg2>"])

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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