Linux acquire root permissions through a password popup

1.1k views Asked by At

I am writing a python/Qt application which needs to perform disk mounting. Hence it needs root permissions. Is there any way that I could make the script acquire root permissions using a popup for password, like gparted does? I know I can detect within the script, if it has been run with sudo or not, with something like this (for example in bash):

if [[ $(id -u) -ne 0 ]];

But after this, how do I ask for the password and acquire root permissions at runtime?

Thanks.

1

There are 1 answers

5
Alex Huszagh On BEST ANSWER

Like this:

>>> import os
>>> os.system("gksudo software-center")

Popup sudo that then executes the command of interest (here, Ubuntu software center).

You can edit this with any subprocess or other way of calling a shell command.

EDIT: Anonymous downvoters: gksudo is the perfect response here? You want someone who needs root access who may not have run the script with root access to begin with. gksudo is just an interface to get root permissions to do a command, particularly for GUIs, exactly what is requested.

Leave a comment please.