阻止来自sudo用户的命令

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

sudo passwd $root 

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

 %sudo ALL=(ALL) ALL, !/usr/bin/passwd $root 

我该怎么阻止它?

根据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). 

这就是为什么你的sudoers政策不起作用。

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

  • 假设你的sudoers包含这个指令:

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

     foo sudo 
  • sudo组删除用户foogpasswd -d foo sudo之后,用户foo无法使用sudo运行任何命令。

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

     sudo visudo -f /etc/sudoers.d/foo 
  • 定义用户foo权限,例如:

     foo ALL=/usr/bin, !/usr/bin/passwd, !/usr/bin/su 

    这意味着用户foo可以在目录/usr/bin/passwdsu命令之外的任何命令。 注意:如果用户foo想要更改密码,可以在没有sudo情况下运行passwd命令。

  • 用户foo权限的另一个例子:

     foo ALL =/usr/bin, /usr/bin/passwd [A-Za-z]*, !/usr/bin/passwd root 

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

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

通过visudo添加命令别名:

 Cmnd_Alias PASSWD=/usr/bin/passwd Cmnd_Alias SU=/bin/su 

通过visudo添加限制:

 %nopasswdgroup ALL = ALL, !PASSWD, !SU 

将用户添加到名为nopasswdgroup的组:

 usermod -aG nopasswdgroup nopasswduser