Create Users And Change Passwords With A Bash Script

These two scripts are very important for the system admin who regularly works with mail servers and somehow forgets to backup his system username and password! Let’s say somehow we lost the usernames and passwords of the mail server. In this case the admin has to manually create all the users and then change the passwords for all the users. Tedious job. Let’s make our life easier.

First create a file which contains all the user name. Something like this:


Save the file as userlist.txt. Now create the following bash file:

for i in `more userlist.txt `
echo $i
adduser $i

Save the file and exit.

chmod 755 userlist.txt

Now run the file:


This will add all the users to the system. Now we have to change the passwords. Let's say we want username123 as password. So for user nayeem the password will be nayeem123, rubi123 for user rubi and so on.

Create another bash file as follows:

for i in `more userlist.txt `
echo $i
echo $i"123" | passwd –-stdin "$i"
echo; echo "User $username’s password changed!"

Run the file. All the passwords are changed.

Share this page:

Suggested articles

26 Comment(s)

Add comment



Just something that should be considered.  Force all your users to change their passwords because the temporary password is a security risk

for i in `more userlist.txt `
echo $i
echo $i | change -d 0 "$i"
echo; echo "User $i will be forced to change password on next login!"

 I then log as that user and see this

WARNING: Your password has expired.
You must change your password now and login again!
Changing password for user amcorona.
Changing password for amcorona
(current) UNIX password:

By: Anonymous

 You might want to check what you have written in the article as you would not want to save your bash script and then try and execute a file containing usernames.

chmod 755 userlist.txt

Now run the file:




By: ravindra

This script works. Thanks

By: Anonymous

want to create multiple users with in /home with dir same as username and set permisson for every user to there respective dir.

want to set password for all user same as i need how to do it for multiple users in this script



By: Jim

echo $i | change -d 0 "$i"
 should be

echo $i | chage -d 0 "$i"

And if you are not running these scripts as root you will need to put sudo in the commands with explicit paths. Of course with sudo it will prompt you for a password unless you are in a sudoers group with NOPASSWD:

On SuSE:

# which chage

 # which passwd


By: Anonymous

I try this in Ubuntu and seems not working :(

By: Ankur

There is some problem in the password script, I have checked with the below script it's working fine.

for i in `more userlist.txt `
echo $i
echo $i"123" | passwd "$i" --stdin
echo; echo "User $username's password changed!"


By: Anonymous

Thanks for the script.. Its working..

By: Sander Smit

When I follow these steps ik get a message:

./userlist.txt line1: NKokmeijer: command not found

./userlist.txt line2: FLuursen: command not found

./userlist.txt line3: SNiezen: command not found

./userlist.txt line4: GBielen: command not found

What is going wrong?

By: Jeet

you need to make few changes like inside the bash file theire is adduser line, replace it with useradd  and run script as "sh" in case you are working with redhat linux.



By: John

Excellent! I had used a script to create multiple users, but I forgot to include passwords in the script. Your script helped me change their passwords on multiple servers. Thanks for posting this.

By: Gopi

Give me steps how to created the commentline for the different users in this same script?

By: Eric Dalton

so all this info is good to know, but my question is what syntax do i use to create a script that prints all the data from

/etc/passwd while also having it sorted. To aid with user managment being able to look over a complet list of current users while creating new users. Any and All assistance is greatly appriciated.

By: jon

i not get clear what name to put for the bash file


By: Tùng LS

I recommend you to create a file with username & password. Then the script will read the username and password from file and create user:

The file userlist.txt format:

username1 [email protected]

username2 [email protected]


The shell script:


while read u1 p1


   echo Username: $u1 Password: $p1 created

   adduser  $u1

   echo $p1 | passwd --stdin "$u1"

 echo Username: $u1 Password: $p1 created

done < userlist.txt

last, run the script as root:



chmode +x




By: Pich Chhoun

When i run this script it alert "command in userlist.txt not found"

By: afif


I need to change database user passwd in mysql.

like as: i have db user name is openexchange and db name is oxdatabase_5, this database i have many user and passwd. i need to a bash script which change user pass from oxdatabase_5 from http client interface.

i am using mail server for change their passwd. 

By: Derek

If I have a text document that is instead formatted like this:





How would I implement it in this code to adjust for that? I'm trying to learn and I'm not too familiar with what each command does!

By: Lau

This doesn't work when i run it, it will create new user but does not change the password

this is the error msg :-

passwd: Only one user name may be specified.

By: Monish

seems you need to modify below parameters. It should look like echo $i"123" | passwd "$i" --stdin

echo $i"123" | passwd –-stdin "$i"

By: khadeer



after executing the above script as following your steps 


login as normal user, its not asking password change

login as: noor

[email protected]'s password:

Access denied

[email protected]'s password:

By: me

passwd does not have '--stdin' option in all distributions (e.g. SLES 12 does not).

Therefore, check "man chpasswd"

By: Mark

I see that you could make just a single script but make all parts in order so everything works, all I am trying to say that you could make one single script that does all of that.

By: 19krikma

Hello I have worked on this more than 1 day and figured that users that run UNIX password will not work for them so I have came up with the solution

useradd -m -s /bin/bash-4.4 "$username"

echo "$username:password" | chpasswd         

echo $added | chage -d0 "$username"

This will create a user, give him password right a way and then make him change password on next log in.

By: jagadeesh

Plesae share me code for connect a remote linux servers with a specific user without ssh key to pass passwords over the bash shell


By: Mouzam


I got this below request to create a single script to perform all the below activities by taking an input from user.


User Add - > need to have ticket number and Email ID of user who requested.

User Disable -> Need to disable the user for particular days before deleting it.

User Del -> Need to archive the user home directory before deleting it.


Can you please keep all above script in one and it should request user for option like 1. User del 2. User add. 3. User disable option.


Entire script output should be triggered to one file which should be your log file of entire script activity.