Adsense Leaderboard Ad

10.23.2012

Script to Change all Users Passwords on Red Hat / Centos

Question sent in by Boyd:

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, see here. 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:

test1
test2
test3
test4

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\nlinuxpassword" | passwd $i
done

OUTPUT:
Changing password for user test1.
New password: Retype new password: passwd: all authentication tokens updated successfully.
Changing password for user test2.
New password: Retype new password: passwd: all authentication tokens updated successfully.
Changing password for user test3.
New password: Retype new password: passwd: all authentication tokens updated successfully.
Changing password for user test4.
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\nlinuxpassword" | passwd $i
usermod -L $i
chage -d 0 $i
usermod -U $i
done

The above will force the users to change their password next time they log in.

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

8 comments:

  1. Can you provide the script for hp-ux os since it asks to " pick the passwoed option "

    ReplyDelete
  2. Sorry Pranay, I never used hp-ux, maybe one of our readers can help you.

    ReplyDelete
  3. Hello Pranay:

    If that prompt for password option is immediately before the actual password prompt, I imagine you can simply add the response to the string above making it:

    echo -e "password_option_answer\nnewpassword\nnewpassword" | passwd $i

    ReplyDelete
  4. I am getting this error.

    Changing password for user tsieab.
    New UNIX password: Retype new UNIX password: Sorry, passwords do not match
    New UNIX password: BAD PASSWORD: it is WAY too short
    Retype new UNIX password: No password supplied
    No password supplied
    No password supplied
    passwd: Authentication token manipulation error



    My script is like this.
    #!/bin/bash
    for i in `cat user.sh`; do
    echo -e "12qwaszx\n12qwaszx" | passwd $i
    done

    ReplyDelete
  5. @Kamal...

    I am not sure what users.sh is, but in order to change a users password you need to echo it twice... Try this...

    echo -e '12qwaszx'"\n"'12qwaszx' | passwd tsieab

    I would be able to offer more help with your complete script, but I am not sure what you are doing with:

    "for i in `cat user.sh`; do"

    ReplyDelete
  6. Hello,

    This was of great help, but I want something like, script should read username from file1 and password from file2 respectively.
    How do we do this?
    Can you help?

    ReplyDelete
  7. Hello
    This is great work very well done. I have a problem too it is requested that you inform me how can do this.
    Let say i have folders named
    Folder1/apache/users/admin
    Folder2/apache/users/admin
    Folder3/apache/users/admin
    Folder4/apache/users/admin

    my passwords are stored in admin file in differ folder path is same
    i want update the password how i can do this in linux

    ReplyDelete
    Replies
    1. I am sorry but I do not understand your request. Maybe you can use the contact form here: http://www.putorius.net/p/get-answers-to-your-linux-questions.html

      Please include more information, maybe I can help.

      Delete