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.


Try usermod:

usermod --password PASSWORD USERNAME

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

  • 1
    ...which I can generate using mkpasswd. Excellent, thanks! – Jake Petroules Nov 18 '11 at 11:27
  • 16
    You can also use openssl to generate the encrypted password. For example: usermod --password $(echo my_new_password | openssl passwd -1 -stdin) USERNAME – Eric Smith Jan 16 '15 at 19:34
  • 1
    Three years too late. I couldn't make the answer @EricSmith, but I did discover a simpler version of the command that did work: usermod --password $(openssl passwd -1 {password}) {username} – BoCoKeith Nov 29 '18 at 15:36

You should look at the chpasswd command (if available in your linux flavor):

echo 'userid:newpasswd' | chpasswd

Or, you can cat a file listing userid:passwd for each account on a separate line.

That's it.

  • this worked for me in Dockerfile – Vishrant Jan 27 at 18:58

Inspired by Eric Smith's idea, combining openssl passwd and usermod -p command worked. Generate hashed value of password along with salt value.

$ openssl passwd -1  -salt 5RPVAd clear-text-passwd43

Then, copy the encrypted string to usermod. Make sure to wrap it with single quote.

$ usermod -p '$1$5RPVAd$vgsoSANybLDepv2ETcUH7.' root

Check it out in shadow file.

$ grep root /etc/shadow

You should use password aging, and set the users so that they must change their password on the first login. See this article.

  • These intended to be FTP-only accounts, but good advice. ;) – Jake Petroules Nov 18 '11 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.