检查用户是否是root
如何在 PHP 脚本中验证用户是否为 root 用户?命令是什么? 我尝试了类似的操作:
exec("su -l login < `echo password`");
但是 su 命令无法接收密码...
请注意,该机器与互联网隔离,因此如果需要,我可以以 root 身份运行 PHP。
编辑: 我不想知道运行脚本的当前用户是否是root。 我的 PHP 脚本中有一个用户列表,我想知道每次登录时他是否具有 root 权限。
How can i verify if a user is root in a PHP script ? What is the command ?
I tried something like that :
exec("su -l login < `echo password`");
but the su command can not receive password...
Note that the machine is isolated from internet so I can run PHP as root if needed.
EDIT:
I don't want to know if the current user who run the script is root or not.
I have a list of users in my PHP script and I want to know for each login if he has root privileges.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
posix_getuid()
怎么样? ?How about
posix_getuid()
?首先,问问自己“具有 root 权限”的登录的确切定义是什么。 AFAICT 有 2 个基本解决方案。
系统管理员使用 uid 0 创建多个帐户的老式方式,我(而且我当然不是唯一这样做的人)认为这是一场噩梦。在这种情况下,您可以使用 posix_getpwnam 检查列表中的所有用户并查看他们的 uid 是否匹配 0。
以下代码片段就是这样做的,$privileged 将包含具有 root 权限的用户:
另一种(恕我直言,只有明智的)方法是将所有具有 root/管理权限的用户添加到特定组(wheel 或 admin 已在不同的 Linux 发行版中使用,找出哪一个适合您)。这种情况更简单,因为您可以使用 posix_getgrnam 来获取特定组中的所有成员。
以下代码片段将匹配您提供的登录名数组,并查看谁是具有特定权限的成员,$privileged 将再次包含结果(即列表中属于您指定组的成员的用户):
First, ask yourself what exactly defines a login "having root privileges". AFAICT there are 2 basic solutions.
The old-school way, where sysadmins create multiple accounts with uid 0, which I - and I'm certainly not alone in this - consider to be a nightmare. In this scenario you could check all users in your list using posix_getpwnam and see if their uid matches 0.
The following code snippet does just that, $privileged will contain the users with root privileges :
The other (and imho only sane) way to do this is to add all users with root/administrative privileges to a specific group (wheel or admin are already used in different Linux distributions, find out which one works for you). This scenario is even simpler, since you can use posix_getgrnam to fetch all members in a specific group.
The following code snippet will match an array of logins you provide, and see who's a member with specific privileges, again $privileged will contain the result (ie. the users in your list that are a member of the group you specified) :
试试这个。
Try This .
如果你的意思确实是“is root”,那么你可以检查
whoami
,看看它是否出现“root”。如果这是一个明智的做法,那就是另一个问题了;)
If you really mean "is root", then you can just check for
whoami
, and see if it comes up 'root'If it's a smart thing to do is a different question ;)
评论后重写
我现在明白你需要在 PHP 中执行 root 身份验证。
我建议使用
pwauth
而不是 whoami/login 等。这里是一个关于如何从 PHP 使用它的示例,而我相信可以有更简单的测试方法。
Rewritten after your comment
I now understood that you need to perform a root authentication in PHP.
I suggest to use
pwauth
instead of whoami/login, etc.Here is an example on how to use it from PHP, while I believe there can be a simpler way of testing.
如果您想查看某个系统用户(而不是Web服务用户)是否具有root权限,您可以使用
posix_getpwnam()
函数:请记住,这仅检查用户名。如果您还需要检查密码,您实际上必须尝试通过系统进行身份验证。
If you want to see if a certain system user (as opposed to a web-service user) has root priviliedges, you can use the
posix_getpwnam()
function:Keep in mind that this only checks the username. If you also need to check the password you actually have to try to authenticate with the system.