Create Users And Change Passwords With A Bash Script

Want to support HowtoForge? Become a subscriber!
 
Submitted by fakrul (Contact Author) (Forums) on Fri, 2007-03-16 16:33. :: Linux

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:

nurealam
nayeem
mrahman
farid
rubi
sankar

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

#!/bin/sh
for i in `more userlist.txt `
do
echo $i
adduser $i
done

Save the file and exit.

chmod 755 userlist.txt

Now run the file:

./userlist.txt

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:

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

Run the file. All the passwords are changed.


Please do not use the comment function to ask for help! If you need help, please use our forum.
Comments will be published after administrator approval.
Submitted by John (not registered) on Tue, 2013-07-30 20:34.
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.
Submitted by Sander Smit (not registered) on Sun, 2011-01-09 21:42.

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?

Submitted by Anonymous (not registered) on Mon, 2010-05-10 07:43.
Thanks for the script.. Its working..
Submitted by Ankur (not registered) on Wed, 2009-11-25 22:07.
There is some problem in the password script, I have checked with the below script it's working fine.

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

Regards,
Ankur

Submitted by Anonymous (not registered) on Thu, 2009-06-04 14:08.
I try this in Ubuntu and seems not working :(
Submitted by amcorona (registered user) on Tue, 2007-08-28 15:59.

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

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

 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:
 

Submitted by Jim (not registered) on Fri, 2011-01-21 15:38.

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
/usr/bin/chage


 # which passwd
/usr/bin/passwd 

 

Submitted by Anonymous (not registered) on Thu, 2009-05-21 11:40.

 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:

./userlist.txt

 ;)

 

Submitted by Anonymous (not registered) on Wed, 2014-06-11 12:01.
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

 

 

Submitted by ravindra (not registered) on Fri, 2010-06-18 08:55.
This script works. Thanks