How to automatically add user account AND password with a Bash script?


I need to have the ability to create user accounts on my Linux (Fedora 10) and automatically assign a password via a bash script(or otherwise, if need be).

It's easy to create the user via Bash e.g.:

[whoever@server ]#  /usr/sbin/useradd newuser

Is it possible to assign a password in Bash, something functionally similar to this, but automatically:

[whoever@server ]# passwd newuser
Changing password for user testpass.
New UNIX password:
Retype new UNIX password: 
passwd: all authentication tokens updated successfully.
[whoever@server ]#
9/15/2014 3:19:02 PM

Accepted Answer

You can run the passwd command and send it piped input. So, do something like:

echo thePassword | passwd theUsername --stdin
1/27/2010 11:30:23 PM

You could also use chpasswd:

echo username:new_password | chpasswd

so, you change password for user username to new_password.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow