When you need to troubleshoot firewall issues or review security events, having all iptables logs in their own dedicated file makes finding relevant information much easier than searching through general system logs.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/imthenachoman/How-To-Secure-A-Linux-Server/llms.txt
Use this file to discover all available pages before exploring further.
Why Use a Separate Log File
By default, iptables logs are mixed with other system logs in/var/log/syslog or /var/log/messages. Separating them provides:
- Easier troubleshooting: Quickly find firewall-related events without filtering through unrelated logs
- Better organization: Keep security logs separate from general system logs
- Simplified analysis: Use log analysis tools more effectively on firewall-specific data
- Cleaner monitoring: Monitor firewall activity without noise from other services
Configuration Steps
Add Log Prefix to Firewall Rules
First, configure your firewall to prefix all log entries with a unique string. This allows rsyslog to identify and route iptables messages.If you’re using iptables directly, add
--log-prefix "[IPTABLES] " to your logging rules.If you followed the PSAD installation steps in this guide, you’ve already configured this prefix in step 4 of the PSAD setup.
Configure rsyslog
Create the file This tells rsyslog to:Quick command:
/etc/rsyslog.d/10-iptables.conf with the following content:- Match log messages containing
[IPTABLES] - Write them to
/var/log/iptables.log - Stop processing (prevent duplicate entries in other logs)
If you expect high-volume firewall logging, prefix the filename with a
- to omit syncing after every write:Update PSAD Configuration
Since firewall messages are now in a different file, update PSAD to use the new location.Edit Quick command:
/etc/psad/psad.conf and set the IPT_SYSLOG_FILE parameter:Restart Services
Apply the changes by restarting PSAD and rsyslog:Alternatively, you can reboot the server to ensure all changes take effect.
Configure Log Rotation
Prevent the iptables log from consuming too much disk space by configuring log rotation.Create This configuration:
/etc/logrotate.d/iptables with the following content:- Rotates logs daily
- Keeps 7 days of logs
- Compresses old logs
- Handles missing log files gracefully
- Signals rsyslog after rotation
Verifying the Configuration
After completing the setup, verify that iptables logs are being written to the new file:Customizing the Log Prefix
If you want to use a different log prefix instead of[IPTABLES]:
- Update your firewall rules to use your custom prefix
- Update the rsyslog configuration in
/etc/rsyslog.d/10-iptables.confto match your prefix - Restart rsyslog
Remember to update the prefix in all places where it’s referenced, including firewall rules, rsyslog configuration, and PSAD configuration.