阻止来自 sudo 用户的命令

Mag*_*gam 11 root sudo

我不需要管理员来更改我的 root 密码。我不希望任何 sudo 用户执行此命令:

sudo passwd $root
Run Code Online (Sandbox Code Playgroud)

我已经使用以下命令在 sudoers 文件中尝试过:

%sudo ALL=(ALL) ALL, !/usr/bin/passwd $root
Run Code Online (Sandbox Code Playgroud)

我怎样才能阻止它?

Let*_*ety 16

根据sudoers 手册

   It is generally not effective to "subtract" commands from ALL using the
   ’!’ operator.  A user can trivially circumvent this by copying the
   desired command to a different name and then executing that.  For
   example:

       bill        ALL = ALL, !SU, !SHELLS

   Doesn’t really prevent bill from running the commands listed in SU or
   SHELLS since he can simply copy those commands to a different name, or
   use a shell escape from an editor or other program.  Therefore, these
   kind of restrictions should be considered advisory at best (and
   reinforced by policy).
Run Code Online (Sandbox Code Playgroud)

这就是您的 sudoers 政策不起作用的原因。

如果您想阻止用户获得 root 权限并更改其密码,请尝试以下步骤:

  • 假设您的 sudoers 包含此指令:

     root    ALL=(ALL:ALL) ALL
     %sudo   ALL=(ALL:ALL) ALL
    
    Run Code Online (Sandbox Code Playgroud)
  • 假设您的用户名是foo,他的组是foosudogroups命令输出为:

    foo sudo
    
    Run Code Online (Sandbox Code Playgroud)
  • foosudo组中删除用户:gpasswd -d foo sudo此后,用户foo无法使用 sudo 运行任何命令。

  • 编辑 sudoers 文件。使用这个命令:

    sudo visudo -f /etc/sudoers.d/foo
    
    Run Code Online (Sandbox Code Playgroud)
  • 定义用户foo权限,例如:

    foo ALL=/usr/bin, !/usr/bin/passwd, !/usr/bin/su
    
    Run Code Online (Sandbox Code Playgroud)

    这意味着用户foo可以运行目录中/usr/bin/除了passwdandsu命令之外的任何命令。注意:如果用户foo想更改他的密码,可以passwd不带sudo.

  • 另一个用户foo权限示例:

    foo ALL =/usr/bin, /usr/bin/passwd [A-Za-z]*, !/usr/bin/passwd root
    
    Run Code Online (Sandbox Code Playgroud)

    这意味着用户foo可以在目录中运行任何命令,/usr/bin/并且可以在所有机器上更改除 root 之外的任何人的密码。

您可以通过定义Cmnd_Aliases和创建“权限级别”来定义命令组。您可以在sudoers 手册的示例部分找到有用的示例,这里有一个关于如何使用 sudoers的有用链接