我几乎讨厌为此提交一个主题,但我自己无法弄清楚。我正在运行 Federoa 17 服务器,并且尝试通过 rsyslog 将 iptables 中丢弃的数据包记录到单独的日志文件中,但它一直将它们发送到 /var/log/messages 。
我的防火墙脚本的片段:
#!/bin/bash
iptables -F
# My accepted rules would be here
iptables -A INPUT -j LOG --log-prefix "iptables: "
iptables -A FORWARD -j LOG --log-prefix "iptables: "
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT
iptables-save > /etc/sysconfig/iptables
service iptables restart
iptables -L -v
应该从 iptables 捕获消息的配置文件:
[root@fc17 ]# cat /etc/rsyslog.d/iptables.conf
:msg, startswith, "iptables: " /var/log/iptables.log
& ~
我的 rsyslog.conf 文件中的片段:
#### GLOBAL DIRECTIVES ####
# Include all config files in /etc/rsyslog.d/
$IncludeConfig /etc/rsyslog.d/*.conf
#### RULES ####
# I put this in here too to see if it would work; it doesn't
:msg, startswith, "iptables: " /var/log/iptables.log
& ~
# Log all kernel messages to the console.
# Logging much else clutters up the screen.
#kern.* /dev/console
# Log anything (except mail) of level info or higher.
# Don't log private authentication messages!
*.info;mail.none;authpriv.none;cron.none /var/log/messages
自从进行更改以来,我已经多次重新启动了 iptables 和 rsyslog,无论如何,它只会将丢弃的数据包从 iptables 记录到 /var/log/messages。
我听说在兼容模式下运行 rsyslog 会导致各种问题。这里可能是这种情况吗?以下是我系统上的运行选项:
[root@fc17 ]# ps -ef | grep rsyslog
root 3571 1 0 00:59 ? 00:00:00 /sbin/rsyslogd -n -c 5
startswith
比较运算符不起作用,因为当我检查日志时,msg
不是以iptables:
开头:
[root@localhost ~]# cat /etc/rsyslog.d/test.conf
:msg, startswith, "iptables:" /var/log/iptables.log
但是,
contains
比较运算符在我的 FC18 上有效:
[root@localhost ~]# cat /etc/rsyslog.d/test.conf
:msg, contains, "iptables:" /var/log/iptables.log
参考:Rsyslog 站点
您应该在指令部分的“/etc/rsyslogd.conf”中添加以下两行
$klogParseKernelTimestamp on
$klogKeepKernelTimestamp off
这将删除出现在每个内核消息开头的内核时间戳,例如以下日志中的“[6448.546951]”
Mar 31 14:36:14 localhost kernel: [ 6448.546951] iptables: IN=ppp0 OUT= MAC= SRC=
2019 年解决方案。在 Ubuntu18.04 上使用 rsyslogd 8.32.0 进行测试。
您仍然可以使用
startswith
,
[root@localhost ~]# cat /etc/rsyslog.d/test.conf
:msg, startswith, " iptables:" /var/log/iptables.log
通过更改
/etc/rsyslogd.conf
中的行
module(load="imklog" ParseKernelTimestamp="on" KeepKernelTimestamp="off")
我在 centos 6 上使用 rsyslogd 5.8.10,我的日志报告显示如下:
Aug 12 11:50:41 node2 kernel: [10256396.525411] IPTables-Dropped: IN=eth0 OUT= MAC=00:25:90:c3:05:40:00:24:13:10:8c:00:08:00 SRC=212.237.40.56 DST=37.153.1.29 LEN=45 TOS=0x00 PREC=0x00 TTL=244 ID=54321 PROTO=UDP SPT=45661 DPT=53413 LEN=25
我尝试使用以下方法禁用时间戳:
$klogParseKernelTimestamp on
$klogKeepKernelTimestamp off
但是显示:
8 月 12 日 11:50:22 node2 rsyslogd-3003:无效或未知的配置文件命令 - 您是否忘记加载模块? [尝试http://www.rsyslog.com/e/3003]
模块中有这个:
#### MODULES ####
$ModLoad imuxsock # provides support for local system logging (e.g. via logger command)
$ModLoad imklog # provides kernel logging support (previously done by rklogd)
#$ModLoad immark # provides --MARK-- message capability
提前谢谢您。