OGOPSEC Brand Mark
OGOPSECOBSIDIAN GOOSE

OGOPSEC // Dispatch

Prevent Unauthorized Linux Login: Access Control That Holds

By Jamie Folsom ·

To prevent unauthorized Linux login, you first have to see that "login" is not a single door. There is the local console, remote access over SSH, display-manager logins on a desktop, and underneath all of them the PAM stack that every one of those paths calls. An attacker only needs one of these to be weak. This post walks each entry point and the control that holds it, then is honest about where access control ends and other defenses have to begin.

Start with an accurate inventory of accounts

You cannot protect logins you do not know exist. List real, loginable accounts and lock the ones that should never authenticate interactively:

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

Service accounts should have a shell of /usr/sbin/nologin and a locked password (passwd -l). Every account with a usable shell and a valid credential is a login an attacker can aim at.

Close the remote door

SSH is the most-scanned entry point on any internet-facing box. The essentials, in /etc/ssh/sshd_config:

PasswordAuthentication no
PermitRootLogin no
AllowUsers alice bob

Key-only authentication removes password guessing; AllowUsers means that even a valid credential for some other account cannot log in remotely. Apply changes, then confirm from a second connection before closing your current one.

Close the local door

Physical and console access deserve the same scrutiny. Two measures matter:

  • Automatic screen locking. An unlocked, unattended session is an unauthorized login waiting to happen. Set an idle lock in your desktop environment, and consider locking on events like a security key being removed.
  • Bootloader and firmware passwords. Without them, someone at the keyboard can edit the GRUB entry to boot into a root shell, bypassing your login entirely. See our GRUB security post for the specifics.

The layer underneath: PAM

Every login path — console, SSH, su, the display manager — ultimately runs through Pluggable Authentication Modules. That makes PAM both the strongest place to enforce a rule and the most dangerous place to make a mistake. Two high-value, low-risk policies:

Lock out brute force

The pam_faillock module locks an account after repeated failures, which blunts local and service-level guessing:

# in /etc/security/faillock.conf
deny = 5
unlock_time = 900

Require more than a password

PAM is where you add a second factor or a hardware requirement to every login at once. A module like pam_u2f can require a FIDO2 security key; a credential such as Dire Wolf uses a fail-closed PAM policy — no authorized device present, no session — so pulling the token denies login across all paths rather than just one.

The warning that comes with PAM: a broken PAM configuration can lock out everyone, including you. Always keep a root session open while editing PAM files, and test in a separate login before you trust the change.

Watch for the logins you didn't prevent

Prevention is never perfect, so instrument it. Know where successful and failed logins are recorded and actually look:

journalctl -u sshd
lastb        # failed login attempts
last         # successful logins

A login you detect quickly is a far smaller problem than one you find in a forensic review three months later.

What access control does and doesn't cover

Closing these doors prevents the unauthorized logins that come from guessed passwords, forgotten accounts, exposed remote access, and walk-up console attacks. For most systems, that is the overwhelming majority of the risk.

It does not help once someone holds a legitimate credential — a stolen unlocked laptop with keys in the agent, a phished session, an insider with a valid account. It also does nothing about data at rest if the disk is removed and read on another machine; that is the job of encryption, not login control. And a login prevented at the OS level says nothing about an application-level account on a service you run.

Access control decides who may start a session. It is necessary and it is not sufficient — pair it with encryption for data at rest and with monitoring for the attempts that slip through.