How to create user and password in one script for 100+ users

43,687

Solution 1

#!/bin/bash
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"
done

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.

Solution 2

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.

Solution 3

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
Share:
43,687

Related videos on Youtube

Monika
Author by

Monika

Updated on September 18, 2022

Comments

  • Monika
    Monika almost 2 years

    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.

    #!/bin/bash
    for i in `more users.txt`
    do
    useradd $i;
    echo "User $i added successfully"
    passwd $("$i"123)
    echo "Password added successfully for user"
    done
    
    • Rui F Ribeiro
      Rui F Ribeiro over 6 years
      Please do not use photos were text will do. It is not that difficult typing in that script.
    • Raman Sailopal
      Raman Sailopal over 6 years
      I would look at changing the passwords for users in batch mode using chpasswd.
  • Hunter.S.Thompson
    Hunter.S.Thompson over 6 years
    OP wants the users to have unique names, and the password should be $user123, your answer does not accommodate that.
  • Praveen Kumar BS
    Praveen Kumar BS over 6 years
    @Hunter.S.Thompson Edited the answer and changed as per the requirement and tested too . its working fine
  • Hunter.S.Thompson
    Hunter.S.Thompson over 6 years
    I edited your answer to properly show what the OP needs to do when the usernames are in a textfile.
  • Hunter.S.Thompson
    Hunter.S.Thompson over 6 years
    Please give a reason for the downvote, maybe I can learn something, something I did wrong?
  • Natalie Adams
    Natalie Adams almost 6 years
    Where do you see that back ticks are deprecated? It says nothing about that in the official manual. gnu.org/software/bash/manual/bashref.html#Command-Substituti‌​on
  • Hunter.S.Thompson
    Hunter.S.Thompson almost 6 years
  • Hunter.S.Thompson
    Hunter.S.Thompson almost 6 years
  • somethingSomething
    somethingSomething almost 6 years
    @Hunter.S.Thompson Works like a charm , thanks alot
  • mckenzm
    mckenzm over 5 years
    ksh is not bash, and as an AIX developer for many years, there is a lot of syntax out there that is "deprecated", or undocumented (IBM internal use only stuff). Sitewide on SE there is one mention of lquerypv for instance. Bash on AIX supports backticks, but you would need to have it installed.