As you know the passwd command is interactive, so it cannot be used in scripts in it’s basic form.
As you know, when you type passwd, you are asked to type the password twice.
$ sudo passwd mike
Enter new UNIX password:
Retype new UNIX password:
The following two tricks are non interactive:
Trick one, as root:
$ sudo su
# echo "user:password_here" | chpasswd
Trick two, as root:
$ sudo su
# echo -e "password_here\npassword_here" | passwd user
I usually the chpasswd trick when creating scripts that need to add users. Also, the second command works just fine.
Leave a Reply