Back to Resources
Level
Script
Security
Whether a Linux device is compromised, lost, or subject to strict compliance standards, instantly revoking user access is paramount. This script securely locks all local and SSH-enabled accounts, ensuring no one can log in or remain logged in, all without losing remote management capabilities through Level.
This script finds all currently logged-in users and terminates their sessions, effectively booting them off the system. It then fully disables each account, including root, by locking their passwords and setting their expiration to an immediate end date. This dual action eliminates the chance for re-logins or ongoing unauthorized use, granting you peace of mind that the system remains inaccessible except through Level’s remote management.
1#!/bin/bash
2
3# This resource is provided as a convenience for Level users. We cannot
4# guarantee it will work in all environments. Please test before deploying
5# to your production environment. We welcome contributions to our community
6# library
7
8# Level Library
9# https://level.io/library/script-linux-lock-device
10
11# Initialize script-scoped error flag
12script_errors=false
13
14echo "Killing all user sessions..."
15for user in $(who | awk '{print $1}' | sort | uniq); do
16 pkill -KILL -u "$user"
17 if [[ $? -ne 0 ]]; then
18 echo "ALERT: Failed to kill session for user $user."
19 script_errors=true
20 fi
21done
22
23echo "Fully locking all user accounts (SSH & local access)..."
24for user in $(awk -F: '{if ($3 >= 1000 && $3 < 65534) print $1}' /etc/passwd); do
25 passwd -l "$user" &> /dev/null
26 usermod -L -e 1 "$user" &> /dev/null
27 if [[ $? -ne 0 ]]; then
28 echo "ALERT: Failed to lock user account $user."
29 script_errors=true
30 fi
31done
32
33echo "Locking root account..."
34passwd -l root &> /dev/null
35usermod -L -e 1 root &> /dev/null
36if [[ $? -ne 0 ]]; then
37 echo "ALERT: Failed to lock root account."
38 script_errors=true
39fi
40
41if [[ "$script_errors" == true ]]; then
42 echo "ALERT: Errors occurred during execution. Exiting with code 1."
43 exit 1
44fi
45
46echo "All users have been kicked off and all accounts are completely locked."
47
Linux - Lock Device
This script forcefully logs out all active local users and locks all local user accounts, including root, on a Linux system. It is designed to restrict access by disabling user logins, making it useful for security enforcement or system lockdowns. Caution should be taken, as it also locks the root account, which may prevent administrative access unless alternative authentication methods (e.g., SSH keys) are available.
Bash
100
Local system
Explore more automations, scripts, and policies to further enhance your IT operations.