1

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
Monika
  • 51

3 Answers3

8
#!/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.

Alexander
  • 9,850
3

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.

1

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