如何创建一个 linux 用户,其中 sudo 提示输入 root 密码而不是当前用户

Doc*_*tor 0 users root debian sudo

我创建了一个新用户,adduser但是当我尝试从他的帐户 sudo 时,我得到了xxxx * is not in the sudoers file. This incident will be reported..

所以我开始篡改visudo命令......

我知道 root 帐户具有以下配置:

root    ALL=(ALL:ALL) ALL
Run Code Online (Sandbox Code Playgroud)

但我不想为我的新用户复制这个...


我希望我的用户 xxxx 没有 root 访问权限,除非使用 sudo
使用 sudo 时,我希望 xxxx 用户提示输入 root 密码而不是他自己的密码!!

谢谢


使用 debian 10
VM,仅 ssh

tel*_*coM 5

我希望我的用户 xxxx 没有 root 访问权限,除非使用 sudo。

这实际上是普通用户的正常状态:除了配置sudo以指定允许哪些命令之外,您无需执行任何操作。

root    ALL=(ALL:ALL) ALL
Run Code Online (Sandbox Code Playgroud)

sudo如果您已经切换到 root 用户,则存在此行只是为了允许使用。授予非 root 用户权限的重要一行是这一行:

# Allow members of group sudo to execute any command
%sudo   ALL=(ALL:ALL) ALL
Run Code Online (Sandbox Code Playgroud)

(在某些 Linux 发行版中,在 OS 安装期间生成的第一个普通用户可能会成为该sudo组的成员,因此会自动为他们启用 root 访问权限。这是例外,并非所有用户的默认设置。)

sudoers 文件中权限规范的基本形式是

<who>  <where>=(<as_who>) <what>
Run Code Online (Sandbox Code Playgroud)
  • <who>指定哪些用户可以使用此条目。它可以是用户、组(以 % 符号为前缀)或先前定义的 User_Alias(基本上是一长串用户和/或组的简写)和其他一些内容。

  • <where>可以是主机名:它将此条目限制为特定系统。它在企业环境中可能很有用,您sudoers在所有主机上放置一个集中管理的标准文件,但通常将其指定为 ALL 以避免在主机名解析不起作用或系统配置错误的情况下出现问题。

  • (<as_who>)定义可以运行命令的用户帐户。默认值为 root,但您也可以限制用户仅在某些特定的非 root 应用程序帐户上使用 sudo 运行命令。

  • <what> 是允许的命令(或命令,以逗号分隔)。

还有各种标签和选项,但以上是sudoers简而言之文件的重要部分。

使用 sudo 时,我希望 xxxx 用户提示输入 root 密码而不是他自己的密码

这是可能的,而且很容易做到。如果将这些行添加到sudoers文件中:

Defaults:xxxx rootpw,timestamp_timeout=0
xxxx    ALL=(ALL:ALL) ALL
Run Code Online (Sandbox Code Playgroud)

然后对于用户xxxx(并且对于他们),该sudo命令将要求输入 root 密码 - 并且每次使用sudo命令时都会询问,而不是允许最多 15 分钟的时间使用该sudo命令而不询问的默认行为再次输入密码。

如果您计划监视用户并在该用户需要时自己输入 root 密码,这一点很重要 - 如果您错过了该timestamp_timeout=0选项,用户可能会首先要求您输入 root 密码以进行某些合法操作,然后分散注意力在接下来的 15 分钟内为您提供,在此期间,他们将能够使用sudo任何他们想要的东西。

但请注意,如果您将 root 密码告诉您的用户,他们也可以将其与su命令一起使用,除非您对su. 经典的(通常也是唯一的)限制方法su是要求用户成为特定组的成员,否则他们将根本无法使用su。要应用此限制,请编辑/etc/pam.d/su并在开头附近找到此部分:

# Uncomment this to force users to be a member of group root
# before they can use `su'. You can also add "group=foo"
# to the end of this line if you want to use a group other
# than the default "root" (but this may have side effect of
# denying "root" user, unless she's a member of "foo" or explicitly
# permitted earlier by e.g. "sufficient pam_rootok.so").
# (Replaces the `SU_WHEEL_ONLY' option from login.defs)
# auth       required   pam_wheel.so
Run Code Online (Sandbox Code Playgroud)

只需#从最后一行的开头删除,该su命令将root仅供组成员使用。