简体   繁体   English

Python sudo权限

[英]Python sudo privileges

I have a script ( A.py ) that will spawn new processes ( B.py ) dynamically but those scripts need to be created as root . 我有一个脚本( A.py ),它将动态生成新进程( B.py ),但这些脚本需要以root身份创建。 If I run 如果我跑步

$ python A.py

as a normal user, then when I run 以普通用户身份运行时

>>> subprocess.Popen('sudo nohup python B.py &') 

I'll need to enter the root password to start. 我需要输入root密码才能启动。 I don't really want to do that. 我真的不想这么做。


Now if I run the first script as root 现在,如果我以root身份运行第一个脚本

$ sudo python A.py

then I'll be able to run 那我就可以跑了

>>> subprocess.Popen('nohup python B.py &')

like normal. 像平常一样。 The thing that concerns me is a timeout period with the sudo and it will drop to normal privileges then when A.py want to spawn/kill a process it will ask for a password and stop working as intended. 我关心的是sudo的超时时间,它将降为普通特权,然后当A.py想要生成/杀死进程时,它将要求输入密码并按预期方式停止工作。

  1. Will running sudo python A.py keep root privileges for the life of the script, or will it lose it with the sudo timeout (I believe default is 15min) like a normal terminal? 运行sudo python A.py会在脚本的生命sudo python A.py保留root特权,还是会像普通终端一样在sudo timeout (我相信默认值为15分钟)后丢失它?
  2. Is there a better way of doing this? 有更好的方法吗?

Will running sudo python A.py keep root privileges for the life of the script, or will it lose it with the sudo timeout (I believe default is 15min) like a normal terminal? 运行sudo python A.py会在脚本的生命周期内保留root特权,还是会像普通终端一样在sudo timeout (我相信默认值为15分钟)后丢失它?

It will keep root privileges for the lifetime of the script. 它将在脚本的生存期内保留root特权。

Running commands don't have their root privileges revoked; 正在运行的命令不会撤消其root特权。 that's not what the timeout does. 超时不是那样的。 The timeout simply controls how often a user has to enter their password when they do a sudo command. 超时只是控制用户执行sudo命令时必须输入密码的频率。 Since your now root-privileged script would simply execute nohup python B.py directly rather than sudo nohup python B.py the sudo timeout would not come into play. 由于您现在具有root特权的脚本将直接直接执行nohup python B.py而不是sudo nohup python B.py因此sudo超时不会起作用。

You could have an additional script which would run python B.py , this script being owned by root, with the setuid flag set. 您可能还有一个其他脚本,它将运行python B.py ,该脚本由root拥有,并设置了setuid标志。 (Alternatively, give B.py an appropriate shebang and make it executable directly.) Then running it at any time would run it as root. (或者,给B.py一个适当的shebang并使其直接可执行。)然后在任何时候运行它将以root身份运行。 (You naturally then need to make sure it's protected so that it can only be run appropriately and can't execute arbitrary code, providing privilege escalation.) (自然,您自然需要确保它受到保护,以使其只能适当运行,并且不能执行任意代码,从而提供特权升级。)

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

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