Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 如何检查使用useradd和groupadd创建用户和组的权限_Linux_Bash_Privileges - Fatal编程技术网

Linux 如何检查使用useradd和groupadd创建用户和组的权限

Linux 如何检查使用useradd和groupadd创建用户和组的权限,linux,bash,privileges,Linux,Bash,Privileges,如何检查当前用户是否具有使用useradd和groupadd创建用户和组的所有权限 我不想不必要地为我的bash脚本请求root权限(例如要求成为root或调用sudo)。相反,我只想确保有使用这些命令的权限 命令: $ ls -l $(which useradd) $(which groupadd) -rwxr-xr-x 1 root root 93136 Mai 28 2020 /usr/sbin/groupadd -rwxr-xr-x 1 root root 147160 Mai 28

如何检查当前用户是否具有使用
useradd
groupadd
创建用户和组的所有权限

我不想不必要地为我的bash脚本请求root权限(例如要求成为root或调用sudo)。相反,我只想确保有使用这些命令的权限

命令:

$ ls -l $(which useradd) $(which groupadd)
-rwxr-xr-x 1 root root  93136 Mai 28  2020 /usr/sbin/groupadd
-rwxr-xr-x 1 root root 147160 Mai 28  2020 /usr/sbin/useradd

假设您需要root或sudo来添加新用户(对于组也是如此),您可以通过检查用户是否在相应的组中来检查该用户是否具有sudo权限

   getent group sudo  // shows all users in groupd sudo

不知道您在哪个系统/发行版上-但在arch上,例如sudoers在group wheel中…

由于
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.
在命令行中没有笨拙的引用,只需在您想要的命令前面加上sudo这个词。如果要以root以外的用户身份运行该命令,只需添加-u username开关:

$ sudo -u <USER_NAME> useradd username
$sudo-u用户添加用户名
将有一个日志条目写入/var/log/secure文件,以显示是谁做的契约

当然,系统管理员可以将sudo配置为不请求密码。在这种情况下,尽管仍将写入审核跟踪条目,但会立即执行该命令

参考资料:-

如需任何帮助,请访问评论部分


我很乐意帮忙

您可以通过以下方式检查用户权限:


id

关于Linux debian Linux 5.10.0-6-amd64#1 SMP debian 5.10.28-1(2021-04-09)x86_64 GNU/Linux,
您可以在脚本中尝试这种方式

groupadd 2>/dev/null ; if test $? -eq 2 ; then echo ok ; else echo bad ; fi
如果可以访问groupadd或useradd,则返回值为2,因为缺少参数。

如果您无法访问groupadd或useradd,则返回值为127。

请将
ls-l$(哪个useradd)$(哪个groupadd)
的输出添加到您的问题中。最简单的方法是尝试一下,看看结果是否为eperm。您的意思是
-eq 0
?请参阅(页面底部…退出值下)@DavidC.Rankin No-eq 2是一个不错的选择(命令语法无效),因为groupadd需要参数。对于未授权的用户,返回值是127,我在屏幕上看到:bash:groupadd:commande introvableOh…kay。。。如果要区分返回值,比如
case$?在里面esac
可能更有帮助。这样,您可以在计算
0
以确认成功的同时计算任意多个不同的返回代码,而不仅仅是检测无效语法。我的意思是,检查
2
的返回没有什么错——这确实告诉您有无效的语法,但就其本身的有用性而言,它是相当有限的。许多/大多数发行版仍然为此使用
wheel
组。
sudo
组是最近才加入的(过去5-6年左右是最近加入的)
groupadd 2>/dev/null ; if test $? -eq 2 ; then echo ok ; else echo bad ; fi