Q: I have a text file containing user names on Redhat Linux CentOS. I would like to change their password all at once in a script. Please provide me with a script to do that.

A: This is more of a request than a question, but since I have nothing else to write about here we go.

I actually already did a little post on how to change a users password with one line. All we need to do is loop through you text file and run the command.

Let’s say you have a text file called /var/tmp/users that contains all the names of users on a system, one per line like this:

user1
user2
user3
user4

We can use a simple for loop to read those names one at a time and pass it to the passwd function like so:

#!/bin/bash
for i in `cat /var/tmp/users`; do
echo -e "'linuxpassword'\n'linuxpassword'" | passwd $i
done

Example output:

Changing password for user user1.
New password: Retype new password: passwd: all authentication tokens updated successfully.
Changing password for user user2.
New password: Retype new password: passwd: all authentication tokens updated successfully.
Changing password for user user3.
New password: Retype new password: passwd: all authentication tokens updated successfully.
Changing password for user user4.
New password: Retype new password: passwd: all authentication tokens updated successfully.

As you can see we changed all the users password that are listed in that file to “linuxpassword”.

I would not recommend this unless you are going to at least force the users to change their password the next time they log in. To accomplish this we can change the script to:

#!/bin/bash
for i in `cat /var/tmp/users`; do
echo -e "'linuxpassword'\n'linuxpassword'" | passwd $i
usermod -L $i
chage -d 0 $i
usermod -U $i
done

I hope this answers your question.  If not, feel free to post in the comments!