由于useradd 和groupadd 命令需要一些额外的权限才能运行,您可以为特定命令(如 useradd 和 groupadd)设置对 sudo 的访问权限,如下所示:-
请看一遍,它会让你明白大部分事情
控制对 sudo 的访问
/etc/sudoers 文件配置用户可以使用 sudo 访问的程序,以及是否需要密码。
系统管理员使用/usr/sbin/visudo 命令将用户添加到此文件。文件中的每个非注释行都有两部分:
用户名 ("") 或组名 ("%")。
可以运行程序的机器名称列表,或关键字 ALL。在等号 (=) 之后,可以运行命令的用户身份列表,括在圆括号(括号)中;通配符 ALL 也可能出现。最后,可以作为命名用户运行的应用程序列表;关键字 ALL 是通配符。
以下示例应该有助于说明这一点:
<USER_NAME> ALL=(ALL) ALL
# User <USER_NAME> can execute any command as any user, but must know the password to the <USER_NAME> account.
<USER_NAME> ALL=(root) shutdown
# User <USER_NAME> can execute only command shutdown, but must know the password to the <USER_NAME> account.
<USER_NAME> ALL=(root) NOPASSWD: /usr/bin/id
# User <USER_NAME> can execute only the application /usr/bin/id; no password will be needed.
<USER_NAME> ALL=() NOPASSWD: /usr/bin/id
# User <USER_NAME> can execute only the application /usr/bin/id; no password will be needed.
一旦系统管理员在 /etc/sudoers 文件中输入了必要的设置,用户就可以像这样安全地访问特权系统资源和活动:
$ sudo useradd username
在命令行上没有尴尬的引用,只需在您想要的命令前加上 sudo 一词即可。如果你想以 root 以外的用户身份运行命令,只需添加 -u 用户名开关:
$ sudo -u <USER_NAME> useradd username
将有一个日志条目写入 /var/log/secure 文件以显示谁做了这件事。
当然,系统管理员可以配置 sudo 不请求密码。在这种情况下,尽管仍会写入审计跟踪条目,但该命令会立即执行。
参考:- Sudo Tutorial
请在 cmets 部分寻求帮助
很乐意提供帮助!!!