Passwords in bash scripts

With the recent updates and or changes in Linux, I found my old method of using yes "${Password}" | passwd "${Username}"  in chroot for assigning passwords in bash scripts no longer works. Seems this is possibly by design for security concerns from what I can tell.

Through some searching, reading and testing, I came up with a nice passwd replacement of chpasswd. chpasswd is part of the shadow package, which is an Arch base package. The man page on chpasswd reveals an -R option. This seems a perfect fit for my use case.

-R, --root CHROOT_DIR
           Apply changes in the CHROOT_DIR directory and use the configuration files from the CHROOT_DIR directory.


In my latest install script, I used chpasswd as a replacement for passwd:


URN=jeff    ## Username      ##
URP=test    ## User Password ##
RTP=test    ## Root Password ##
and

    arch-chroot /mnt useradd -m -g users -G wheel -s /bin/bash "${URN}"
    echo "${URN}:${URP}" | chpasswd -R /mnt
    echo "root:${RTP}" | chpasswd -R /mnt

For the entire script and other bash scripting efforts, see my latest github alias:  https://github.com/Cody-Learner/saist/blob/master/inst

Note: The passwd --stdin option has been removed from current shadow 4.6-1 available in Arch.
Comments