Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»Programming»Bash Shell»How to Assign Password to User in a Shell Script

    How to Assign Password to User in a Shell Script

    By RahulNovember 27, 20141 Min Read

    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. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    cp Command in Linux (Copy Files Like a Pro)

    dd Command in Linux (Syntax, Options and Use Cases)

    Top 10 JQ Commands Every Linux Developer Should Know

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    • dd Command in Linux (Syntax, Options and Use Cases)
    • Iptables: Common Firewall Rules and Commands
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.