środa, 14 stycznia 2015

bash script for changing password

I have seen a lot of articles describing how to change password via a script, some of them work most of them don't.
Below I present option that works very well.

On a local machine can perform command:
echo -e "user:password" | chpasswd

If we are not happy with pasting in password in plain text then we use mkpasswd to encrypt the password and modify slightly command for changing password:
root@debian:/var/www# mkpasswd

Then we use our encrypted password in command:
echo -e "user:TqDuqPhgvp7pM" | chpasswd -e

What about running this command remotely on a server:
ssh root@IP_ADDRESS 'echo -e "user:password" | chpasswd'

encrypted version works almost the same
ssh root@IP_ADDRESS 'echo -e "user:encypted_password" | chpasswd -e'

What if I want to change password on multiple servers at once:
create a file (list.txt) with a list of ip addresses one under another

and run a command in a loop:
for i in $(cat list.txt); do ssh root@$i 'echo -e "user:password" | chpasswd' && echo $i ; done

This post might be little bit outdated because everyone should be using keys instead of passwords.
At my work we use both this is why I wanted to do it almost automatically.

Brak komentarzy:

Prześlij komentarz