Change Password Programmatically

You're looking for the chpasswd command. You'd do something like this:

echo 'pi:newpassword' | chpasswd # change user pi password to newpassword

Note that it needs to be run as root, at least with the default PAM configuration. But presumably run as root isn't a problem for a system deployment script.

Also, you can do multiple users at once by feeding it multiple lines of input.


Another alternative is to use the yes command in your script.

yes newpassword | passwd youruser

This will send newpassword to the passwd command for youruser.

It should be mentioned that setting/modifying user passwords via scripts may present security risks and should be avoided whenever possible.

EDIT:

This answer requires root access. Apologies for not mentioning this previously. It is a method that I use when performing administration tasks which require root access.