Using systemd‑journald Filters to Stop Spurious SSH Login Alerts

Spurious SSH Alerts in systemd‑journald: A Quick Filter Fix

I’ve spent a lot of time staring at the journalctl -u sshd output on busy servers. Every minute a bot tries something, and the journal fills with lines that look like:

Aug 12 12:34:56 host sshd[1234]: Failed password for invalid user admin from 203.0.113.42 port 54321 ssh2

If you forward every Failed password line straight to a SIEM or just watch the console, you’ll be drowning in alerts that are usually nothing more than failed logins from bots, mistyped passwords, or legitimate users on a shared workstation. The noise makes it hard to spot the real bad guys.

The good news? systemd‑journald can filter messages before they ever hit your monitoring pipeline. A tiny tweak in /etc/systemd/journald.conf drops most of those “spurious” alerts without touching the SSH daemon itself.


Why Filter at the Journal Layer?

  • Centralized control – One change, one place. No need to patch sshd_config or add custom PAM modules.
  • Performance – Filtering happens in the journal daemon, not in SSH. It’s lightweight and scales with the number of logins.
  • Audit‑ready – The filtered messages never leave the system, so you keep a clean audit trail for compliance.

Step‑by‑Step: Setting Up a Filter

  1. Open the configuration file

    sudo nano /etc/systemd/journald.conf
    
  2. Add a SystemMaxUse= line if you haven’t already
    It keeps the journal from growing out of control, but it’s unrelated to filtering.

  3. Insert the filter

    # Drop SSH failed attempts from known bad IP ranges
    # The following line uses a regular expression to match the message field
    # and discards it if the source IP is in a blacklist.
    # Example blacklist: 203.0.113.0/24, 198.51.100.0/24
    # Adjust the regex to your environment.
    # See systemd documentation for syntax: https://systemd.io/JOURNALD/
    # Note: systemd‑journald uses the same syntax as systemd‑unit files.
    # The filter is applied after the message is parsed.
    # The syntax is:
    #   Match=Field=Value
    #   Result=ignore
    # For regex, use Match=Field=~Regex
    # Example:
    #   Match=__CURSOR=~.*Failed password.*from 203\.0\.113\.\d+.*$
    #   Result=ignore
    

    The key lines are:

    Match=__CURSOR=~.*Failed password.*from 203\.0\.113\.\d+.*$
    Result=ignore
    
    • __CURSOR is a pseudo‑field that contains the raw log line.
    • The regex matches any Failed password line that contains an IP in the 203.0.113.0/24 block.
    • Result=ignore tells journald to drop the entry.
  4. Reload systemd‑journald

    sudo systemctl restart systemd-journald
    
  5. Verify

    journalctl -u sshd | grep "Failed password"
    

    You should no longer see entries from the blacklisted range.


Extending the Filter

  • Multiple IP ranges – Combine regex alternations:

    Match=__CURSOR=~.*Failed password.*from (203\.0\.113\.|198\.51\.100\.)\d+.*$
    
  • Exclude specific users – Add a second filter:

    Match=__CURSOR=~.*Failed password for invalid user admin.*
    Result=ignore
    
  • Keep a whitelist – Instead of ignoring, you can set Result=ok for trusted IPs and let everything else pass.


Trade‑offs & Caveats

Trade‑offDetail
False negativesA regex that’s too broad may drop legitimate failed attempts from a new user. Test with journalctl --grep before applying.
PerformanceThe filter runs in user space; it’s negligible for normal traffic, but heavy regexes on a high‑traffic server could add CPU load. Keep patterns simple.
Audit trailOnce ignored, the event never appears in the journal. If you need a record for compliance, consider logging to a separate file with ExecStartPost= in an sshd.service override.
MaintenanceIP ranges change. Use a script to regenerate the filter file from a dynamic blocklist (e.g., from fail2ban).

Quick Script to Update the Filter

#!/usr/bin/env bash
# update-journald-ssh-filter.sh
# Pulls a blocklist from fail2ban and writes a journald filter.

BLOCKLIST=$(fail2ban-client status sshd | grep "Currently banned:" | awk '{print $4}')
if [ -z "$BLOCKLIST" ]; then
    echo "No banned IPs."
    exit 0
fi

REGEX=$(printf '%s' "$BLOCKLIST" | sed 's/[^ ]*/\\&/g' | paste -sd '|' -)
cat <<EOF | sudo tee /etc/systemd/journald.d/ssh-filter.conf
[Journal]
Match=__CURSOR=~.*Failed password.*from (${REGEX}).*$
Result=ignore
EOF

sudo systemctl restart systemd-journald

Run this daily via cron to keep the filter in sync with fail2ban.


When to Use This Approach

  • You’re already forwarding sshd logs to a SIEM and want to reduce noise.
  • Your infrastructure has a predictable set of bad IP ranges (e.g., internal test networks).
  • You prefer a declarative, system‑wide solution over per‑user PAM tweaks.

If you need granular control per user or per host, consider PAM modules like pam_faildelay or pam_tally2. For global filtering, the journal method is the most straightforward.


Further Reading

See also