5

Part of my python program needs administrator access. How can I gain root privileges using a GUI popup similar to the gksudo command?

I only need root privileges for a small part of my program so it would be preferable to only have the privileges for a particular function.

I'm hoping to be able to do something like:

gksudo(my_func, 'description of why password is needed')
david4dev
  • 4,656
  • 2
  • 23
  • 33

2 Answers2

4

gksudo can be used to launch programs running with administrator privileges. The part of your application that needs to run as root, must be able to be invoked as a separate process from the command line. If you need some form of communication between the two, you could use sockets or watch for a file, etc.

knutin
  • 4,733
  • 15
  • 24
  • 1
    But in case you use sockets, make sure that an attacker couldn't connect to the gksudoed program. IMHO, it would be better to use DBUS or so (session-specific). – thejh Nov 06 '10 at 10:45
  • should you go with this option, you should use the `subprocess` module instead of using `os.system`. http://docs.python.org/library/subprocess.html –  Nov 07 '10 at 15:09
3

You have two options here:

You will need to make the part of the program that requires root privileges a separate file and then execute the file like this:

>>> import subprocess
>>> subprocess.call(['gksudo','python that_file.py'])

which will bring up the password prompt and run that_file.py as root

You could also require that a program be run as root from the start and just have the program user type "gksudo python your_program.py" in the command-line from the start which is obviously not the best idea if your program is normally launched from a menu.

AJ00200
  • 12,717
  • 7
  • 20
  • 20