/var/log/iptables.log
, but continued to be duplicated in /var/log/messages
and /var/log/syslog
, which was very annoying and the task was incomplete. Finding a way not to duplicate messages in the system, I decided to publish the results. iptables -A INPUT -p ICMP --icmp-type 8 -j LOG --log-prefix "Ping detected: " iptables -A INPUT -p ICMP --icmp-type 8 -j ACCEPT
/var/log/messages
and /var/log/syslog
: kernel: [122972.300408] Ping detected: IN=eth0 OUT= MAC=00:64:d9:36:7b:d7:00:24:2d:a6:e2:43:08:91 SRC=xxx.xxx.xxx.xxx DST=xxx.xxx.xxx.xxx LEN=60 TOS=0x00 PREC=0x00 TTL=124 ID=23020 PROTO=ICMP TYPE=8 CODE=0 ID=33602 SEQ=2462
iptables -A INPUT -p ICMP --icmp-type 8 -j LOG --log-prefix "Iptables: Ping detected: " iptables -A INPUT -p ICMP --icmp-type 8 -j ACCEPT
/etc/rsyslog.d/iptables.conf
with the following content: echo ':msg, contains, "Iptables: " -/var/log/iptables.log' > /etc/rsyslog.d/iptables.conf echo '& ~' >> /etc/rsyslog.d/iptables.conf
& ~
- says that no further processing of the record should be done, so it will not fall into other log files."Iptables: "
is the same log-prefix - the criterion by which rsyslog decides to redirect the log to the desired file. The prefix could not be changed, but left as is - Ping detected
, but if the rule is not one, then it is more convenient to have a common prefix for all the rules, which was made./var/log/iptables.log
- the log file itself. /etc/init.d/rsyslog restart
/var/log/iptables.log
looks like this: kernel: [122972.300408] Iptables: Ping detected: IN=eth0 OUT= MAC=00:64:d9:36:7b:d7:00:24:2d:a6:e2:43:08:91 SRC=xxx.xxx.xxx.xxx DST=xxx.xxx.xxx.xxx LEN=60 TOS=0x00 PREC=0x00 TTL=124 ID=23020 PROTO=ICMP TYPE=8 CODE=0 ID=33602 SEQ=2462
# INVALID: iptables -A INPUT -m state --state INVALID -j LOG --log-prefix "Iptables: Invalid packet: " # INPUT , : iptables -A INPUT -m limit --limit 3/minute --limit-burst 3 -j LOG --log-prefix "Iptables: INPUT packet died: " # FORWARD , : iptables -A FORWARD -m limit --limit 3/minute --limit-burst 3 -j LOG --log-prefix "Iptables: FORWARD packet died: "
echo ':msg, contains, "Iptables: Invalid packet" -/var/log/iptables_invalid.log' > /etc/rsyslog.d/iptables_invalid.conf echo '& ~' >> /etc/rsyslog.d/iptables_invalid.conf echo ':msg, contains, "Iptables: INPUT" -/var/log/iptables_input.log' > /etc/rsyslog.d/iptables_input.conf echo '& ~' >> /etc/rsyslog.d/iptables_input.conf echo ':msg, contains, "Iptables: FORWARD" -/var/log/iptables_forward.log' > /etc/rsyslog.d/iptables_forward.conf echo '& ~' >> /etc/rsyslog.d/iptables_forward.conf
/etc/init.d/rsyslog restart
/etc/logrotate.d/iptables
file with the following contents. For one common log: /var/log/iptables.log { daily rotate 30 compress missingok notifempty sharedscripts }
/var/log/iptables_invalid.log { daily rotate 30 compress missingok notifempty } /var/log/iptables_input.log { daily rotate 30 compress missingok notifempty } /var/log/iptables_forward.log { daily rotate 30 compress missingok notifempty }
daily
- rotate dailyrotate 30
- save the last 30 files rotatedcompress
- compressmissingok
- missing file is not an errornotifempty
- do not process empty files logrotate -f /etc/logrotate.conf
Source: https://habr.com/ru/post/259169/
All Articles