[TriLUG] automatically generating an encrypted passwd for useradd -p for RHEL

Jeremy Portzer jeremyp at pobox.com
Tue Dec 2 09:51:40 EST 2003


On Tue, 2003-12-02 at 08:17, Marvin Blackburn wrote:
> I need a method for automatically generating a crypt(3) password so I can
> input it into the
> usermod -p command to force a user to enter a new password when logging in.
> 
> I saw a way to use python to generate the key, but its not automatated.
> 

I use Perl, like this.  The password to be encrypted is in the $word
variable from other code.  Note that you need to prepend the string
'$1$' to the 8-character salt to get an MD5 password.  This  is true for
the crypt(3) library function too, that's not just a Perl thing.

# allowed chars in password salts:
   my $saltset =
   "./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

    my $salt ="";
    #figure out a random salt:
    for (my $i=0;$i < 8; $i++)
    {
        $salt = $salt . substr($saltset, int rand length $saltset, 1);
    }
     
    # return password and its encrypted version:
    return ($word,crypt($word,"\$1\$$salt"));


Let me know if you want the entire code to this Perl script, which
generates random passwords too.  You can also of course use the
"mkpasswd" command to generate the passwords themselves.

--Jeremy

-- 
/---------------------------------------------------------------------\
| Jeremy Portzer        jeremyp at pobox.com      trilug.org/~jeremy     |
| GPG Fingerprint: 712D 77C7 AB2D 2130 989F  E135 6F9F F7BC CC1A 7B92 |
\---------------------------------------------------------------------/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 189 bytes
Desc: This is a digitally signed message part
URL: <http://www.trilug.org/pipermail/trilug/attachments/20031202/e87e36d1/attachment.pgp>


More information about the TriLUG mailing list