Course contentsShow
← Linux
Lesson 874 of 1,8198. User Management and Pluggable Authentication ModulesFree lesson

Automating User Account Reconciliation

You will run periodic scripts to ensure local account states match the authoritative directory.

In this lesson, you will learn how to create automated reconciliation scripts to ensure that your local system accounts remain in perfect alignment with your authoritative identity provider.

Why Reconciliation Matters

Even with robust tools like SSSD for directory integration, "drift" can occur. A user might be disabled in your Active Directory or LDAP server, but if their local account UID still exists or a cached credential remains, they might retain access to specific files or local services. Reconciliation is the process of periodically comparing the source of truth (the central directory) against the local /etc/passwd file and the local file system to prune stale accounts.

Implementing the Reconciliation Loop

The goal is to write a script that queries the central directory for active users and cross-references them with local accounts. If a user is not found in the directory but exists locally, the script can flag them or disable their access.

Here is a simplified logic flow for a reconciliation script:

#!/bin/bash
# Get list of authoritative users from the directory
getent passwd | cut -d: -f1 > /tmp/dir_users.txt

# Iterate through local system users (excluding system defaults)
for user in $(awk -F: '$3 >= 1000 && $3 < 60000 {print $1}' /etc/passwd); do
    if ! grep -q "^$user$" /tmp/dir_users.txt; then
        echo "Alert: User $user no longer exists in directory. Locking account..."
        usermod -L "$user"
    fi
done

Best Practices for Automation

  1. Scheduled Execution: Run these scripts via cron or a systemd timer during off-peak hours to minimize performance impact.
  2. Audit First, Act Second: Initially, configure your scripts to log discrepancies to a centralized auditing system rather than automatically deleting users. This prevents accidental lockouts due to temporary network partitions between your server and the identity provider.
  3. Handle Home Directories: Reconciliation isn't just about the account entry; consider using find to identify orphan files owned by non-existent UIDs, which can be moved to a quarantine area before permanent deletion.

Key Takeaway: Automated reconciliation acts as a vital safety net, ensuring that directory-level identity changes are strictly enforced at the local system level by pruning stale accounts and preventing unauthorized persistent access.