Ubuntu: How to Lock/Unlock a User Account

Updated: January 31, 2024 By: Guest Contributor Post a comment

Introduction

Managing user accounts accurately and securely is a fundamental aspect of system administration. In a multi-user environment like Ubuntu, it’s crucial to know how to lock and unlock user accounts to safeguard the system from unauthorized access. Locking a user account disables the user from logging in, whereas unlocking it restores access. In this tutorial, we will guide you through the process of locking and unlocking user accounts on Ubuntu.

In Ubuntu, all user account information is stored in the /etc/passwd and /etc/shadow files. These files contain each user’s login details, including the account’s locked or unlocked state. To manipulate an account’s status, we utilize the command-line tools available in the GNU/Linux ecosystem.

Locking a User Account

To lock a user account, you will need to add an exclamation mark (!) at the beginning of the encrypted password in the /etc/shadow file. This action can be done manually or using the passwd command with the --lock option.

Here’s how to lock a user account using the passwd command:

sudo passwd --lock username

Replace username with the actual username of the account you want to lock. After running the command, the user will not be able to log in using their account until it is unlocked.

Unlocking a User Account

To unlock a user account, simply remove the exclamation mark preceding the encrypted password in the /etc/shadow file, or use the passwd command with the --unlock option:

sudo passwd --unlock username

Again, ensure you replace username with the actual username of the account you are intending to unlock. This command will allow the user to log in to their account once again.

Checking Account Status

To check whether a user account is locked or unlocked, you can inspect the /etc/shadow file or use the following command:

sudo passwd --status username

This command will output the status of the user’s account, including if it is locked (L) or unlocked (P).

Handling User Sessions

If a user is currently logged in and you lock their account, they will not be forcibly logged out. However, they will be unable to create new sessions. To log out a user forcefully, you can use the pkill command:

sudo pkill -KILL -u username

This will terminate all processes owned by the user, effectively logging them out.

Scripting Account Locks

To handle multiple user accounts efficiently, consider writing a shell script to lock or unlock accounts in bulk. This can streamline the process and reduce the likelihood of human error.

Below is an example shell script that can be used to lock or unlock multiple Ubuntu user accounts in bulk. The script accepts two parameters: a file containing a list of usernames and an action (lock or unlock). It then iterates through each username in the list and performs the specified action.

#!/bin/bash

# Check if two arguments are provided
if [ "$#" -ne 2 ]; then
    echo "Usage: $0 <user_list_file> <action>"
    echo "action: lock or unlock"
    exit 1
fi

# File containing list of users
USER_LIST_FILE=$1

# Action to perform: lock or unlock
ACTION=$2

# Check if the user list file exists
if [ ! -f "$USER_LIST_FILE" ]; then
    echo "Error: User list file does not exist."
    exit 1
fi

# Function to lock a user account
lock_user() {
    local username=$1
    passwd --lock "$username"
    echo "Locked $username"
}

# Function to unlock a user account
unlock_user() {
    local username=$1
    passwd --unlock "$username"
    echo "Unlocked $username"
}

# Process each user
while IFS= read -r username; do
    if [ "$ACTION" = "lock" ]; then
        lock_user "$username"
    elif [ "$ACTION" = "unlock" ]; then
        unlock_user "$username"
    else
        echo "Invalid action: $ACTION"
        exit 1
    fi
done < "$USER_LIST_FILE"

To use this script:

  1. Create a text file containing the usernames, each on a new line.
  2. Run the script with the file and the desired action. For example, to lock users listed in users.txt, use: ./script.sh users.txt lock
  3. To unlock, use: ./script.sh users.txt unlock

Ensure the script has execute permissions with chmod +x script.sh.

Note: This script should be run with sufficient privileges to modify user accounts, typically as root or using sudo. Always test scripts in a safe environment before deploying them in production.

Best Practices

  • When locking or unlocking user accounts in a production environment, communication is key. Notifying the affected users and explaining the reason behind locking their account can prevent confusion and possible disruptions.
  • When locking or unlocking user accounts, it’s vital to ensure that no system services are running under that user. Locking an account used by system services may disrupt the functionality of your Ubuntu system. Always check running processes and, if necessary, notify users before making any changes to their accounts.

Conclusion

This tutorial has covered the essential commands and considerations for locking and unlocking user accounts on Ubuntu. With these tools in hand, you can maintain a secure and smooth operating environment for all system users.