fail2ban 不发送电子邮件

fail2ban not sending emails

我的 fail2 ban 和我的 s-nail 似乎分开工作,但不能一起工作。 以下命令成功发送电子邮件:

echo 'Message body' | mail --subject='A subject' mydestaddress@gmail.com 

我已经为 sshd 配置了 fail2ban 并对其进行了测试(iptables 有效)。我的 jail.local 在这些方面与 jail.conf 不同:

# for testing purposes
bantime  = 1m
findtime  = 10m
maxretry = 3

destemail = mydestaddress@gmail.com
sender = myaddress@gmail.com

mta = mail

[sshd]

# To use more aggressive sshd modes set filter parameter "mode" in jail.local:
# normal (default), ddos, extra or aggressive (combines all).
# See "tests/files/logs/sshd" or "filter.d/sshd.conf" for usage example and details.
#mode   = normal
enabled = true
port    = ssh # not changed
logpath = %(sshd_log)s # not changed
backend = %(sshd_backend)s # not changed

如果我改变 action = %(action_)saction = %(action_mw)s 在 jail.local 的 [default] 部分,我的 systemd 单元将在重启时失败。

此外,根据这个 bugreport 我还尝试修改 action.d/ 中的 mail.confmail-whois.confmail-buffered.conf 以使

mail -E 'set escape'

进入

mailx

但这并没有帮助,无论是否在 action.d 文件中进行此修改,服务都会崩溃。

我的 /var/log/fail2ban.log 没有提供信息,但是我的服务状态:

    $ sudo systemctl -l status fail2ban

× fail2ban.service - Fail2Ban Service
     Loaded: loaded (/usr/lib/systemd/system/fail2ban.service; enabled; vendor preset: disabled)
     Active: failed (Result: exit-code) since Sat 2021-10-30 14:27:03 CEST; 2min 3s ago
       Docs: man:fail2ban(1)
    Process: 33602 ExecStartPre=/bin/mkdir -p /run/fail2ban (code=exited, status=0/SUCCESS)
    Process: 33603 ExecStart=/usr/bin/fail2ban-server -xf start (code=exited, status=255/EXCEPTION)
   Main PID: 33603 (code=exited, status=255/EXCEPTION)
        CPU: 389ms

Oct 30 14:27:03 systemd[1]: Starting Fail2Ban Service...
Oct 30 14:27:03 systemd[1]: Started Fail2Ban Service.
Oct 30 14:27:03 fail2ban-server[33603]: 2021-10-30 14:27:03,877 fail2ban.configreader   [33603]: ERROR   Found no accessible config files for 'action.d/#sendmail-whois' under /etc/fail2ban
Oct 30 14:27:03 fail2ban-server[33603]: 2021-10-30 14:27:03,877 fail2ban.jailreader     [33603]: ERROR   Unable to read action '#sendmail-whois'
Oct 30 14:27:03 fail2ban-server[33603]: 2021-10-30 14:27:03,877 fail2ban.jailsreader    [33603]: ERROR   Errors in jail 'sshd'. Skipping...
Oct 30 14:27:03 fail2ban-server[33603]: 2021-10-30 14:27:03,896 fail2ban                [33603]: ERROR   Async configuration of server failed
Oct 30 14:27:03 systemd[1]: fail2ban.service: Main process exited, code=exited, status=255/EXCEPTION
Oct 30 14:27:03 systemd[1]: fail2ban.service: Failed with result 'exit-code'.

回答而不是编辑,因为它很重要: 服务崩溃的修复如下。

Found no accessible config files for 'action.d/#sendmail-whois' under /etc/fail2ban

这条消息让我相信设置

mta = mail

action_mwl = 计算 %(mta)s 时没有被正确考虑。我向 jail.local 添加了自定义操作:

action_custom,我将“%(mta)s”更改为纯“邮件”。现在服务将开始。不过还是没有邮件通​​知,还没解决

想通了。关键部分是这个,在邮件配置文件的注释中:

# Option:  actionban
# Notes.:  command executed when banning an IP. Take care that the
#          command is executed with Fail2Ban user rights.
# Tags:    See jail.conf(5) man page
# Values:  CMD

强调这一行:命令以 Fail2Ban 用户权限执行。 一直以来我都尝试用我自己的用户而不是 root 来发送邮件。

切换到 root (sudo su) 并尝试相同的邮件发送命令,我很快意识到我缺少 /etc/msmtprc,以及 ~/.mailrc~/.netrc (对于根用户).

添加这些很快解决了邮件发送问题。

一旦可能,将标记为已解决。