简体   繁体   English

如何在 Ubuntu 上的 Python3 中获取 root 然后执行 shell 命令?

[英]How to get to root and then execute shell commands in Python3 on Ubuntu?

I am running some shell commands with os.system that needs to be run as root.我正在使用需要以 root 身份运行的 os.system 运行一些os.system命令。

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.但是上面两个脚本都只是变成root然后停止执行,所以我的命令的rest没有执行。

I'm running Python 3.6.9 on an Ubuntu 18.04 VM.我在 Ubuntu 18.04 VM 上运行Python 3.6.9

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). sudo获得的 root 权限仅适用于通过 sudo 运行的命令,并且不会提升调用者的权限(在本例中为您的 python 脚本)。 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.因此,您的第一个命令os.system("sudo su")将运行交互式根 shell,但是在您退出之后,然后您的 python 代码执行对os.system("other commands")的后续调用,这些将运行在其普通用户权限下。

You could run each command one at a time via sudo:您可以通过 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.请注意,每个命令都会被sudo单独记录在系统日志中,即使有多个命令, sudo也不应在短时间内多次要求输入密码。

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:或者,如果您需要执行一系列步骤,例如更改调用者中可能无法执行的目录(例如,如果运行 python 脚本的非 root 用户无法访问该目录),那么您可以为例子:

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.) (仅供参考, &&类似于;但其他命令仅在cd成功时运行,因此更安全,尽管这一点与 shell 语法有关,而不是 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.如果有很多命令,当然您也可以选择只制作一个单独的“帮助”shell 脚本并通过 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.最后要注意的是,如果您在非交互式环境中运行 python 脚本,您可能需要告诉sudo不要提示输入密码,至少对于相关的调用用户和目标命令是这样。 For details, do man sudoers and look for examples involving NOPASSWD .有关详细信息,请执行man sudoers并查找涉及NOPASSWD的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM