How to set user passwords using passwd without a prompt?
Solution 1:
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
Solution 2:
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.
Solution 3:
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
$1$5RPVAd$vgsoSANybLDepv2ETcUH7.
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
root:$1$5RPVAd$vgsoSANybLDepv2ETcUH7.:17774:0:99999:7:::
Solution 4:
Here is a good solution, just one line:
useradd -p $(openssl passwd -1 "$pass") "$user"
I can add others parameters like -m to create the home directoty, etc.