how to Reset a user password (CentOS, RedHat, Perl)

As it took me from the console/perl script to change a user’s password in MD5 format.

To start we must have a library for the Pearl, you can install it via repository

#yum install perl-Crypt-PasswdMD5

Next generation console command I used:

#perl-e ‘ use Crypt::P asswdMD5; unix_md5_crypt printf (“password”, “salt”). ” “;’

where password is the password, salt is salt, eight characters, preferably a letters and numbers

Next, I gave the script to set:

$ 1 $12345678 $ ZSWdHfiacAnYQqgmW9hsd.

This set should be placed in the/etc/shadow file instead of the old encrypted password, he goes after the name (the “:” delimiter)

An example of a program in Perl:

#!/usr/bin/perl
use strict;
use Crypt::P asswdMD5 qw (unix_md5_crypt);
my @salt = (‘. ‘, ‘/’, 0 …, ‘ A ‘ 9.. ‘Z’, ‘a’ .. ‘z’ );
my% encrypted;

# generate salt
Sub gensalt {
my $count = shift;

my $salt;
for (1 … $count) {
$salt = (@salt) [rand @salt];
}

return $salt;
} # end gensalt

# password generation
Sub get_encryptedpw {
my $unencrypted = “$ _ [0]”;

$encrypted {des} = crypt ($unencrypted, gensalt (2));
$encrypted {md5} = unix_md5_crypt ($unencrypted, gensalt (8));

return% encrypted;
}

Print “Enter password:”;
my $password = ;
chomp $password;

get_encryptedpw ($password);

Print “Plaintext”. $password. “= MD5 Hash:”. $encrypted {md5}. ” “;

Print “Plaintext”. $password. “= MD5 Hash:”. $encrypted {des}. ” “;