OGOPSEC Brand Mark
OGOPSECOBSIDIAN GOOSE

OGOPSEC // Dispatch

Lock Your Linux Session When You Remove the Key

By Jamie Folsom ·

Authentication answers "who may log in." It says nothing about the moment you stand up, key in pocket, and leave a logged-in machine behind. If you want the physical presence of your key to mean something after login too, you can make Linux lock the session the instant the device is removed. The mechanism is simple and built in: configure a udev rule so that unplugging your key triggers a screen lock.

This post shows a working rule, a more robust script-based variant, and — because the simple version has sharp edges — a clear account of what locking on USB removal does and does not protect against.

Find your device

First identify the key so the rule only fires for it. Plug it in and run:

Watch the monitor output as you unplug and replug; you will see the remove and add events and the attributes attached to them. You want the vendor and product IDs for your specific key.

The basic udev rule

Create a rule file, substituting your own IDs:

Now remove the key and your sessions should lock. loginctl lock-sessions asks logind to lock every active session, which the desktop environment turns into a lock screen.

A more robust variant

Calling loginctl directly from udev is fragile: udev runs actions in a minimal environment, long-running or interactive commands are discouraged, and "lock all sessions" is a blunt instrument on a multi-user box. A cleaner approach is to have the rule start a tiny systemd unit, and let that unit do the work with proper context. Point the rule at a service:

Then define /etc/systemd/system/lock-on-removal.service with a short ExecStart script that locks the specific target user's session (for example, by resolving the active session with loginctl list-sessions and calling loginctl lock-session <id>). Keeping the logic in a script makes it testable and lets you log what happened, which udev's fire-and-forget RUN does not.

Test before you trust it

Reload rules and exercise the behaviour deliberately:

Verify it fires reliably across a reboot and across suspend/resume, not just once in a fresh session. Removal events are generally dependable, but a rule you have not tested under those conditions is a rule you cannot count on.

What this protects against — and what it doesn't

Lock-on-removal is a good habit-enforcer. It defends against the realistic, everyday risk: you get up, forget to lock, and someone has a few minutes alone with an open session. Tying the lock to the key you are already carrying removes the forgetfulness from the equation.

It is not a serious barrier to a determined attacker, and it is important to be clear why:

  1. A lock screen is not encryption. The session is still running and your decrypted data is still in memory. An attacker with physical access and skill can attack the running system directly, bypassing the lock entirely.
  2. The rule matches by device identity, not cryptographic proof. A device presenting the same vendor and product IDs would be treated the same way. This mechanism controls locking, not authentication; do not confuse the two.
  3. It does nothing about cold-boot or DMA attacks against the contents of RAM.
  4. It can be undone by anyone who can edit the rule — that is, anyone with root on the machine.

In other words: this makes the right thing happen automatically in the common case. It is a convenience that enforces discipline, not a defence against an adversary who already has your hardware and time.

Where it fits

Pair lock-on-removal with the things that actually protect data when the machine is out of your hands: home-directory or full-disk encryption, a short idle-lock timeout as a fallback, and a login factor that fails closed. OGOPSEC's Dire Wolf ties session state to the presence of its hardware credential as part of a broader fail-closed design, but you can get the everyday benefit today with a dozen lines of udev. Just hold it to its real job — enforcing the lock you would otherwise forget — and let encryption carry the weight it was built for.