以Python的不同用户身份运行进程[重复]

时间:2023-02-15 16:22:14

This question already has an answer here:

这个问题在这里已有答案:

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

Is there a pythonic way of running a process as userB while the script is running as userA?

当脚本作为userA运行时,是否存在以userB运行进程的pythonic方式?

1 个解决方案

#1


8  

The following answer has a really nice approach for this: https://*.com/a/6037494/505154

以下答案有一个非常好的方法:https://*.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.

这里有一个工作代码示例,但摘要是使用带有preexec_fn的subprocess.Popen()来设置子进程的环境,以便它作为另一个用户执行。

#1


8  

The following answer has a really nice approach for this: https://*.com/a/6037494/505154

以下答案有一个非常好的方法:https://*.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.

这里有一个工作代码示例,但摘要是使用带有preexec_fn的subprocess.Popen()来设置子进程的环境,以便它作为另一个用户执行。