我以root用户身份登录到终端。

然后在Python中:
os.setuid(471)能够切换到子根,但是当我尝试使用os.setuid(0)切换回根用户时,出现以下错误:Operation not permitted
请让我知道如何从子根目录切换回根用户。

最佳答案

调用os.fork()并在子进程中切换到非root用户。只需在 child 中退出并等待 child 在 parent 中退出,即可“切换回去”。例如:

pid = os.fork()
if pid == 0:
    # child - do the work and exit
    try:
        os.setuid(471)
        ... do the work here
    finally:
        os._exit(0)

# parent - wait for the child to do its work and keep going as root
os.waitpid(pid, 0)

关于python - 无法使用python切换回root用户,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14230467/

10-16 16:21