Disable a Linux User Account Without Leaving SSH Open

Share:

Close-up of a Linux penguin sticker placed on a blue ice cube tray with frozen cubes.

Knowing how to disable a Linux user account is routine sysadmin work, but there’s a trap in it that catches experienced administrators. The most commonly recommended method, locking the password, does not stop someone logging in with an SSH key.

That means the standard “lock the account” reflex can leave a departed employee with working access. This guide covers four tested methods and explains which ones actually close every door. It finishes with a checklist for full revocation.

By Chukwuma Irozuru

A close-up shot of a hand holding a penguin sticker against a blurred outdoor background.

Method 1: Expire the Account (Recommended)

Setting an expiry date in the past disables the account completely, including SSH key logins, because account expiry is checked during the account phase of authentication regardless of how the user authenticated.

Bash
# Disable immediately (1 = one day after the Unix epoch, i.e. 1970-01-02)
sudo usermod --expiredate 1 fatima

# Verify
sudo chage -l fatima | grep "Account expires"

# Re-enable (set back to "never")
sudo usermod --expiredate "" fatima

You can also set a future date, which is useful for contractors with a known end date.

Bash
# Auto-disable at end of January 2027
sudo chage -E 2027-01-31 contractor_john

# Remove the expiry
sudo chage -E -1 contractor_john

This is the method to reach for first when you need to disable a Linux user account properly. It’s the only single command here that blocks both password and key-based access.

Method 2: Lock the Password

Locking prepends a ! to the password hash, making it impossible to match. Both commands below do exactly the same thing.

Bash
# Either of these locks the password
sudo passwd -l paul
sudo usermod -L paul

# Check the status (L = locked, P = usable password)
sudo passwd -S paul

# Unlock
sudo passwd -u paul

Important: This only blocks password authentication. If the user has an entry in ~/.ssh/authorized_keys, they can still log in. Use this when you want to force a password reset workflow, not when you’re revoking access.

The Passwordless Account Gotcha

If the account had no password to begin with, unlocking fails:

passwd: unlocking the password would result in a passwordless account.

Set a password instead of unlocking, or use usermod -p as the error message suggests.

Method 3: Force a Password Change

This doesn’t disable anything. It marks the current password as expired, so the user must change it at the next login.

Bash
# Force a reset at next login
sudo passwd -e office

Useful for compliance rotations or after a suspected credential leak. Note that passwd -e takes no date argument. If you want a scheduled expiry, use chage as shown in Method 1.

Method 4: Set a Nologin Shell

Replacing the login shell stops interactive logins while keeping the account functional for services that run as that user.

Bash
# Prevent interactive login
sudo usermod -s /usr/sbin/nologin serviceacct

# Restore
sudo usermod -s /bin/bash serviceacct

This is the right tool for service accounts. It won’t stop commands forced through SSH or scheduled jobs, so don’t treat it as a security boundary on its own.

Watch Out: The -l Flag Means Two Different Things

This trips people up and the consequences are messy.

CommandFlagWhat it does
passwd -l user--lockLocks the password
usermod -L user--lockLocks the password (same effect)
usermod -l new old--loginRenames the account
passwd -e user--expireExpires the password now
usermod -e DATE user--expiredateSets account expiry date

So -l locks in passwd but renames in usermod. And -e expires a password in passwd but sets an account expiry date in usermod. Read the command, not just the flag.

Why Locking Alone Won’t Disable a Linux User Account

Three gaps matter when you’re revoking access rather than pausing it.

Locking the password leaves SSH key authentication working. It also leaves existing sessions running, so someone already logged in stays logged in indefinitely. And root can still switch to the account with su - user, since that bypasses password checks entirely.

Any one of these means the account isn’t really disabled. Treat password locking as a pause button, not a revocation.

Talk to our team about Linux hardening and access management → TALK

Full Lockout Checklist

When someone leaves and you need to disable a Linux user account completely, run all of these.

Bash
USER=old_staff

# 1. Expire the account (blocks password AND key logins)
sudo usermod --expiredate 1 "$USER"

# 2. Lock the password as well
sudo usermod -L "$USER"

# 3. Remove interactive shell access
sudo usermod -s /usr/sbin/nologin "$USER"

# 4. Revoke SSH keys
sudo mv /home/"$USER"/.ssh/authorized_keys \
        /home/"$USER"/.ssh/authorized_keys.revoked

# 5. Terminate any running sessions
sudo pkill -KILL -u "$USER"

# 6. Check for scheduled jobs left behind
sudo crontab -u "$USER" -l

Steps four and five are the ones people forget, and they’re the ones that matter most.

Verifying You Actually Disabled the Account

Always confirm rather than assume. After you disable a Linux user account, these three checks tell you whether it really took effect.

Bash
# Password status: L = locked, P = usable, NP = no password
sudo passwd -S username

# Full account ageing and expiry details
sudo chage -l username

# Any sessions still active?
who | grep username

Drop a comment, we will love to hear from you

Read More Here

More from this Author

Leave a Reply

Your email address will not be published. Required fields are marked *

Verified by MonsterInsights