1

I am running some shell commands with os.system that needs to be run as root.

I tried-

os.system("sudo su")
os.system("other commands")

and also-

home_dir = os.system("sudo su")
os.system("other commands")

But both the above scripts just become root and then stop executing, so the rest of my commands aren't executed.

I'm running Python 3.6.9 on an Ubuntu 18.04 VM.

woohu1
  • 23
  • 2
  • 4
  • 1
    How do you expect to become *root* by running two sequenced `.system(..` where every is running in it's own subprocess. ***stop executing***: The `subprocess` is waiting for input the password, but the dialog is not showing. – stovfl May 30 '20 at 08:26
  • @stovfl So what do you suggest I do? – woohu1 May 30 '20 at 08:30
  • Actually, when you run `sudo` from terminal, it requires to enter password, and similar happen there. After `os.system('sudo')` it requires password but you just can't see it. – just-Luka May 30 '20 at 08:31
  • If you need sudo for some purpose, you can easily executive py file as root – just-Luka May 30 '20 at 08:35
  • @cike4ka Yes, but this program will be executed automatically(be called by a PHP script) – woohu1 May 30 '20 at 08:36
  • @woohu1 Consider to delete this question, you need something like [unix-permissions-read-vs-execute-php-context](https://stackoverflow.com/questions/2010623) – stovfl May 30 '20 at 08:50

1 Answers1

1

The root privileges gained by sudo only apply to the command that is run through sudo, and do not raise the privileges of the caller (in this case, your python script). So your first command os.system("sudo su") would run an interactive root shell, but after you have exited from that and then your python code does the subsequent call to os.system("other commands"), these will run under its ordinary user privileges.

You could run each command one at a time via sudo:

os.system("sudo some_command")
os.system("sudo some_other_command")

Note that each command will be separately logged by sudo in the system log, and that even if there are several commands, sudo shouldn't ask for a password more than once within a short time interval.

Or if you need to do a sequence of steps like changing directories that might not be possible in the caller (for example, if the directory is not accessible by the non-root user that is running the python script), then you could do for example:

os.system("sudo sh -c 'cd some_dir && some_other_command'")

(Just for info, && is similar to ; but the other command is only run if the cd succeeded, so it is safer, although this point relates to shell syntax rather than python.)

If there are a lot of commands, of course you also have the option of just making a separate "helper" shell-script and running the entire script through sudo.

os.system("sudo sh /path/to/myscript.sh")

Finally to note, if you are running your python script in a non-interactive environment, you may need to tell sudo not to prompt for a password, at least for the relevant invoking user and target commands. For details, do man sudoers and look for examples involving NOPASSWD.

alani
  • 12,573
  • 2
  • 13
  • 23