如何为 Linux 用户设置 CAP_SYS_NICE 能力?
我的程序使用 Linux 系统调用 setpriority()
来更改它创建的线程的优先级。它需要设置负优先级(-10),但是,正如文档中提到的,以普通用户身份运行时会失败。
用户需要 CAP_SYS_NICE 能力才能根据自己的需要设置优先级,但我不知道如何向用户提供这种能力。
所以我的问题是:如何为 Linux 用户设置 CAP_SYS_NICE 功能?
My program is using the Linux system call setpriority()
to change the priorities of the threads it creates. It needs to set negative priorities (-10) but, as mentioned on the documentation, this fails when run as a normal user.
The user needs the CAP_SYS_NICE
capability to be able to set the priorities as he wants, but I have no idea how to give such capability to the user.
So my question: how to set CAP_SYS_NICE
capability to a Linux user?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
有一个非常方便的实用程序用于设置二进制文件的功能:setcap。这需要在应用程序二进制文件上以 root 身份运行,但一旦设置,就可以以普通用户身份运行。示例:
您可以使用 getcap 确认应用程序具有哪些功能:
我建议将这些功能集成到安装行中的 makefile 中,无论如何,该安装行通常以 root 身份运行。请注意,功能不能存储在 TAR 文件或任何衍生包格式中。如果您稍后打包应用程序,则需要一个脚本(Debian 软件包的 postinst)来在部署时应用该功能。
There is a nice handy utility for setting capabilities on a binary: setcap. This needs to be run as root on your application binary, but once set, can be run as a normal user. Example:
You can confirm what capabilities are on an application using getcap:
I'd suggest integrating the capabilities into your makefile in the install line, which is typically run as root anyhow. Note that capabilities cannot be stored in a TAR file or any derivative package formats. If you do package your application later on, you will need a script (postinst for Debian packages) to apply the capability on deploy.
Jan Hudec 是对的,进程不能只给自己一种能力,而 setuid 包装器是获得该能力的明显方法。另外,请记住,当您删除 root 权限时,您需要
prctl(PR_SET_KEEPCAPS, ...)
。 (有关详细信息,请参阅prctl
手册页。)否则,当您转换为非 root 真实用户 ID 时,您将放弃该功能。如果您确实只想启动具有不同允许的良好级别的用户会话,您可能会看到
pam_limits
和limits.conf
手册页,如pam_limits
code> 模块允许您更改硬性限制。它可能是这样的一行:Jan Hudec is right that a process can't just give itself a capability, and a setuid wrapper is the obvious way get the capability. Also, keep in mind that you'll need to
prctl(PR_SET_KEEPCAPS, ...)
when you drop root. (See theprctl
man page for details.) Otherwise, you'll drop the capability when you transition to your non-root real user id.If you really just want to launch user sessions with a different allowed nice level, you might see the
pam_limits
andlimits.conf
man pages, as thepam_limits
module allows you to change the hard nice limit. It could be a line like:AFAIK 不可能获得能力。根进程拥有所有能力,可以放弃它们,但一旦放弃,就无法重新获得。因此,您需要一个 suid-root 包装器,它将放弃所有其他功能并运行该进程。
AFAIK It's not possible to get a capability. Root processes have all capabilities and can give them up, but once given up, they can't be regained. So you'll need a suid-root wrapper that will give up all other capabilities and run the process.
关于 sudo,我像这样添加了用户:
然后它工作正常:
Regarding sudo, I added the user like this:
And then it worked fine:
如果您通过 sudo 获得 root 访问权限,
您可以使用
setpriv
将任何您想要的功能添加到命令中。不过,使用它确实很痛苦,所以我为它编写了一个脚本。
将其保存为
/usr/local/bin/chyort
并chmod +x
它。要使用该脚本,请在需要实时权限的命令前加上
chyort
前缀:(该名称的灵感来自
chrt
和 чёрт。)If you have root access through sudo,
you can use
setpriv
to add any capability you want to a command.Using it is a real pain in the ass, though, so I wrote a script for it.
Save this as
/usr/local/bin/chyort
andchmod +x
it.To use the script, prefix the command that requires realtime permissions with
chyort
:(The name was inspired by
chrt
and чёрт.)