Score:1

Scripted Password Change Does Not Survive Reboot Unless One Login Occurs - Debian 11

mx flag

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:

Set Root Password Automatically

Score:0
mx flag

I have solved this problem. What is needed is a simulated login after setting the password. Nothing else is needed but the following:

#!/bin/bash
## Change Root Password
echo "root:$(cat /dev/urandom | tr -dc '[:alnum:]' | fold -w ${1:-8} | head -n 1)" | tee >( chpasswd )
su - root

This answer is based on the following:

https://unix.stackexchange.com/questions/164890/simulate-initial-login-of-user-without-using-sudo

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.