Ubuntu – How to set user passwords using passwd without a prompt

password

I am writing a script to add a large amount of users to a system. Part of this involves setting default passwords for each user. How can I set users' passwords without it prompting me for the password up front?

Unfortunately passwd doesn't seem to take an argument stating the new password to set. I'm using Ubuntu 11.10.

Best Answer

Try usermod:

usermod --password PASSWORD USERNAME

The only thing is this needs a pre-encrypted password string which you'd have to generate first.

In order to generate the encrypted password you can use openssl. For example:

usermod --password $(echo MY_NEW_PASSWORD | openssl passwd -1 -stdin) USERNAME