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!
Leave a Reply Cancel reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
9 Comments
Join Our Newsletter
Categories
- Bash Scripting (17)
- Basic Commands (50)
- Featured (7)
- Just for Fun (5)
- Linux Quick Tips (98)
- Linux Tutorials (65)
- Miscellaneous (15)
- Network Tools (6)
- Reviews (2)
- Security (32)
Can you provide the script for hp-ux os since it asks to " pick the passwoed option "
Sorry Pranay, I never used hp-ux, maybe one of our readers can help you.
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_answernnewpasswordnnewpassword" | passwd $i
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 "12qwaszxn12qwaszx" | passwd $i
done
@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"
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?
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
I am sorry but I do not understand your request. Maybe you can use the contact form here: https://www.putorius.net/contact-us
Please include more information, maybe I can help.
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