Level Verified

Linux Unlock Device Script

Instantly restore normal user and root account access on Linux devices previously locked, preserving remote management through Level.

Import into Level

Problem overview

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.

Bash 100s timeout Runs as Local system Linux
#!/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

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.

Use cases

  • 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

  • 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

Frequently asked questions.

Will this remove the device from Level management?

No. The script only unlocks user accounts; it does not affect Level’s remote management.

How can I confirm if a user has been unlocked?

After running the script, attempt a local login or SSH session with valid credentials to verify successful reactivation.

What if the script fails to unlock certain accounts?

Check the logs and confirm there are no conflicting system restrictions. Additionally, ensure the account names exist and haven’t been removed or altered since lockdown.

Do I need to run this manually each time?

You can configure an Automation in Level to run this script automatically on a predefined schedule or on-demand, according to your workflow.

Ready when you are.

No credit card. No sales call. Just sign up and start managing.