Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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

share|improve this question
    
You will probably have to use gksu/kdesu anyway. –  Anton Barkovsky Dec 12 '11 at 21:08
add comment

1 Answer

up vote 2 down vote accepted

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:

http://linuxproblem.org/art_9.html

Method 4:

Use gksudo (graphical sudo):

gksudo "gnome-open %u"

Or, on KDE kdesu:

kdesu <command>
share|improve this answer
    
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
show 1 more comment

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.