
- by x32x01 ||
Managing multiple user accounts on Linux can get messy - especially when you need to lock or unlock many at once. Thankfully, with a few simple shell scripts, you can automate this entire process! 
Let’s break it down step by step
Understanding the Basics
While both commands can handle one user at a time, you can write small shell scripts to automate them for multiple users simultaneously.
Preparing the User List
Create a text file named
Here’s a simple shell script to lock multiple accounts:
Make it executable:
Then run it:
Output Example:
Want to confirm if users are locked? Use this script:
Make it executable:
Run it:
If you see
To unlock all users again, use this script:
Make it executable:
Run it:
Output Example:
Now check again using the status script - if you see 
With just a few shell scripts, you can efficiently lock, unlock, and verify multiple Linux accounts without manually handling each one.
Pro Tip: Keep your scripts secure and executable only by authorized admins.
Got any questions or suggestions? Drop a comment below - let's geek out together! 


Let’s break it down step by step

Understanding the Basics
What Is the passwd Command?
Thepasswd
command is one of the most essential tools for Linux admins. It updates user authentication tokens stored in the /etc/shadow
file using Linux-PAM and Libuser API. It’s mainly used for managing user passwords, including locking or unlocking them.What Is the usermod Command?
The usermod command lets administrators modify user account settings, such as adding users to specific groups or updating account details.While both commands can handle one user at a time, you can write small shell scripts to automate them for multiple users simultaneously.
Preparing the User List
Create a text file named user-lists.txt
that contains all the usernames you want to lock or unlock - one per line: Bash:
$ cat user-lists.txt
u1
u2
u3
u4
u5
Lock Multiple Users in Linux
Here’s a simple shell script to lock multiple accounts: Bash:
# user-lock.sh
#!/bin/bash
for user in `cat user-lists.txt`
do
passwd -l $user
done
Bash:
chmod +x user-lock.sh
Then run it:
Bash:
sh user-lock.sh

Code:
Locking password for user u1.
passwd: Success
Locking password for user u2.
passwd: Success
...
Check Locked User Status
Want to confirm if users are locked? Use this script: Bash:
# user-lock-status.sh
#!/bin/bash
for user in `cat user-lists.txt`
do
passwd -S $user
done
Bash:
chmod +x user-lock-status.sh
Run it:
Bash:
sh user-lock-status.sh
If you see
LK
next to the username, it means the account is locked 
Code:
u1 LK 2025-10-20 0 99999 7 -1 (Password locked.)
Unlock Multiple Users in Linux
To unlock all users again, use this script:
Bash:
# user-unlock.sh
#!/bin/bash
for user in `cat user-lists.txt`
do
passwd -u $user
done
Bash:
chmod +x user-unlock.sh
Run it:
Bash:
sh user-unlock.sh

Code:
Unlocking password for user u1.
passwd: Success
...
PS
, it means the account is unlocked 
Code:
u1 PS 2025-10-20 0 99999 7 -1 (Password set, SHA512 crypt.)
Conclusion
With just a few shell scripts, you can efficiently lock, unlock, and verify multiple Linux accounts without manually handling each one.



Last edited: