Iptables logging using NFLOG and ulogd2 on Debian Jessie

So, I’ve been hating for quite some time having my dmesg cluttered by iptables logs. Yesterday I had enough and decided to clean it up.

Here it is how to do it:

First of all, you have to replace -j LOG in your rules with -j NFLOG
this will stop iptables from logging to standard syslog and switch to sending log packets via multicast. You’ll have to assign a number from 0 to 2^16-1 to the nflog_netlink multicast group, the default is 1
Plus you can associate a label to the logging rule using –nflog-prefix.
The iptable rule should resemble this:

[bash] (blablabla…) -j NFLOG –nflog-group 32 –nflog-prefix iptables[/bash]

Because you won’t be using syslog anymore, you can’t just rely on rsyslogd anymore, you will need to install ulogd2. It is not mandatory, you can use whatever you like to fetch data from the multicast socket, including wireshark or writing your own code. Since I just want a different logfile, ulogd2 is perfectly fine.

[bash]sudo apt-get install ulogd[/bash]

Open up /etc/ulogd.conf and check that it contains these lines:

[bash]plugin=”/usr/lib/x86_64-linux-gnu/ulogd/ulogd_output_LOGEMU.so”[/bash] (Mine’s a 64 bit install, it will probably look slightly different on 32 bit)

[bash]stack=log1:NFLOG,base1:BASE,ifi1:IFINDEX,ip2str1:IP2STR,print1:PRINTPKT,emu1:LOGEMU

Make sure no other “stack” lines are uncommented

# Logging of system packet through NFLOG
[log1]
# netlink multicast group (the same as the iptables –nflog-group param)
# Group O is used by the kernel to log connection tracking invalid message
group=32

[/bash]

Make sure group=n exactly matches the –nflog-group n rule in iptables! And make sure this is the only [log1] section in the configuration file

[bash]

[emu1]
file=”/var/log/iptables.log”
#sync=1

[/bash]

If you want immediate logging, albeit slower, just uncomment sync=1, otherwise you might observe a small delay before the lines apper in the logfile.

Restart ulogd

[bash]sudo service ulogd restart[/bash]

Voilà!

Oh and one last thing:

If your log remains empty, make sure the NFLOG rules come before the DROP rules