Skip to content

How to Log Packets with iptables

DodaTech Updated 2026-06-24 2 min read

Logging iptables packets helps debug firewall rules and detect attacks. Adding LOG rules before DROP rules captures valuable information. This guide walks through the specific troubleshooting steps to diagnose and resolve iptables logging issues.

Before You Begin

Before you begin, be sure to have the following in place:

  • A Linux server with the relevant software installed
  • Access to the command line interface
  • Appropriate permissions (root or sudo)

Quick Fix

Wrong

iptables -A INPUT -j DROP (no logging)

Wrong: Dropping packets without logging

iptables -A INPUT -j LOG --log-prefix "iptables: " --log-level 4

Right: Logging dropped packets before the DROP rule

Output

Jun 24 12:00:00 hostname kernel: iptables: IN=eth0 OUT= MAC=... SRC=10.0.0.1 DST=...

Prevention

To avoid future issues, follow these best practices:

  • Place LOG rules immediately before DROP or REJECT rules
  • Use descriptive --log-prefix for easy grep filtering
  • Limit log rate with -m limit --limit 5/min to avoid log flooding
  • Use --log-level 4 (warning) for standard logging
  • Monitor logs with journalctl -kf or tail -f /var/log/kern.log

DodaTech Tools

For further assistance with any of the above issues, consider using DodaTech consulting services or DodaTech tutorials for more in-depth guidance.

Common Mistakes with log

  1. Using head and tail instead of pattern matching, causing runtime errors on empty lists
  2. Forgetting that lazy evaluation defers computation until the value is forced, causing space leaks with unevaluated thunks
  3. Using return to exit a function early instead of wrapping a pure value in the monad

These mistakes appear frequently in real-world IPTABLES code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.

Practice Exercise

Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.

This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.

FAQ

Where do iptables log messages appear?|||iptables logs appear in kernel logs: /var/log/kern.log, /var/log/messages, or via journalctl -k.
How do I avoid filling the disk with iptables logs? Add rate limiting with -m limit --limit 5/minute --limit-burst 10 to the LOG rule. Only log dropped packets, not accepted ones.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro