I recently worked on compliance-related requirements and installed the audit service on an operating system. One of the requirements was to archive audit logs regularly, so I looked into how to rotate audit logs by time rather than by size.
I found a Red Hat knowledge base article called How to implement audit log rotation with compression based on time instead of size. The notes below extract the parts that are actually needed.
Since auditd does not support date-based rotation directly, the first step is to disable its default size-based rotation behavior. Edit /etc/audit/auditd.conf and change:
1 | max_log_file_action = ROTATE |
to:
1 | max_log_file_action = ignore |
Then restart the service:
1 | systemctl restart auditd |
After this change, auditd will no longer rotate its own logs automatically. At that point, create a script under /etc/cron.daily, for example audit_log, with the following content:
1 |
|
Then make the script executable:
1 | chmod +x audit_log |
After that, cron can call it once per day, rename the rotated audit logs based on time, compress them, and keep only the most recent files.
If you want to test the behavior manually, just run:
1 | /etc/cron.daily/audit_log |
During the first few runs, rm may complain because there are not enough historical files yet. That is expected and can be ignored.