Change user password within bash script on Ubuntu/Debian

While creating a bash script to automate the configuration of a vhost and wordpress installation, I needed to automatically create an ubuntu user and assign the password from within the script without ANY user interaction.

I tried many different methods to no avail;  The only solution that worked for me was piping username:password into chpasswd:

  • echo “$usrn:$usrpwd” | chpasswd

source: http://www.ibm.com/developerworks/aix/library/au-chpasspwgen.html

Subscribe

Subscribe to our e-mail newsletter to receive updates.

4 Responses to “Change user password within bash script on Ubuntu/Debian”

  1. mahmudin ashar July 10, 2010 at 12:32 am #

    nice article, simple and work for me. Thanks ..

  2. Deafboy April 30, 2011 at 12:06 pm #

    Not working…. Debian squeeze, bash

    [root@cicolina bin]# echo “test:testpass” | chpasswd
    [root@cicolina bin]# cat /etc/passwd | grep test
    test:x:5060:100:here:/home/users/test:/bin/bash
    [root@cicolina bin]#

    • Deafboy April 30, 2011 at 12:08 pm #

      Sorry, ma fault! I should have looked into /etc/shadow. It’s working, you can delete my previous post

  3. Jason Browne January 13, 2012 at 1:17 pm #

    Spent half a day trying everything I could find for this. This finally worked. Thanks bunches!

Leave a Reply