Search code examples
phpshellexecuteshell-execpasswd

PHP passwd error


I'm doing this:

$user = 'kevin';
$pass = 'nivek';
shell_exec('echo -e "' . $pass . '\n' . $pass . '" | sudo passwd ' . $user);`

But when I execute this, I get this error:

Enter new UNIX password: Retype new UNIX password: Sorry, passwords do not match
passwd: Authentication token manipulation error
passwd: password unchanged

Why? How can I solve this and how can I hide the output from passwd?


Solution

  • You can use the following code:

    $user = 'kevin';
    $pass = 'nivek';
    shell_exec('echo -e "' . $pass . '" | sudo passwd --stdin ' . $user);
    

    That appears to work for me (although tested without sudo, you may need the second).

    I'm not sure that sudo would even work.