Skip to content

How to Assign Password to User in a Shell Script

Advertisement

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.

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

Rahul

I, Rahul Kumar am the founder and chief editor of TecAdmin.net. I am a Red Hat Certified Engineer (RHCE) and working as an IT professional since 2009..

Leave a Reply

Your email address will not be published. Required fields are marked *


Back to top