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.
#!/bin/bash # 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 done
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
+ : + 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!