#Why this matters
Every Linux server starts with three access boundaries: file permissions, login authentication, and remote shell configuration. Misconfigured permissions leak secrets or enable local privilege escalation, while weak authentication or exposed SSH settings invite automated brute-forcing within minutes of deployment.
Hardening these layers requires precision because a small mistake in POSIX ACLs, Pluggable Authentication Modules (PAM), or /etc/ssh/sshd_config can silently break services or lock administrators out of a remote machine.
#Standard permissions, special bits, and POSIX ACLs
Standard POSIX permissions divide access into owner (u), owning group (g), and others (o), with read (4), write (2), and execute (1) bits. Three special bits add targeted behaviour:
| Bit | Octal | Effect on files | Effect on directories |
|---|---|---|---|
| SUID | 4000 |
Process runs with file owner's EUID | Ignored on Linux |
| SGID | 2000 |
Process runs with file group's EGID | New items inherit directory's group |
| Sticky | 1000 |
Ignored on modern Linux | Only owner or root can delete or rename items |
When a single owning group is too coarse, POSIX Access Control Lists (ACLs) grant permissions to specific users or secondary groups without making a file world-readable. A trailing + in ls -l (for example -rw-r-----+) signals an extended ACL.
$ sudo setfacl -m u:auditor:rx /srv/reports
$ sudo setfacl -m d:u:auditor:rx /srv/reports
$ getfacl /srv/reports
# file: srv/reports
# owner: root
# group: ops
user::rwx
user:auditor:r-x
group::r-x
mask::r-x
other::---
default:user::rwx
default:user:auditor:r-x
default:group::r-x
default:mask::r-x
default:other::---Watch the mask:: entry closely. On a file with an ACL, the group bits shown by ls -l represent the ACL mask, not the owning group alone. If you run chmod g-x on that file, you shrink the mask and silently strip execute permission from auditor as well (which getfacl flags with #effective:r--). To remove an ACL cleanly, use setfacl -x u:auditor /srv/reports or setfacl -b to strip all extended entries.
#How PAM evaluates logins
PAM separates authentication logic from services like sshd, login, and sudo. Each service has a configuration file under /etc/pam.d/ made of lines with four fields:
- module_typecontrol_flag | module_path | module_arguments
PAM splits rules into four management groups:
- auth: verifies identity (passwords, hardware keys, or lockout counters via
pam_faillock.so). - account: checks account validity (expiry, locked status, allowed hosts,
pam_nologin.so). - password: handles credential updates and complexity (
pam_pwquality.so). - session: sets up and tears down sessions (
pam_limits.so, home directories, login logs).
Control flags decide how each result affects the stack:
required: must pass, but PAM runs remaining modules of that type before failing (hiding which check failed).requisite: must pass, and aborts the stack immediately on failure.sufficient: if it passes and no earlierrequiredfailed, succeeds immediately; failure is non-fatal.optional: matters only when no other module of that type returns a result.
#Hardening OpenSSH safely
OpenSSH reads /etc/ssh/sshd_config (and drop-in files under /etc/ssh/sshd_config.d/*.conf, where earlier directives take precedence). A strong baseline eliminates password brute-forcing, blocks direct root login, and restricts who may connect:
# /etc/ssh/sshd_config.d/99-hardening.conf
PermitRootLogin no
PubkeyAuthentication yes
PasswordAuthentication no
KbdInteractiveAuthentication no
UsePAM yes
AllowGroups sshusers
MaxAuthTries 3
X11Forwarding no
AllowAgentForwarding noKeeping UsePAM yes while PasswordAuthentication no is set ensures PAM account and session modules (account expiration, nologin, resource limits) still run for key-based logins.
Before disabling password logins, confirm your Ed25519 key works and verify permissions under StrictModes yes (the default). If ~/.ssh is not 700 or ~/.ssh/authorized_keys is writable by group or others (for instance 664), sshd silently ignores the key and falls back or rejects the login.
#Common mistakes
- Disabling passwords before testing keys: confirm key login in a second terminal while keeping an admin shell open.
- Ignoring
sshd_config.dprecedence: OpenSSH applies the first matching directive, so an early drop-in file overrides settings appended tosshd_config. - Loose
.sshpermissions:sshrejects private keys looser than0600, andsshd(StrictModes yes) rejectsauthorized_keysif group or others can write~,~/.ssh, orauthorized_keys. - Editing
/etc/pam.d/without a backup: a syntax error incommon-authorsystem-authcan breaksudoandsshdtogether.
#Summary
Standard UNIX bits (rwx, SUID, SGID, sticky) handle basic ownership, while POSIX ACLs add per-user rules capped by the ACL mask. PAM evaluates logins across auth, account, password, and session stacks using control flags such as required, requisite, and sufficient. Hardened OpenSSH disables root and password logins while keeping UsePAM yes, enforcing StrictModes yes, and verifying syntax with sshd -t before closing an active shell.