How to Audit User Accounts and Remove Unused Ones

Over time, servers accumulate user accounts — former team members, test accounts, service accounts no longer in use. Each unused account is unnecessary attack surface worth periodically auditing and cleaning up.

Why This Matters

An unused account is still a valid authentication target — if it has a weak or default password, or was created for someone no longer authorized, it represents unnecessary risk with zero legitimate ongoing benefit.

Step 1 — List All User Accounts

cat /etc/passwd

Review the full list, distinguishing between system/service accounts (typically with UIDs below 1000) and actual human user accounts.

Step 2 — List Accounts That Can Actually Log In

awk -F: '($7 !~ /nologin|false/) {print $1}' /etc/passwd

Filters out accounts with a shell explicitly set to nologin or false, showing only accounts that could theoretically be used for interactive login.

Step 3 — Check Last Login Time for Each Account

lastlog

Shows the most recent login time for each account — accounts showing "Never logged in" or a very old last-login date are candidates for review.

Step 4 — Cross-Reference Against Your Actual Current Team

For each active-seeming account, confirm it corresponds to someone who genuinely still needs access — a former employee/contractor's still-active account is a common, easily-overlooked security gap.

Step 5 — Disable (Don't Immediately Delete) Suspicious Accounts

sudo usermod -L username
sudo usermod -s /usr/sbin/nologin username

Locking the account and disabling its shell prevents login while preserving the account and its files temporarily — useful in case you need to investigate or reference something before fully removing it.

Step 6 — Fully Remove Confirmed-Unnecessary Accounts

sudo userdel -r username

-r also removes the user's home directory and mail spool — verify you don't need anything from that account's files before using this flag.

Step 7 — Audit sudo Group Membership

getent group sudo

Review specifically who has sudo/administrative privileges — even more important to keep current than general account existence, given the elevated access sudo group membership grants.

Step 8 — Audit SSH Authorized Keys

for user_home in /home/*; do
    echo "=== $user_home ==="
    cat "$user_home/.ssh/authorized_keys" 2>/dev/null
done

Review which SSH keys are authorized for each account — an old, no-longer-relevant key left in authorized_keys is a persistence risk if that key was ever compromised or belongs to someone no longer authorized.

Establishing an Offboarding Process

Rather than periodic cleanup alone, establish a clear process for removing access immediately when someone's authorization ends (employee departure, contractor engagement ending) — prevention is better than periodic detection.

Setting a Regular Audit Schedule

Perform this account audit on a regular cadence (quarterly, for example), not just once — account accumulation is an ongoing, gradual process that benefits from consistent periodic review.

Common Errors

Accidentally removing an account still needed by a running service — verify an account isn't a service account something depends on before deletion; check for any processes running as that user first.

Continue Reading

Browse more articles in Server Security & Hardening.

  • audit user accounts linux, remove unused accounts, user account cleanup, offboarding server access
  • 0 Utenti hanno trovato utile questa risposta
Hai trovato utile questa risposta?

Articoli Correlati

SSH Hardening: Change the Port, Disable Root Login & Use SSH Keys (Ubuntu & Debian)

SSH is the front door to your VPS — and by default it listens on a well-known port, often...

How to Install and Configure Fail2Ban on Ubuntu & Debian (Complete Guide)

Fail2Ban monitors your server's log files and automatically blocks (bans) IP addresses that show...

How to Configure UFW Firewall on a Linux VPS (Ubuntu & Debian)

UFW (Uncomplicated Firewall) is the standard firewall front-end on Ubuntu and Debian. A correctly...

How to Enable Two-Factor Authentication (2FA) for SSH on a Linux VPS

Two-Factor Authentication (2FA) adds a second layer of protection to SSH: even if your password...

VPS Security Checklist for Beginners: 12 Essential Steps

Every new VPS is deployed with default settings that are convenient but not secure. This...