문제

* QUICK SOLUTION * For those of you visiting this page based on the title solely and not wanting to read through everything below, or thinking everything below doesn't apply to your situation, maybe this will help... If all you are looking to do is change a users password on boot and are using Ubuntu 12.04 or similar, here is all you have to do. Add a script to start on boot containing the following:

printf "New Password\nRepeat Password\n" | passwd user

Keep in mind, this must be run as root, otherwise you will need to provide the original password like so:

printf "Original Password\nNew Password\nRepeat Password\n" | passwd user

* START ORIGINAL QUESTION *

I have a first boot script that sets up a VM by doing some configuration and file copies from a mounted iso. Basically the following happens:

  1. VM boots for the first time.
  2. /etc/rc.local is used to mount a CD ISO to /media/cdrom and execute /media/cdrom/boot.sh
  3. The boot.sh file does some basic configuration, copies some files from CD to the VM and should update the users password, using the current password.

This part of the script fails. The password is not updating. I have tried the following:

VAR="1234test6789"
echo -e "DEFAULT\n$VAR\n$VAR" | passwd user

Basically the default VM is setup with a user (for example jack) with a default password (DEFAULT) The script above, using the default password updates to the new password stored in VAR. The script works by itself when logged in, but I cant get it to do the same on boot. I'm sure there is some sort of system policy or something that prevents this. If so, I need some sort of work around. This VM is being mass deployed and is packaged automatically and configured with a custom user password that is passed from the CD ISO.

Please help. Thank you!

* UPDATE *

Oh, and I'm using Ubuntu 12.04

* UPDATE *

I tried your suggestion. The following files directly in the rc.local ie the password does not update. The script is running however. I tested by adding the touch line.

touch /home/jack/test
VAR="1234test5678"
printf "P@ssw0rd\n$VAR\n$VAR" | passwd jack

P@ssw0rd is the example default VM password.
Jack is the example username.

* UPDATE *

Ok, we think the issue may be tied to rc.local. So rc.local is called really early on before run levels and may be causing the issue.

* UPDATE *

Well, potentially good news. The password seems to be updating now, but its updating to something other than what I set in $VAR. I think it might be adding something to it. This is ofcourse just a guess. Everytime I run the test, immediately after the script runs at boot I can no longer login with the username it was trying to update. I know that's not a lot of information to go on, but it's all I've got at the moment. Any ideas what or why its appending something else to the password?

* SOLUTION *

So there were several small problems as to why I could not get the suggestion below working. I won't outline them here as they are irrelevant. The ultimate solution was from Graeme tied in with some other features of my script which I will share below.

The default VM boots
rc.local does the following:

if [ -f /etc/program/tmp ]; then
  mount -t iso9660 -o ro /dev/cdrom /media/cdrom
  cd /media/cdrom
  ./boot.sh
fi

(The tmp file is there just to prevent the first boot script from running more than once. After boot.sh runs one, it removes that tmp file.)

boot.sh on the CDROM runs (with root privileges) boot.sh copies files from the CDROM to /etc/program boot.sh also updates the users password with the following:

VAR="DEFAULT"
cp config "/etc/program/config"
printf "$VAR\n$VAR\n" | passwd user
rm -rf /etc/program/tmp

(VAR is changed by another part of the server that is connected to our OVA deployment solution. Basically the user gets a customized, well random password for their VM so similar users cannot access each others VMs)

There is still some testing to be done, but I am reasonably satisfied that this issue is resolved. 95%

도움이 되었습니까?

해결책

Edit - updated for not entering the original password

The sh version of echo does not have the -e option, unlike bash. Switch echo for printf. Also the rc.local script will have root privileges, so it won't prompt for the original password. Using that will cause the command to fail since 'DEFAULT' will be taken as the new password and the confirm will fail. This should work:

VAR="1234test6789"
printf "$VAR\n$VAR\n" | passwd user

Ubuntu uses dash at boot time, which is a drop in replacement for sh and is much more lightweight that bash. echo -e is a common bashism which doesn't work elsewhere.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top