|
| 1 | +title: fail2ban with systemd journal |
| 2 | +date: 2025-02-05 |
| 3 | +category: linux |
| 4 | +tags: linux, aws, security |
| 5 | + |
| 6 | +The [fail2ban](https://github.com/fail2ban/fail2ban) package installed |
| 7 | +on the Debian version offered by AWS has a default configuration that |
| 8 | +depends on reading log files. This doesn't work when |
| 9 | +e.g. [sshd](https://www.openssh.com/) writes failed login attempts to |
| 10 | +the systemd journal rather than the traditional `/var/log/auth.log`. |
| 11 | + |
| 12 | +The error looks like this: |
| 13 | +```text |
| 14 | +# systemctl status fail2ban |
| 15 | +× fail2ban.service - Fail2Ban Service |
| 16 | + Loaded: loaded (/lib/systemd/system/fail2ban.service; enabled; preset: enabled) |
| 17 | + Active: failed (Result: exit-code) since Wed 2025-02-05 19:43:14 UTC; 2s ago |
| 18 | + Duration: 82ms |
| 19 | + Docs: man:fail2ban(1) |
| 20 | + Process: 7698 ExecStart=/usr/bin/fail2ban-server -xf start (code=exited, status=255/EXCEPTION) |
| 21 | + Main PID: 7698 (code=exited, status=255/EXCEPTION) |
| 22 | + CPU: 80ms |
| 23 | +
|
| 24 | +systemd[1]: Started fail2ban.service - Fail2Ban Service. |
| 25 | +.. |
| 26 | +fail2ban-server[8032]: 2025-02-05 19:51:15,947 fail2ban [8032]: ERROR Failed during configuration: Have not found any log file for sshd jail |
| 27 | +.. |
| 28 | +systemd[1]: fail2ban.service: Main process exited, code=exited, status=255/EXCEPTION |
| 29 | +systemd[1]: fail2ban.service: Failed with result 'exit-code'. |
| 30 | +``` |
| 31 | + |
| 32 | +The fix is simple, though, just add the following to the enabled jail, |
| 33 | +or set it in `/etc/fail2ban/jail.conf` to apply this to all jails: |
| 34 | + |
| 35 | +```text |
| 36 | +# vim /etc/fail2ban/jail.d/defaults-debian.conf |
| 37 | +``` |
| 38 | + |
| 39 | +And add the line with `backend`: |
| 40 | +```conf |
| 41 | +[sshd] |
| 42 | +enabled = true |
| 43 | +backend = systemd |
| 44 | +``` |
| 45 | + |
| 46 | +Now, restart `fail2ban` and check its status: |
| 47 | +```text |
| 48 | +# systemctl restart fail2ban |
| 49 | +# systemctl status fail2ban |
| 50 | +● fail2ban.service - Fail2Ban Service |
| 51 | + Loaded: loaded (/lib/systemd/system/fail2ban.service; enabled; preset: enabled) |
| 52 | + Active: active (running) since Wed 2025-02-05 19:52:34 UTC; 9min ago |
| 53 | + Docs: man:fail2ban(1) |
| 54 | + Main PID: 8041 (fail2ban-server) |
| 55 | + Tasks: 5 (limit: 1107) |
| 56 | + Memory: 16.3M |
| 57 | + CPU: 458ms |
| 58 | + CGroup: /system.slice/fail2ban.service |
| 59 | + └─8041 /usr/bin/python3 /usr/bin/fail2ban-server -xf start |
| 60 | +
|
| 61 | +systemd[1]: Started fail2ban.service - Fail2Ban Service. |
| 62 | +.. |
| 63 | +fail2ban-server[8041]: Server ready |
| 64 | +``` |
| 65 | + |
| 66 | +Finally, check `fail2ban` itself to see that the `sshd` jail is |
| 67 | +working: |
| 68 | + |
| 69 | +```text |
| 70 | +# fail2ban-client status sshd |
| 71 | +Status for the jail: sshd |
| 72 | +|- Filter |
| 73 | +| |- Currently failed: 0 |
| 74 | +| |- Total failed: 0 |
| 75 | +| `- Journal matches: _SYSTEMD_UNIT=sshd.service + _COMM=sshd |
| 76 | +`- Actions |
| 77 | + |- Currently banned: 0 |
| 78 | + |- Total banned: 0 |
| 79 | + `- Banned IP list: |
| 80 | +``` |
| 81 | + |
| 82 | +As you can see, `fail2ban` is keeping tabs on failed ssh login |
| 83 | +attempts and will put failed IPs in jail. Happy banning! |
0 commit comments