简体   繁体   English

我正在尝试从 PHP 创建 Linux 用户

[英]I am trying to create Linux users from PHP

shell_exec("sudo useradd -m $username");

shell_exec("yes $password | sudo passwd $username");

The code snippet above successfully creates the user but it does not set the password, am I doing something wrong?上面的代码片段成功创建了用户,但没有设置密码,我做错了吗?

在此处输入图像描述

The $username is the megan user at the bottom of the shadow file $username 是影子文件底部的 megan 用户

You cannot invoke passwd non-interactively.您不能以非交互方式调用passwd Period.时期。

You can supply useradd with a pre-computed password hash with the -p option, though.不过,您可以使用-p选项为useradd提供预先计算的密码 hash。 [See: man useradd ] [参见: man useradd ]

function os_pw_hash($password) {
    $salt = base64_encode(random_bytes(12)); // 16-byte salt
    return crypt('hunter2', '$6$'.$salt.'$');
}

var_dump( os_pw_hash('hunter2') );

Output: Output:

string(106) "$6$0LIJoQz2W0vP35Ej$kg75OyhAZb9iAbqa/sO56pXs/peA8wPd4DKv5Al0FllBApBe7BvXUA6Q6fQ3bqpxfz.XH6GWnI.mH6yLfTQil1"

You're also going to want to run this [and honestly all your shell parameters] through escapeshellarg() to make sure metacharacters are properly escaped.您还需要通过escapeshellarg()运行此 [以及老实说您的所有shell 参数] 以确保正确转义元字符。

Lastly:最后:

for this use case security is not a concern对于这个用例,安全不是问题

Security is always a concern .安全始终是一个问题 This is usually doubly true for cases when you don't think it should be.对于您认为不应该的情况,这通常是双重的。 I have had users that I unfortunately trusted to know better exploit security holes in internal applications to execute commands with root privileges in order to avoid simply having to make a ticket.不幸的是,我有一些我信任的用户知道如何更好地利用内部应用程序中的安全漏洞以 root 权限执行命令,以避免简单地制作票证。

Don't do this.不要这样做。

You want as much isolation from the outward facing parts of your system (web pages) from the internal administration.您希望将系统的外向部分(网页)与内部管理隔离开来。 To that end your script should only be able to create users within the constraints you set.为此,您的脚本应该只能在您设置的限制范围内创建用户。 Write a separate script which takes 2 arguments - a username and a password (although for preference it should generate a random password) which applies THOROUGH validation of the inputs (eg no '/' in user name) and give your webserver uid sudo provileges on that script only (it could be iwritten n PHP calling adduser directly).编写一个单独的脚本,它需要 2 arguments - 一个用户名和一个密码(尽管它应该生成一个随机密码),它应用对输入的彻底验证(例如,用户名中没有'/')并给你的网络服务器 uid sudo provileges on该脚本(可以直接调用 adduser 编写 n PHP)。

The reason your code isn't working is that passwd clears the input buffer before reading the password.您的代码不起作用的原因是passwd在读取密码之前清除了输入缓冲区。 And typically it asks for the new password twice - but the prompts and replies vary by context.通常它会要求输入新密码两次 - 但提示和回复因上下文而异。 There are other programs you can use for setting passswords which are more consistent - chpasswd is fairly standard on Linux systems - and as Sammitch says some versions of useradd allow the password to be specified at the time the user is created.您可以使用其他程序来设置更一致的密码 - chpasswd 在 Linux 系统上是相当标准的 - 正如 Sammitch 所说,某些版本的 useradd 允许在创建用户时指定密码。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试在.php文件中创建到另一个.php文件的链接 - I am trying to create a link in a .php file to another .php file 我正在尝试创建一个按钮,以删除使用PHP和AJAX从MySQL中选择的一行数据。 - I am trying to create a button to delete a row of data selected from MySQL using PHP and AJAX. 我正在尝试从C#使用PHP SOAP服务并在VS 2010中创建类包装器 - I am trying to consume a PHP SOAP service from C# and create a class wrapper in VS 2010 从php添加Linux用户 - adding Linux users from php 我正在尝试从经过身份验证的用户配置文件的用户表中传递用户的 ID - I am trying to pass the id of a user from the users table from an authenticated users profile 我正在尝试在线显示用户列表 - I am trying to display the list of users online 你好。 我正在尝试在 laravel 中创建一个项目,并为用户和管理员登录/注销。我已经接受了用户 model - Hello. I am trying to create a project in laravel with login/logout for users and admin.I have take users model 我正在尝试在PHP的无序列表中打印所有数据库用户。 但这不起作用 - I am trying to print all the database users in a Unordered list in PHP. But it is not working 我正在尝试使用PHP中的Ajax从数据库中获取图像 - I am trying to fetch image from database using ajax in php 我正在尝试从 php 的月份中获取周末日期 - I am trying to get weekend dates from Months in php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM