如何允许普通用户在没有密码的情况下杀死 visudo 中的某个根应用程序

Ye *_*ang 3 linux kill

我想允许普通用户杀死由 root 用户启动的某个应用程序。

在视觉上:

我添加了这样的一行:

normal_user ALL=(ALL) NOPASSWD: /usr/bin/kill $(ps aux | grep 'target_application' | awk '{print $2}')
Run Code Online (Sandbox Code Playgroud)

但是在保存并以normal_user身份执行以下命令后,我仍然收到输入root密码的提示:

sudo /usr/bin/kill $(ps aux | grep 'target_application' | awk '{print $2}')
Run Code Online (Sandbox Code Playgroud)

那我该怎么办?非常感谢!

jmh*_*jmh 5

sudo 不会将该命令解释为要执行的 shell 脚本。因此你说过这个文字命令可以作为 normal_user 运行:

/usr/bin/kill $(ps aux | grep 'target_application' | awk '{print $2}')
Run Code Online (Sandbox Code Playgroud)

但是,由于 shell 会在$(...)调用 sudo 之前解释其中的内容,因此您正在运行的命令看起来更像这样:

sudo /usr/bin/kill 1234
Run Code Online (Sandbox Code Playgroud)

所以它不允许你使用它。

正如 fedorqui 建议的那样,您应该编写一个杀死用户的脚本,然后授予 normal_user 运行该脚本的权限(但请确保他们没有对该脚本或其目录的写访问权限)。

kill_target_application.sh:

#!/bin/sh
/usr/bin/kill $(ps aux | grep 'target_application' | awk '{print $2}')
Run Code Online (Sandbox Code Playgroud)

使用此命令允许用户执行或读取脚本,但不能修改它:

chown root:root <filename>
chmod 755 <filename>
Run Code Online (Sandbox Code Playgroud)

为所有用户授予 (r)ead 和 e(x)ecute 权限,但只有 root 可以修改它。还要确保用户没有对该目录或其任何父目录的写权限。如果您不熟悉这些实用程序,请在执行此操作之前阅读 chown 和 chmod 手册页。

视觉输入:

normal_user ALL=(ALL) NOPASSWD: /path/to/kill_target_application.sh
Run Code Online (Sandbox Code Playgroud)

你可能应该使用“killall”而不是这个复杂的 ps | grep 选项。或者至少看看 pgrep。

此外,这听起来确实像是 init 脚本的工作。

  • 因此,您确保用户没有对脚本或目录的写访问权限。 (2认同)