SSH Bastion Hardening & Fail2ban with Systemd Backend
Before you begin
- Applies to:Linux SSH bastion hardening with Fail2ban and a systemd journal backend.
- Where to start:Start with a console or an approved fallback connection to the bastion. Test access separately before closing the original session.
- What you need:Have administrator access, approved management sources, a tested SSH login, and backups of the existing configuration.
Guide tools
Scope, Purpose & Architecture Baseline
This procedure establishes a baseline for hardening public-facing or perimeter SSH bastion hosts and configuring Fail2ban to mitigate brute-force attempts.
| Attribute | Specification |
|---|---|
| Document ID | SOP-SEC-004 (Revision 2) |
| Classification | Operational Guide |
| Subsystem | systemd-journald, sshd, fail2ban |
| Change Window Required? | Yes (brief SSH service reload) |
Hardened SSH Bastion Architecture: Public SSH access restricted to Ed25519 cryptographic keys on non-standard ports, actively monitored by Fail2ban via the systemd-journald backend to dynamically inject kernel firewall bans (nftables/iptables) against brute-force attacks.
Phase 1: Pre-Flight & Key Provisioning
Generate and Deploy Ed25519 Public Key
Key ManagementGenerate an Ed25519 keypair with 100 key-derivation function (KDF) rounds on the local administrative workstation and copy the public key to the bastion host.
# 1. Generate key with 100 KDF rounds (run on local workstation)ssh-keygen -t ed25519 -a 100 -C "admin-bastion-2026"
# 2. Transfer public key to target bastion serverssh-copy-id -i ~/.ssh/id_ed25519.pub sysadmin@<BASTION_IP>
# 3. Confirm keypair authentication succeeds before hardening daemonssh -i ~/.ssh/id_ed25519 sysadmin@<BASTION_IP>Phase 2: OpenSSH Daemon Hardening
Deploy Modular Hardened Directives via Drop-In
OpenSSH ConfigurationAvoid editing /etc/ssh/sshd_config directly. Deploy a modular drop-in file under /etc/ssh/sshd_config.d/, disable root login and passwords, and validate syntax before restarting.
# Create drop-in configuration filesudo tee /etc/ssh/sshd_config.d/99-hardened-ssh.conf > /dev/null << 'EOF'PermitRootLogin noPasswordAuthentication noKbdInteractiveAuthentication noPubkeyAuthentication yesMaxAuthTries 3MaxSessions 4ClientAliveInterval 300ClientAliveCountMax 2EOF
# Pre-Restart Syntax Test: Verify configuration syntaxsudo sshd -t
# Apply new configuration if no errors are reportedsudo systemctl restart sshPhase 3: Fail2ban with Journald Backend
Install & Configure Fail2ban for Systemd Integration
Intrusion PreventionInstall Fail2ban and Python systemd bindings, then configure a dedicated jail override utilizing the systemd backend with strict rate limits.
# Install Fail2ban along with systemd Python bindingssudo apt update && sudo apt install -y fail2ban python3-systemd
# Create modular jail overridesudo tee /etc/fail2ban/jail.d/sshd-systemd.local > /dev/null << 'EOF'[sshd]enabled = trueport = sshmode = aggressivebackend = systemdmaxretry = 3findtime = 600bantime = 3600ignoreip = 127.0.0.1/8 ::1 10.0.0.0/8 172.16.0.0/12 192.168.0.0/16EOF
# Enable and start Fail2ban servicesudo systemctl enable --now fail2banPhase 4: Operational Verification & Ingestion Test
Verify Fail2ban Ingestion and Live Systemd Journal Matching
VerificationVerify that the Fail2ban SSH jail is actively inspecting the systemd journal and tracking authentication failures.
# Inspect active jail statussudo fail2ban-client status sshd
# Expected Output Verification:# Status for the jail: sshd# |- Filter# | |- Currently failed: 0# | |- Total failed: 0# | `- Journal matches: _SYSTEMD_UNIT=ssh.service + _COMM=sshd# `- Actions# |- Currently banned: 0# |- Total banned: 0# `- Banned IP list:Phase 5: Emergency Operations & Rollback
Emergency Unban and Service Rollback
Incident ResponseUnban accidentally blocked administrative IPs or execute complete rollback to default configuration if connectivity anomalies occur.
# Option A: Emergency Unban of Administrator IP Addresssudo fail2ban-client set sshd unbanip <BLOCKED_IP>
# Option B: Complete Rollback of Hardening Overridessudo rm -f /etc/ssh/sshd_config.d/99-hardened-ssh.conf /etc/fail2ban/jail.d/sshd-systemd.localsudo systemctl restart ssh && sudo systemctl restart fail2ban
# Confirm listening statussystemctl is-active sshYour feedback keeps these production procedures accurate and battle-tested.