Archive for September 15th, 2007

Manual encrypting your shadow password

Posted in Linux SysAdmin on September 15th, 2007 by Johan Huysmans – 3 Comments

Your passwords are stored encrypted in the /etc/shadow file. This encryption is normally done by the passwd command, but for some cases you want to produce your own encrypted password for manually placing it in the shadow file.

With following perl oneliner you can produce encrypt your password:
# echo "EnterHereYourNonEncryptedPassword" | perl -nle 'print crypt($_, "\$1\$".join "", (".", "/", 0..9, "A".."Z", "a".."z")[rand 64, rand 64, rand 64, rand 64, rand 64, rand 64, rand 64, rand 64]);'
This will print an ecrypted string on the screen:
$1$UckA8UmW$Ck0rISvkyk2rDutFT4nU20

Let’s split the oneliner in different pieces so it is easier to understand. The most important perl function, which does the actual encrypting, used in the oneliner is:
crypt("password", "salt");
The “salt” is a random string starting with $1$. Following join will produce such 8 charachters long random string:
join "", (".", "/", 0..9, "A".."Z", "a".."z")[rand 64, rand 64, rand 64, rand 64, rand 64, rand 64, rand 64, rand 64];

More information about the crypt function can be found here: http://perldoc.perl.org/functions/crypt.html.
As you can notice the join is also listed on that page ;)