Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Do I Create A User & Set Password Without User Interaction?

I have been recently working on a project named: arch loop, which is an automated installer for Arch Linux. I have seen a few installers and scripts to make Arch installation easier, but I am someone who installs Arch Linux, more than three times a day, so following the Arch-way takes a long time and constantly requires user interaction.

The Problem:

The password is, the information about non-root user is to be created is taken before itself, and when the appropriate time comes, we will be using the following command:

arch-chroot /mnt useradd -m -g users -G wheel -s /usr/bin/bash archuser
arch-chroot /mnt bash -c "echo -e 'password\npassword\n' | passwd
arch-chroot /mnt bash -c "echo -e 'rootpassword\nrootpassword\n' | passwd root

to send the password to passwd binary in the chroot system. But I don't know why it does not work. When the password is being verified by the sudo command after the installation is finished. The password seems to be perfectly working. But when tried to log in with the non-root user from tty, the password seems to be incorrect.

Things I Have Already Tried:

  1. Manually encrypting the provided password with the below code and passing it to the useradd binary with -p option:
perl -e 'print crypt("password", "\$6\$SALTsalt\$") . "\n"'"

Please guide me on how to set a user's provided password at a later time, without requiring any user interaction.

Thank You :)

like image 263
Vasanth Srivatsa Avatar asked May 31 '26 22:05

Vasanth Srivatsa


1 Answers

There exists the chpasswd command. It is just there only to make passwd available in batch scripts. Just do:

echo "root:rootpassword" | arch-chroot /mnt chpasswd

or maybe better, without the need for mount -o bind the sys proc and dev directories:

echo "root:rootpassword" | chpasswd -R /mnt

@subjective: Sorry for the opinion, the project looks ok, however much more work is to be done. I guess the aim is to bring Archlinux closer to "normal" users. However, I don't like the choose of python for the project. Going with plain POSIX sh would make this available for all. I don't like hardcoded partitions, mlocate (do you really use mlocate?), multiple arch-chroot calls where you could just do a single big script, not handling os.system error codes (!), multiple pacman calls without even -Sy (!) (pacman can fail if upstream updates the repos), and few more things I don't like. Except for that, nice python abstraction and cool aim. I remember the old archlinux installation scripts few (or more) years ago, they were nice, however I think used commands themselves anyway. Good luck.

like image 109
KamilCuk Avatar answered Jun 03 '26 15:06

KamilCuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!