useradd via PHP


having some problems adding users to linux from a php script:

I have a PHP script that is triggered by a CRON job having root rights.
This scripts is feeded with usernames from a database and has
to add the users to the local users on the webserver via

useradd [accountname] -p [passwordhash] -s /bin/false -m

the problem is the second parameter [passwordhash]

how to generate a valid password hash in PHP and how to
give it to useradd via PHP or in other words:


Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Hi  JtR

Createing an password string is pretty easy using the crypt function
returns a string (ie passwordhash) crypt ( string str [, string salt] )
Check out for details.
It will default to creating a DES string if you don't give it a salt

It's pretty straight forward if you want if you want to create a MD5 it needs a 12 char length string starting with $1$ and ending with $
You can use something like
$salt = '$1$';
$chars =  array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","8","9","0" );
for ($i = 0 ; $i < 8; $i ++)  
  $salt .= $chars[rand (0,(count($chars)-1))];
$salt .= '$';
$passwdhash = crypt ( $passwd,$salt);
Hope that helps


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JtRAuthor Commented:
Yes you are right! Have solved it some hours later!

The second parameter $salt tells the stupid crypt function what algorithm to use.
If it starts with to characters (I took "AB") then it throws DES crypted strings.
So the solution is easy:

$pw_hash = crypt( $password, "AB" );
system( "useradd $username -p $pw_hash -s /bin/false -m" );

So bad that your comment was so late! It had saved me some hours!
So what! Thank you! :)

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Web Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.