Scripting the passwd command in Linux and Unix

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.

Liked it? Take a second to support Geekster on Patreon!
Tagged with: , , , , , , ,
Posted in The Linux and Unix Articles!

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Support LinuxG via Patreon
Support LinuxG.net on Patreon!
Subscribe

  

Subscribe to get the latest Linux news and how to guides directly on your e-mail!