Changing password via a script

Ask a question
To change a password in Linux through a Bash script, he two scripts that are most important are 'Create Users' and 'Change Passwords Shell' scripts, for the system admin which regularly uses the mail servers, as there might be multiple functionalities associated with the admin job. If there is a failure to backup the admin's system username and password, it hampers other admin tasks and chances of password misappropriation are high. If you lose the usernames and passwords of the mail server, the admin has to manually create all the users again and then change the passwords for all of them. There are two methods to change passwords on the system if you are using the Linux operating system .The conventional command is passwd, -stdin, pipe. One more option that can be used is the chpasswd.

Method 1: passwd


Conventionally, the command use to change a password on a Linux based system is passwd. The option related to this command is -stdin , and this is all done through a pipe

echo -e "new_password\nnew_password" | (passwd --stdin $USER)

Method 2: chpasswd


An alternative is to use chpasswd, as below:

echo "password:name" | chpasswd


Note that the first method can be use to change the password on Samba based system:
echo -e "new_password\nnew_password" | (smbpasswd -a -s $USER) 
Jean-François Pillou

CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Learn more about the CCM team