Lock & Unlock Multiple Users in Linux

x32x01
  • 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 🧠


What Is the passwd Command?​

The passwd 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



1️⃣ 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
Make it executable:
Bash:
chmod +x user-lock.sh

Then run it:
Bash:
sh user-lock.sh

✅ Output Example:
Code:
Locking password for user u1. 
passwd: Success 
Locking password for user u2. 
passwd: Success 
...



2️⃣ 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
Make it executable:
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.)



3️⃣ 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
Make it executable:
Bash:
chmod +x user-unlock.sh

Run it:
Bash:
sh user-unlock.sh

✅ Output Example:
Code:
Unlocking password for user u1. 
passwd: Success 
...
Now check again using the status script - if you see 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.

💡 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! 😎🐧
 
Last edited:
Related Threads
x32x01
Replies
0
Views
768
x32x01
x32x01
x32x01
Replies
0
Views
936
x32x01
x32x01
x32x01
Replies
0
Views
786
x32x01
x32x01
x32x01
Replies
0
Views
914
x32x01
x32x01
x32x01
  • x32x01
Replies
0
Views
820
x32x01
x32x01
x32x01
Replies
0
Views
771
x32x01
x32x01
x32x01
Replies
0
Views
710
x32x01
x32x01
x32x01
Replies
0
Views
814
x32x01
x32x01
x32x01
  • x32x01
Replies
0
Views
2K
x32x01
x32x01
x32x01
  • x32x01
Replies
0
Views
826
x32x01
x32x01
Register & Login Faster
Forgot your password?
Forum Statistics
Threads
629
Messages
634
Members
64
Latest Member
alialguelmi
Back
Top