Question

I'd like to place a special file in the /usr/bin folder of Ubuntu. Basically I'm trying to write a setup file in python which would do the job.

But administrative privileges are needed to fulfill the job, how to provide my setup with these privileges (provided that I have the password and can use it in my program)?

Was it helpful?

Solution

You can't just escalate the privileges of alredy running script (well, unless you use some local exploit).

The best way to go is to follow @Lattyware's advice and just force user to run the script with root priveleges (via sudo or by any other means). This is pretty common practice for installers.

However, if you REALLY need to escalate privileges midway (I can't imagine why), you can do something like:

import os
cpstr = 'echo %(pass)s | sudo -S cp "%(from)s" "%(to)s"'
os.system(cpstr % {'pass':'userpassword', 'from':'./build/bin/myapp', 'to':'/bin/myapp'})

OTHER TIPS

You need to run the program with escalated privileges. Under Ubuntu, this is normally done with the sudo command, which will prompt the user for their password.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top