Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 115Security · Users

How to lock or disable a user account

A colleague leaves, a contractor's project ends, an account looks compromised — the access must stop now, but the files, mails and logs must stay for a while. The trap: passwd -l only locks the password; SSH keys still work. This how-to disables an account completely (password, keys, sudo, running sessions, cron), checks it, and deletes it later in a clean way.

  • 5 Lessen
  • 16 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

115.1

The quick answer

usermod -L -e 1 bob
usermod -s /sbin/nologin bob
gpasswd -d bob wheel
loginctl terminate-user bob
chage -l bob | grep -i 'account expires'
Account expires                                         : Jan 02, 1970

-L locks the password, -e 1 expires the account (blocks every login, also with SSH keys), nologin takes away the shell.

115.2

Step by step

  1. Lock everything that gives access:
CommandStops
usermod -L bobPassword logins.
usermod -e 1 bob (or chage -E 0 bob)All logins via PAM — also SSH keys.
usermod -s /sbin/nologin bobInteractive shells.
gpasswd -d bob wheel (sudo on Ubuntu)Admin rights via sudo.
mv ~bob/.ssh/authorized_keys …disabledSSH keys (belt and braces).
  1. End running sessions and jobs:
loginctl terminate-user bob
pkill -KILL -u bob
crontab -l -u bob
crontab -r -u bob
systemctl list-timers --all | grep -i bob

Before removing a crontab, check whether a real job runs as this user (it may need a new owner).

  1. Other places the person had access — the Linux account is only one door: VPN, Git (GitHub/GitLab) and cloud accounts, shared passwords and API tokens they knew (rotate them), deploy keys, Kubernetes kubeconfigs.
  2. Check what is left:
passwd -S bob
bob LK 2026-11-19 0 99999 7 -1 (Password locked.)
sudo -l -U bob
User bob is not allowed to run sudo on web1.
ps -u bob
  1. Unlock (the person comes back, or it was a mistake):
usermod -U -e '' -s /bin/bash bob
  1. Delete later — after the retention time, archive the home folder, then remove the user and find files outside the home:
tar -czf /archive/home-bob-2026-11-19.tar.gz -C /home bob
userdel -r bob
find / -xdev -nouser -print 2>/dev/null

Many servers? Do it with Ansible (ansible.builtin.user with expires, password_lock, state: absent) or central identity (IdM/FreeIPA, Active Directory) — one place to disable.

115.3

Check that it worked

From another machine, SSH with the person's key is refused:

ssh -i bob_key bob@web1
bob@web1: Permission denied (publickey).

passwd -S shows LK, chage -l shows the account expired, sudo -l -U bob shows no rights, and ps -u bob shows no processes.

115.4

Common problems

ProblemCauseFix
The person still logs in with an SSH keyOnly the password was locked.Expire the account (usermod -e 1) and remove authorized_keys.
Still logged in after lockingExisting sessions are not ended by locking.loginctl terminate-user, pkill -u.
A job broke after removing the userA cron job or service ran as this person.Move jobs to a service account before deleting people.
userdel: user bob is currently used by process …Processes still running.Stop them first; then userdel.
Account works on other serversLocal accounts on each server.Use Ansible for all servers, or central identity.
Files of the deleted user belong to a new user laterUID reused.Find and re-own -nouser files after deletion.

115.5

Go further

1Why is passwd -l not enough to stop a user?

It locks only the password; SSH key logins still work — expire the account too.

2Which command expires an account so that no login works any more?

Alle commando's in dit hoofdstuk