简体   繁体   English

以sudo方式运行python调用

[英]Run python calls as sudo

We have a python setup script which was run as root, currently we want to run it from as sudoer user, with a set of privileges. 我们有一个以root身份运行的python安装脚本,当前我们希望以sudoer用户身份(具有一组特权)运行它。 The problem is that our code is full of os.makedirs and open(filename) etc. 问题是我们的代码充满了os.makedirs和open(filename)等。

open(filename) as sudo ?
os.makedirs as sudo ?
... as sudo ?

Solutions which are not suitable for us: 不适合我们的解决方案:

  1. Run the whole setup script as "sudo python script.py" This is a security problem for us. 以“ sudo python script.py”运行整个安装脚本。这对我们来说是一个安全问题。 We need to avoid it. 我们需要避免它。

  2. Popen(["sudo","..."]) This is bad for non-unix systems support, and requires all the code to be rewritten. Popen([“ sudo”,“ ...”])这对非unix系统支持不利,并且需要重写所有代码。

I have beef with requirement #2. 我有2号牛肉。 How is it that you need sudo but you also need to be platform agnostic? 您需要sudo但又需要与平台无关的感觉如何? If that's really what you want, you must re-think requirement #1, OR detect which OS you're on and prefix your commands with sudo if you are on *nix. 如果这确实是您想要的,则必须重新考虑需求1,或者检测到您正在使用的操作系统,并且如果在* nix上,则使用sudo前缀命令。 Also, aside from running the whole script using sudo, you're going to have to change code, so that part of #2 is a mute point. 另外,除了使用sudo运行整个脚本之外,您还必须更改代码,因此#2的一部分是一个静默点。 Using sudo in Popen commands, however, is kind of pointless. 但是,在Popen命令中使用sudo毫无意义。 You will have to enable passwordless sudo for those specific commands which you must run, and IMO that's more of a risk than just running the whole script with UID 0. However, it seems you're only interacting with the filesystem, which as someone commented does raise the question of could you just give your user permission to access the files it needs? 您将必须为必须运行的那些特定命令启用无密码sudo,而IMO比仅以UID 0运行整个脚本要冒更大的风险。但是,似乎您只是在与文件系统进行交互,正如有人评论的那样确实引发了一个问题,您是否可以授予用户访问所需文件的权限?

Tl;dr Give the user running the program permissions to access these files, or step back and ask yourself why you're having this issue and is this really the best solution to your problem Tl; dr授予运行程序的用户访问这些文件的权限,或者退一步并问自己为什么会遇到此问题,这确实是解决问题的最佳方法

In general, when a small piece of your code has to be run with elevated privileges, but it's not safe to run the rest this way, you do something like this: 通常,当您的一小段代码必须以提升的特权运行,但是以这种方式运行其余代码并不安全时,您可以执行以下操作:

You run your main script as root. 您以root身份运行主脚本。 But at startup, it forks or spawns a child process (which is still root), then immediately drops privileges. 但是在启动时,它会分叉或产生一个子进程(该进程仍然是root),然后立即放弃特权。 When you need to do something as root, you use some sufficiently-safe IPC mechanism (a socketpair may or may not be sufficiently-safe, depending on the rest of your design…) to ask the child to do it on your behalf and send back a response. 当您需要以root用户身份进行操作时,可以使用一些足够安全的IPC机制( socketpair可能足够安全,也可能不足够安全,具体取决于设计的其余部分……)要求孩子代您发送并发送回复。 The details are a little different for Windows, but the same basic idea can be made to work. 对于Windows,细节有所不同,但是可以使相同的基本思想起作用。


There are alternatives to this, however, including: 但是,还有其他选择,包括:

  • Create setuid programs, and run them as children from a non-root parent. 创建setuid程序,然后以非根父级的子级身份运行它们。 This one isn't workable on Windows, and may not be appropriate on Unix when you're dealing with child scripts. 这个在Windows上不可用,在处理子脚本时在Unix上可能不合适。
  • Redesign your app into a daemon/service, which is separately managed (typically by your system's daemon/service manager) and runs as root, and the main app, which talks to the daemon over IPC. 将您的应用程序重新设计为一个守护程序/服务(该服务通常由系统的守护程序/服务管理器进行单独管理)并以root身份运行,而主应用程序则通过IPC与该守护程序进行通信。
  • Store credentials that can be used to impersonate root as needed (ideally just on a short-lived child process or thread). 存储可用于根据需要模拟root的凭据(理想情况下仅用于短暂的子进程或线程)。 This one works great on Windows, but doesn't work so well on Unix. 这在Windows上效果很好,但在Unix上效果不佳。

However, in this case, there's a much simpler solution: The only thing you seem to need root privileges for is to read (and maybe write?) files that you installed as owned by a different user. 但是,在这种情况下,有一个更简单的解决方案:您似乎需要root特权的唯一一件事就是读取(并可能写入?)由其他用户拥有的已安装文件。 Just make those files group-readable (and maybe -writable), and run the script as a limited-privilege user who's a member of that group. 只需使这些文件对组可读(并且可能是可写的),然后以该组成员的受限特权用户身份运行脚本即可。

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

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