This question already has an answer here:

For reasons I want to change a user's password to a random one every X minutes using a script. It's a debian machine, the following script is started via @reboot in root's crontab and the script is running. Every X minutes the new password is written to the file specified in the script, however, the password is never changed.

# change user's password to a random one on boot and every five minutes

while : ; do
    npasswd=$(pwgen 8 1)
    echo "$npasswd" > /root/current-user-passwd.txt
    usermod --password $(openssl passwd -1 "$npasswd") user
#   echo "$npasswd" | passwd user
#   usermod -p $(perl -e "print crypt("$npasswd","Q4")") user
    sleep 300

I have tried each of the three methods (the other two being commented out now), none work. However, when I copy the entire script and just paste it in a terminal to run it, it works perfectly fine. It just doesn't work in a script if it's started by cron.

Does anyone know what is going on here?
I've also tried echo -n by the way, same results. I've also tried to set $npasswd in the script to something fixed, just to test it, same results.

Just to clearify: I'm really looking for an answer that explains why this is working manually, but not in a script, not for an alternative way to change the password. Those methods do work, just not if the script is started by cron.

Output of the script with set -o errexit -o nounset -o xtrace on top:

+ :
++ pwgen 8 1
+ npasswd=eiZeed5u
+ echo eiZeed5u
++ openssl passwd -1 eiZeed5u
+ usermod --password '$1$EYRCYJXm$5XkrVNxdrYRzxBmcRwuXm0' user
+ sleep 300

Output of the above when started via cron's @reboot:

+ :
+ pwgen 8 1
+ npasswd=aizof5Ka
+ echo aizof5Ka
+ openssl passwd -1 aizof5Ka
+ usermod --password $1$KFrgibYH$e1dMUeF2AzTtSPExBjbXo1 user
/root/change-user-passwd.sh: 8: /root/change-user-passwd.sh: usermod: not found

Which makes the issue clear to me now, thanks @l0b0!

marked as duplicate by muru, Community Oct 23 '18 at 8:50

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • @l0b0 It's neither. I just needed a temporary password that works only once to login, once the login session ended the password should not work anymore. (Therefore, the every X minutes isn't exactly a requirement, I'd still like to know why this isn't working at all in a script while it does when run manually.) – confetti Oct 23 '18 at 7:12
  • @l0b0 But that wouldn't answer my actual question, that's why I didn't include the information. I'm sure there's another way to create on-time passwords (passwd itself has the --expire option), but I'd really like to know what's causing this particular issue of it working when run manually, but not inside of a script. – confetti Oct 23 '18 at 7:15
  • How are you checking that the password is not changed? Do you have more code to report what the password was changed to in your script? – l0b0 Oct 23 '18 at 7:16
  • @l0b0 I'm trying to physically log into the machine (lightdm login screen) with the new password, it doesn't work. Logging in via ssh as the user with the changed password doesn't work either. If I manually run those commands, it works. If I run it in the script, nothing's changed (the old password still works). – confetti Oct 23 '18 at 7:17
  • @l0b0 I'm sorry, I've just edited my question. Turns out if I run the script manually it does work. It just doesn't work when cron starts it. The file does get updated, but the password never changed. Do you still want me to do your suggestion? – confetti Oct 23 '18 at 7:33

According to: https://stackoverflow.com/questions/714915/using-the-passwd-command-from-within-a-shell-script

echo "$npasswd" | passwd --stdin user


echo "user:$npasswd" | chpasswd

will be good for you.

  • My passwd doesn't have the --stdin option. The pipe of the second command seems weird to me. Why pipe echo to the command when $npasswd is provided to chpasswd directly? I've tried it on the command-line: chpasswd: line 1: missing new password – confetti Oct 23 '18 at 7:23
  • I think I got it from man chpasswd. I have proposed an edit to your answer of the correct usage for chpasswd, may you please accept it? It works manually, I'll try it in a script now. – confetti Oct 23 '18 at 7:25
  • Thanks for correction! And I wish helps your problem solve. – minish Oct 23 '18 at 7:55

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