0

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)?

2 Answers2

1

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.

Gareth Latty
  • 86,389
  • 17
  • 178
  • 183
0

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'})
aland
  • 4,829
  • 2
  • 24
  • 42
  • 1
    To be fair, there are sometimes good reasons to escalate privileges mid-execution. For example, `yaourt`, a package manager for Arch Linux allows the user to build packages from the AUR - however, making a package as root is dangerous (as the scripts could in theory do anything), so it's best to make the package with lower permissions, then get root privileges to install the package. – Gareth Latty Apr 30 '12 at 16:04
  • @Lattyware I see your point, however I think it is usually better to _lower_ privileges when necessary, rather then escalate them: it's easier (therefore, usually safer) to implement (`setuid`/`seteuid`) and the user is aware that what he is going to execute will affect the entire system. – aland Apr 30 '12 at 16:20
  • You make a good point, just saying what I've seen in the wild. – Gareth Latty Apr 30 '12 at 16:21
  • You can also use $HOME/.local/bin in the user's space as this is in the default path for bash at least. – Peter Moore Mar 16 '21 at 13:20