I have written a script for adding and setting password for 100+ users. Eg: for setting password of the user I want to set in "user+123" format for all the users I.e user will change according to the username in password. How can I write a script for this.

for i in `more users.txt`
useradd $i;
echo "User $i added successfully"
passwd $("$i"123)
echo "Password added successfully for user"
  • 1
    Please do not use photos were text will do. It is not that difficult typing in that script. – Rui F Ribeiro Jan 23 '18 at 12:37
  • 2
    I would look at changing the passwords for users in batch mode using chpasswd. – Raman Sailopal Jan 23 '18 at 13:12
for i in $( cat users.txt ); do
    useradd $i
    echo "user $i added successfully!"
    echo $i:$i"123" | chpasswd
    echo "Password for user $i changed successfully"

This little script should be what you are looking for. It adds the user first, then proceeds to change the password. Avoid using backticks, as they are deprecated, $() is a good alternative.

| improve this answer | |

The other answers provide technical answers for how to do this. But please don't do this.

As soon as a threat actor sees the pattern, they could log in as any other user. (And even if you force a password change on first login, someone could lock out all of the other users.)

Instead, send each user a random password, or a token via email to be used to set a new password.

| improve this answer | |

In below script i am creating 5 users like user1,user2,user3,user4,user5 and setting the pasword as "username+123" for each user Tested and it worked successfully.

You can change number of users as per requirment

for i in user{1..5}; do useradd $i; passwd -d $i; echo $i"123" | passwd  $i --stdin; done

If you have a text file in which you have specific usernames, you can use

for i in $(cat users.txt); do useradd $i; passwd -d $i; echo $i"123" | passwd  $i --stdin; done
| improve this answer | |
  • OP wants the users to have unique names, and the password should be $user123, your answer does not accommodate that. – Hunter.S.Thompson Jan 23 '18 at 16:03
  • @Hunter.S.Thompson Edited the answer and changed as per the requirement and tested too . its working fine – Praveen Kumar BS Jan 23 '18 at 16:49
  • I edited your answer to properly show what the OP needs to do when the usernames are in a textfile. – Hunter.S.Thompson Jan 23 '18 at 16:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.