useradd via PHP

Posted on 2006-04-04
Last Modified: 2008-03-17

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:


Question by:JtR
    LVL 3

    Accepted Solution

    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

    LVL 3

    Author Comment

    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! :)


    Featured Post

    Highfive + Dolby Voice = No More Audio Complaints!

    Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

    Join & Write a Comment

    When pages do not download correctly, and you don't know why, the first thing you do is to look at the HTML source code of that page, but not all the downloaded files appear always clearly. If your source includes a javascript that computes the name…
    Problem to be resolved in this article Currently, development of website and web application can be done without writing thousands of lines of programming code by hand. Description This can be done through by using a open source framework such …
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    14 Experts available now in Live!

    Get 1:1 Help Now