如何配置 syslog.conf 文件,以在单独的文件中记录 iptables 消息?

Abi*_*bid 11 linux iptables syslog

如何配置/etc/syslog.conf文件以将日志信息保存iptables在特定文件中。

我想单独保存这些信息,以便我可以轻松快速地提取我想要的内容。

slm*_*slm 13

系统日志

在手册页中查看iptables. 它显示了一个名为的目标LOG,它可以做你想做的事。

例子

  1. 将日志级别设置为LOG4。

    # DROP everything and Log it
    iptables -A INPUT -j LOG --log-level 4
    iptables -A INPUT -j DROP
    
    Run Code Online (Sandbox Code Playgroud)
  2. 配置syslog.conf为将这些消息写入单独的文件。

    # /etc/syslog.conf
    kern.warning     /var/log/iptables.log
    
    Run Code Online (Sandbox Code Playgroud)
  3. 重新启动 syslogd。

    Debian/Ubuntu

    $ sudo /etc/init.d/sysklogd restart
    
    Run Code Online (Sandbox Code Playgroud)

    Fedora/CentOS/RHEL

    $ sudo /etc/init.d/syslog restart
    
    Run Code Online (Sandbox Code Playgroud)

注意:这种记录方法称为固定优先级。它们是数字或名称(1,2,3,4,..)或(DEBUG、WARN、INFO 等)。

日志

如果您正在使用rsyslog,您可以创建一个基于属性的过滤器,如下所示:

# /etc/rsyslog.conf
:msg, contains, "NETFILTER"       /var/log/iptables.log
:msg, contains, "NETFILTER"     ~
Run Code Online (Sandbox Code Playgroud)

然后将 thils 开关添加到您要记录的 iptables 规则中:

–log-prefix NETFILTER
Run Code Online (Sandbox Code Playgroud)

作为替代方案,您还可以使用这种类型的属性过滤器记录消息:

:msg, startswith, "iptables: " -/var/log/iptables.log
& ~
:msg, regex, "^\[ *[0-9]*\.[0-9]*\] iptables: " -/var/log/iptables.log
& ~
Run Code Online (Sandbox Code Playgroud)

注意:第二种方法不需要对iptables.

参考


Pet*_*ter 6

这假设您的防火墙已经生成日志,任何正常的防火墙都应该这样做。对于某些示例,它需要可识别的消息,例如 slm 示例中的“NETFILTER”。

在 rsyslog.d 中创建一个文件

vim /etc/rsyslog.d/10-firewall.conf
Run Code Online (Sandbox Code Playgroud)

这在 CentOS 7 中有效。除了寻找 IN 和 OUT 之外,我不知道如何验证它来自防火墙...... CentOS 很奇怪。除非下一个版本不起作用,否则不要使用它。

# into separate file and stop their further processing
if  ($msg contains 'IN=' and $msg contains 'OUT=') \
then {
    -/var/log/firewall
    & ~
}
Run Code Online (Sandbox Code Playgroud)

这适用于 CentOS 7 并检查消息内容(用 -j LOG 规则消息中的任何内容替换“Shorewall”):

# into separate file and stop their further processing
if  ($msg contains 'Shorewall') and \
    ($msg contains 'IN=' and $msg contains 'OUT=') \
then {
    -/var/log/firewall
    & ~
}
Run Code Online (Sandbox Code Playgroud)

这适用于其他人(Ubuntu、Debian、openSUSE)。这是最好的方法。不搜索消息中的字符串:

# into separate file and stop their further processing
if  ($syslogfacility-text == 'kern') and \\
($msg contains 'IN=' and $msg contains 'OUT=') \\
then    -/var/log/firewall
    &   ~
Run Code Online (Sandbox Code Playgroud)

这是默认的 openSUSE 机器所具有的(我认为每个发行版都应该有但没有)(区别似乎只是“停止”而不是“&〜”;并非所有系统都支持这两种语法):

if  ($syslogfacility-text == 'kern') and \
    ($msg contains 'IN=' and $msg contains 'OUT=') \
then {
    -/var/log/firewall
    stop
}
Run Code Online (Sandbox Code Playgroud)

对于上述所有内容,也不要忘记 logrotate.d 文件:

vim /etc/logrotate.d/firewall
Run Code Online (Sandbox Code Playgroud)

包含:

/var/log/firewall {
    rotate 7
    size 500k
    postrotate
        # before using this, run the command yourself to make sure 
        # it is right... the daemon name may vary
        /usr/bin/killall -HUP rsyslogd
    endscript
}
Run Code Online (Sandbox Code Playgroud)