Linux 以与 Python 不同的用户身份运行进程
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13825689/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
running a process as a different user from Python
提问by EEP
I am running a script as userA
with root access, from this script I want to make a popen()
call and run a different process as userB
.
我正在运行一个userA
具有 root 访问权限的脚本,从这个脚本我想popen()
打电话并运行一个不同的进程作为userB
.
os.setuid() does not seem to work for this (unless I am doing this wrong?), and I would like to avoid a linux based solution such as su -userB -c <command>
os.setuid() 似乎对此不起作用(除非我做错了?),我想避免使用基于 linux 的解决方案,例如 su -userB -c <command>
Is there a pythonic way of running a process as userB
while the script is running as userA
?
userB
当脚本运行时,是否有一种 pythonic 方式来运行进程userA
?
采纳答案by Andrew Clark
The following answer has a really nice approach for this: https://stackoverflow.com/a/6037494/505154
以下答案对此有一个非常好的方法:https: //stackoverflow.com/a/6037494/505154
There is a working code example there, but the summary is to use subprocess.Popen()
with a preexec_fn
to set up the environment of the subprocess so that it executes as another user.
那里有一个工作代码示例,但总结是subprocess.Popen()
与 apreexec_fn
一起使用来设置子进程的环境,以便它作为另一个用户执行。