I am trying to automatically set the root password to random characters via bash script. The script is part of a Vagrant shell provisioner. If the script runs the password change only, I am able to login to the instance just fine. I can then reboot the instance. After the reboot, I am still able to login using that same password as root.
However, if I include a reboot at the very end of the shell provisioner script, after the reboot, login fails with that password as root.
What is happening when there is a login before the reboot that uses the new password? What else can I include in the script so that the password set is able to be used after the reboot?
Here is the Vagrant install script install.sh
. It generates an eight character password, prints it to stdout so it can be read from the Vagrant output after vagrant up
, and redirects it to chpasswd
. There is no problem with this command: it works just fine if I login once before the reboot. It just gets wiped by something if the reboot is part of the script like below.
#!/bin/bash
echo "root:$(cat /dev/urandom | tr -dc '[:alnum:]' | fold -w ${1:-8} | head -n 1)" | tee >( chpasswd )
reboot
Here is the line in the Vagrantfile
for the shell provisioner:
config.vm.provision 'shell', path: 'install.sh'
Here are two diagrams:
I have solved this problem. What is needed is a simulated login after setting the password. Nothing else is needed but the following:
This answer is based on the following:
https://unix.stackexchange.com/questions/164890/simulate-initial-login-of-user-without-using-sudo