如果不手动配置,Fail2ban将从/etc/Fail2ban目录加载配置文件。您可以在那里找到许多名为*.conf的文件。
在首次启动fail2ban服务之前,您应该进行一些适合您系统的配置。至少应该为你想用fail2ban保护的服务启用Fail2Ban。
[提问]:我应该直接在jail.conf和fail2ban.conf中进行配置吗?
[回答]:不建议!!!您应该避免更改fail2ban安装时创建的.conf文件。您应该创建扩展名为.local的新配置文件
由于这些库存文件可能会被软件包升级覆盖,或者因为您的更改可能与某些未来版本不兼容,您不应该在原地编辑它。
因此,要设置jail配置,不要更改jail.conf。要自定义某些过滤器配置,不要修改filter.conf。相反,请创建一个扩展名为.local的新文件,并仅插入要覆盖的设置或要附加到默认配置的设置。例如,jail.local中定义的任何值都将覆盖jail.conf中相同部分中的值(例如[DEFAULT])。
例如,如果原始.conf文件包含:
[DEFAULT]
logpath = /path/to/log
[section1]
logpath = /other/path
enabled = true
[section2]
enabled = true
您自己将创建一个.local文件,其中包含:
[DEFAULT]
logpath = /my-path/to/log
The value of parameter logpath in section1 will be still /other/path.
But value of parameter logpath in section2 will be changed to /my-path/to/log (because it was not specified in the section itself, so the new default value will be used).
[提问] 要让 fail2ban 保护某个服务,需要什么样的配置?
[回答] 您应当创建一个 jail.local 文件,并至少启用相应的监禁(默认情况下所有监禁都是禁用的),或者覆盖您想要更改的默认设置,甚至创建您自己的监禁(和/或)过滤器(如果这些在 fail2ban 的默认配置中是不存在的)。
例如,如果您打算监控在 sshd 和 nginx 中发生的授权失败情况,但您的 nginx 实例的 error.log 被配置为 /var/log/my-nginx/error.log ,那么除了在 [nginx] 部分启用它之外,您还应相应地设置 logpath 参数。
因此,您的 jail.local 文件内容如下:
[nginx]
logpath = /var/log/my-nginx/error.log
enabled = true
[sshd]
enabled = true
[Definition]
logtarget = /var/log/fail2ban.log
socket = /var/run/fail2ban/fail2ban.sock
pidfile = /var/run/fail2ban/fail2ban.pid
dbfile = /var/run/fail2ban/fail2ban.sqlite3
You can also control resp. configure another optional configurations parameters, like ignoreip, etc.
[提问] 如何查看fail2ban将要使用的当前(合并后的)配置
[回答] 可以使用以下命令转储当前配置(fail2ban在启动时加载的所有参数):
# dump parameters:
fail2ban-client -d
# verbose: output config files will be loaded and dump parameters:
fail2ban-client -vd
fail2ban-client -vvd
[提问] fail2ban配置已更改,如何生效更改后的配置
[回答] 您应该执行fail2ban-client reload
(在0.10之前的版本中使用fail2ban-client restart
)。
您还可以使用fail2ban-client 服务器通信协议在运行时单独获取和设置参数。例如:
fail2ban-client set pam-generic logencoding UTF-8
fail2ban-client set nginx findtime 10m
[提问] 我应该如何正确地修改日志文件的位置,而不是在监狱设置或搞乱主.conf文件?
[回答] 要修改默认的日志文件位置,您应该创建一个.local文件paths-common.conf或paths-debian.conf(无论您在jail.local中使用哪个文件),并且只在.local文件中进行更改,这可以使其保持良好的jail设置结构,并避免在更新Fail2Ban时出现问题
创建.local文件
请不要复制:
用您喜欢的编辑器创建和编辑它,而不是直接复制一份
现在,如果你想要一个Nginx过滤器来读取多个网站的所有Nginx访问日志
在文件jail.local中编辑内容:
[nginx]
logpath = /var/log/nginx/*access*.log
enabled = true
或者不要在监狱里使用 | Or instead of using in your jail:
在文件paths-common.local 或者 paths-debian.local (取决于那个文件你在使用) 里面添加nginx_access_log 配置,参考下方:
[DEFAULT]
nginx_access_log = /var/log/nginx/*access*.log
Then in your jail you would rather use
logpath = %(nginx_access_log)s
https://blog.xqlee.com/article/2504241941529838.html