In a GUI-environment, I want to be able to ask the user for the root/sudo password and then execute an app that requires root privileges from within my app.

As an example, say I want to run an editor to edit /etc/fstab, I need to start the editor via exec() or something with root privileges... how do I do this?

Note: gksu, gksudo is not an option - may not be available/installed

  • You will probably have to use gksu/kdesu anyway. – Anton Barkovsky Dec 12 '11 at 21:08

Method 1:

To run the "df -h" command as root:

su -c "df -h"

This will prompt the user for root password.

Method 2:

Alternatively, in /etc/sudoers find this line:

root ALL=(ALL) ALL

and duplicate it for your user johnsmith that you want to give admin privileges:

johnsmith ALL=(ALL) ALL

This way, johnsmith will be able to run any command requiring root rights, by first typing "sudo" in front of the command:

sudo df -h

Method 3:

You can use ssh to execute a command on the same machine:

ssh root@localhost "def -h"

will execute the same command in your server. If you don't want to be prompted for password, follow this tutorial for passwordless ssh:


Method 4:

Use gksudo (graphical sudo):

gksudo "gnome-open %u"

Or, on KDE kdesu:

kdesu <command>
  • Method 1 won't work, he said it's a gui app. – Kevin Dec 12 '11 at 16:54
  • These are all command-line techniques. I need to do it programmatically in a GUI setup. – slashmais Dec 12 '11 at 16:55
  • GUI apps can execute shell commands. I added Method 4, which could be the GUI solution you are looking for. – neuron34 Dec 12 '11 at 16:56
  • Often, ssh is configured to not allow root access. And sudo is frequently shipped with the creation of a group. It is enough to add a user to the group to gain the privilege to use sudo, without tampering with sudoers. – enzotib Dec 12 '11 at 17:06
  • These seem to be the options I have to work with, unless I track through the spaghetti-monster source-code of gksu/gksudo/kdesu to see how they do it. – slashmais Dec 13 '11 at 5:06

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.