I have build a Linux From Scratch live CD and wrote an installer script for it. There is a step in that script in which I take an username and password from the user and create an user. The installed filesystem is mounted at /mnt The username is stored in $USER and password in $PASS

To create the user, I use

chroot /mnt useradd $USER -s /bin/bash -m

To change the password, I use

chroot /mnt echo "$USER:$PASS"|chpasswd

But when I boot into the installed filesystem, I notice that the user has been created, but no password has been assigned. Where am I doing wrong?


Try this. Avoids the situation where chroot /mnt echo "$USER:$PASS"|chpasswd is failing due to the echo running within the chroot and the chpasswd running outside the chroot.

echo "$USER:$PASS" >/mnt/foo
chroot /mnt 'chpasswd </foo'
rm /mnt/foo
  • 1
    also worth mentioning is that if $USER exists on the host system outside of the chroot, then that user's password will have been changed by running the chpasswd outside of the chroot. Oh, and, you don't need a temporary file. chroot /mnt sh -c "echo '$USER:$PASS' | chpasswd" will work. Note well the use of double-quotes around the entire pipeline executed by sh -c, and the single-quotes around the variables, this allows the vars to be interpolated. – cas Mar 27 '16 at 8:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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