Problem overview.
The challenge this resource is designed to solve.
Once a device has been locked due to security concerns, it’s essential to restore full access once the threat has passed or the device is recovered. This script systematically unlocks all user accounts—including the root user—returning the system to regular operational status without hindering remote management from Level.
About this resource.
What it does and how it fits into your workflow.
The script scans the system for valid local user accounts and reverts each from a locked status to an active one. Additionally, it resets the root account, ensuring administrators can regain superuser privileges without obstruction. Throughout this process, your device remains connected to Level, so you maintain remote visibility and control.
#!/bin/bash
# This resource is provided as a convenience for Level users. We cannot
# guarantee it will work in all environments. Please test before deploying
# to your production environment. We welcome contributions to our community
# library
# Level Library
# https://level.io/library/script-linux-unlock-device
# Initialize script-scoped error flag
script_errors=false
echo "Re-enabling all user accounts (SSH & local access)..."
for user in $(awk -F: '{if ($3 >= 1000 && $3 < 65534) print $1}' /etc/passwd); do
passwd -u "$user" &> /dev/null
usermod -U -e "" "$user" &> /dev/null
if [[ $? -ne 0 ]]; then
echo "ALERT: Failed to unlock user account $user."
script_errors=true
fi
done
echo "Unlocking root account..."
passwd -u root &> /dev/null
usermod -U -e "" root &> /dev/null
if [[ $? -ne 0 ]]; then
echo "ALERT: Failed to unlock root account."
script_errors=true
fi
if [[ "$script_errors" == true ]]; then
echo "ALERT: Errors occurred during execution. Exiting with code 1."
exit 1
else
echo "All user accounts have been successfully unlocked."
fi
Use cases.
Common ways to put this resource to work.
- Re-enabling accounts post-emergency lockdown
- Restoring device functionality after security events or audits
- Allowing normal operations to resume following a temporary maintenance lockout
- Quickly unlocking user access after a threat has been mitigated
Recommendations.
Practical guidance for a reliable rollout.
- Test on non-production systems to confirm compatibility and safe operation
- Pair with the Linux Lock Device Script to handle lock-and-unlock cycles seamlessly
- Configure a Level Automation to schedule unlocking tasks if needed routinely
- Verify the accounts have successfully unlocked by testing normal SSH or local logins