Command:

echo <password> | passwd –stdin <username>

Example:

Use the following command to change password of user jack in a shell script. For example we are using string password as password.

Advertisement
echo "password" | passwd --stdin jack

Assigning User Input Password:

Use the following commands to input password from user and assign to user jack.

read -p "Enter Password for User jack: " pwd
echo $pwd | passwd --stdin jack

Also we can prompt password twice from user to make confirmation that user has memorize it correctly. Use the following commands to input password twic from user and assign to user jack.

while :
do
    read -p "Enter Password for User jack: " pwd1
    read -p "Confirm Password for User jack: " pwd2
    if [ "$pwd1" == "$pwd2" ]
    then
		break
    else
	    echo "Password and Confirm password doesn't match...."
    fi
done
echo $pwd1 | passwd --stdin jack
Share.
Leave A Reply

Exit mobile version