Configure Linux audit logging for security compliance and monitoring

Intermediate 35 min Apr 04, 2026 90 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Set up comprehensive audit logging with auditd daemon to track file system changes, process execution, and network connections for security compliance and forensic analysis.

Prerequisites

  • Root or sudo access
  • Basic understanding of Linux system administration
  • Email server configured for report notifications (optional)

What this solves

Linux audit logging provides comprehensive tracking of system activities including file access, process execution, network connections, and user actions. This tutorial configures auditd daemon for security compliance frameworks like PCI DSS, SOX, and HIPAA that require detailed activity monitoring and forensic capabilities.

Step-by-step installation

Update system packages

Start by updating your package manager to ensure you get the latest versions of audit tools.

sudo apt update && sudo apt upgrade -y
sudo dnf update -y

Install audit daemon and tools

Install auditd daemon and audit tools for log analysis and rule management.

sudo apt install -y auditd audispd-plugins audit
sudo dnf install -y audit audit-libs

Configure audit daemon settings

Configure the main auditd daemon settings for log location, size limits, and behavior when disk space is low.

# Log file location
log_file = /var/log/audit/audit.log

Log file size in MB (default 6MB)

max_log_file = 50

Number of log files to keep

num_logs = 10

Action when log file reaches max size

max_log_file_action = rotate

Action when disk space is low

space_left_action = email action_mail_acct = root@example.com

Action when disk is full

disk_full_action = suspend

Format for timestamp

log_format = enriched

Enable kernel audit messages

local_events = yes write_logs = yes

Priority for auditd process

priority_boost = 4

Flush audit records to disk

flush = incremental_async

Frequency of flushing

freq = 50

Set up file system monitoring rules

Create audit rules to monitor critical system files and directories for unauthorized changes.

# Monitor sensitive system files
-w /etc/passwd -p wa -k user_accounts
-w /etc/shadow -p wa -k user_accounts
-w /etc/group -p wa -k user_accounts
-w /etc/sudoers -p wa -k privilege_escalation
-w /etc/sudoers.d/ -p wa -k privilege_escalation

Monitor system configuration

-w /etc/ssh/sshd_config -p wa -k ssh_config -w /etc/hosts -p wa -k network_config -w /etc/hostname -p wa -k network_config -w /etc/resolv.conf -p wa -k network_config

Monitor boot and kernel

-w /boot/ -p wa -k boot_files -w /etc/grub.conf -p wa -k boot_files -w /etc/grub2.cfg -p wa -k boot_files

Monitor cron jobs

-w /etc/cron.allow -p wa -k cron_config -w /etc/cron.deny -p wa -k cron_config -w /etc/cron.d/ -p wa -k cron_config -w /etc/cron.daily/ -p wa -k cron_config -w /etc/cron.hourly/ -p wa -k cron_config -w /etc/cron.monthly/ -p wa -k cron_config -w /etc/cron.weekly/ -p wa -k cron_config -w /var/spool/cron/ -p wa -k cron_config

Monitor system logs

-w /var/log/auth.log -p wa -k auth_logs -w /var/log/secure -p wa -k auth_logs -w /var/log/messages -p wa -k system_logs

Configure process and system call monitoring

Set up rules to monitor process execution, privilege escalation, and system calls that could indicate malicious activity.

# Monitor privilege escalation
-a always,exit -F arch=b64 -S execve -F euid=0 -F auid>=1000 -F auid!=-1 -k privilege_escalation
-a always,exit -F arch=b32 -S execve -F euid=0 -F auid>=1000 -F auid!=-1 -k privilege_escalation

Monitor sudo usage

-a always,exit -F arch=b64 -S execve -F path=/usr/bin/sudo -k sudo_usage -a always,exit -F arch=b32 -S execve -F path=/usr/bin/sudo -k sudo_usage

Monitor process termination

-a always,exit -F arch=b64 -S kill -k process_termination -a always,exit -F arch=b32 -S kill -k process_termination

Monitor file permission changes

-a always,exit -F arch=b64 -S chmod,fchmod,fchmodat -F auid>=1000 -F auid!=-1 -k permission_changes -a always,exit -F arch=b32 -S chmod,fchmod,fchmodat -F auid>=1000 -F auid!=-1 -k permission_changes

Monitor file ownership changes

-a always,exit -F arch=b64 -S chown,fchown,fchownat,lchown -F auid>=1000 -F auid!=-1 -k ownership_changes -a always,exit -F arch=b32 -S chown,fchown,fchownat,lchown -F auid>=1000 -F auid!=-1 -k ownership_changes

Monitor file deletion

-a always,exit -F arch=b64 -S unlink,unlinkat,rename,renameat -F auid>=1000 -F auid!=-1 -k file_deletion -a always,exit -F arch=b32 -S unlink,unlinkat,rename,renameat -F auid>=1000 -F auid!=-1 -k file_deletion

Monitor kernel module operations

-w /sbin/insmod -p x -k kernel_modules -w /sbin/rmmod -p x -k kernel_modules -w /sbin/modprobe -p x -k kernel_modules -a always,exit -F arch=b64 -S init_module,delete_module -k kernel_modules -a always,exit -F arch=b32 -S init_module,delete_module -k kernel_modules

Set up network monitoring rules

Configure audit rules to track network connections and socket operations for security analysis.

# Monitor network connections
-a always,exit -F arch=b64 -S socket -F a0=2 -k network_connections
-a always,exit -F arch=b32 -S socket -F a0=2 -k network_connections
-a always,exit -F arch=b64 -S socket -F a0=10 -k network_connections
-a always,exit -F arch=b32 -S socket -F a0=10 -k network_connections

Monitor bind operations

-a always,exit -F arch=b64 -S bind -k network_bind -a always,exit -F arch=b32 -S bind -k network_bind

Monitor connect operations

-a always,exit -F arch=b64 -S connect -k network_connect -a always,exit -F arch=b32 -S connect -k network_connect

Monitor network configuration changes

-w /etc/network/interfaces -p wa -k network_config -w /etc/sysconfig/network-scripts/ -p wa -k network_config -w /etc/netplan/ -p wa -k network_config

Monitor firewall changes

-w /sbin/iptables -p x -k firewall_changes -w /sbin/ip6tables -p x -k firewall_changes -w /usr/sbin/ufw -p x -k firewall_changes -w /usr/sbin/firewall-cmd -p x -k firewall_changes

Configure immutable audit rules

Set the audit configuration as immutable to prevent tampering with audit rules during runtime.

# Make audit configuration immutable

This must be the last rule in the configuration

-e 2

Set up log rotation for audit logs

Configure logrotate to manage audit log files and prevent disk space issues.

/var/log/audit/*.log {
    daily
    rotate 30
    compress
    delaycompress
    missingok
    notifempty
    create 640 root root
    postrotate
        /sbin/service auditd restart 2> /dev/null || true
    endscript
}

Enable and start audit daemon

Enable auditd to start automatically on boot and start the service immediately.

sudo systemctl enable auditd
sudo systemctl start auditd
sudo systemctl status auditd

Load audit rules

Load the audit rules and verify they are active in the kernel.

sudo auditctl -R /etc/audit/rules.d/
sudo auditctl -l

Verify your setup

Test that audit logging is working correctly by checking service status and generating test events.

# Check auditd service status
sudo systemctl status auditd

Verify audit rules are loaded

sudo auditctl -s sudo auditctl -l | head -20

Check audit log location and permissions

ls -la /var/log/audit/

Generate test events

sudo touch /tmp/audit_test sudo chmod 777 /tmp/audit_test sudo rm /tmp/audit_test

Search for test events in audit logs

sudo ausearch -k file_deletion -ts recent sudo ausearch -k permission_changes -ts recent

Analyze audit logs

Use audit tools to search and analyze logged events for security monitoring and compliance reporting.

Search audit logs by event type

Use ausearch to find specific types of security events in the audit logs.

# Search for privilege escalation events
sudo ausearch -k privilege_escalation -ts today

Search for file permission changes

sudo ausearch -k permission_changes -ts today

Search for user account modifications

sudo ausearch -k user_accounts -ts today

Search for network connections

sudo ausearch -k network_connections -ts today

Search for sudo usage

sudo ausearch -k sudo_usage -ts today

Generate compliance reports

Create formatted reports from audit logs for compliance documentation and security analysis.

# Generate daily activity report
sudo aureport -ts today

Generate user activity summary

sudo aureport -u -ts today

Generate file access report

sudo aureport -f -ts today

Generate executable report

sudo aureport -x -ts today

Generate login report

sudo aureport -l -ts today

Set up automated compliance reporting

Create a script to generate daily audit reports and email them to security teams. This integrates well with encrypted backup strategies for long-term audit retention.

#!/bin/bash

Daily audit report generation

REPORT_DATE=$(date +%Y-%m-%d) REPORT_FILE="/tmp/audit-report-${REPORT_DATE}.txt" EMAIL="security@example.com" echo "Audit Report for ${REPORT_DATE}" > "${REPORT_FILE}" echo "===============================" >> "${REPORT_FILE}" echo "" >> "${REPORT_FILE}" echo "System Activity Summary:" >> "${REPORT_FILE}" sudo aureport -ts today >> "${REPORT_FILE}" echo "" >> "${REPORT_FILE}" echo "User Activity:" >> "${REPORT_FILE}" sudo aureport -u -ts today >> "${REPORT_FILE}" echo "" >> "${REPORT_FILE}" echo "Privilege Escalation Events:" >> "${REPORT_FILE}" sudo ausearch -k privilege_escalation -ts today >> "${REPORT_FILE}" 2>/dev/null || echo "No events found" >> "${REPORT_FILE}" echo "" >> "${REPORT_FILE}" echo "File Permission Changes:" >> "${REPORT_FILE}" sudo ausearch -k permission_changes -ts today >> "${REPORT_FILE}" 2>/dev/null || echo "No events found" >> "${REPORT_FILE}" echo "" >> "${REPORT_FILE}"

Send report via email

if command -v mail >/dev/null 2>&1; then cat "${REPORT_FILE}" | mail -s "Daily Audit Report - ${REPORT_DATE}" "${EMAIL}" fi

Clean up

rm -f "${REPORT_FILE}"
sudo chmod 755 /usr/local/bin/audit-report.sh

Schedule automated reports

Set up a cron job to run daily audit reports automatically. This works well with systemd timer configuration for more advanced scheduling.

sudo crontab -e
# Daily audit report at 6 AM
0 6   * /usr/local/bin/audit-report.sh
Warning: Once audit rules are set to immutable with "-e 2", you cannot modify them without rebooting. Use "-e 1" for testing and "-e 2" only in production.

Configure centralized audit logging

For enterprise environments, configure audit logs to be sent to a central logging server for aggregation and analysis. This integrates with ELK Stack deployments for comprehensive log management.

Configure audisp for log forwarding

Set up the audit dispatcher to forward logs to remote syslog servers.

active = yes
direction = out
path = builtin_syslog
type = builtin
args = LOG_INFO
format = string

Configure rsyslog for audit forwarding

Configure rsyslog to forward audit messages to a central log server.

# Forward audit logs to central server
$ModLoad imuxsock
$SystemLogSocketName /run/systemd/journal/syslog

Forward all audit logs to remote server

local6.* @@logserver.example.com:514

Also log locally

local6.* /var/log/audit-remote.log
sudo systemctl restart rsyslog

Common issues

SymptomCauseFix
auditd fails to startInvalid rule syntaxsudo auditctl -R /etc/audit/rules.d/ to test rules
Audit logs filling diskNo log rotation configuredConfigure logrotate and set max_log_file_action
Rules not loading on bootRules not in /etc/audit/rules.d/Move rules to rules.d directory and restart auditd
Cannot modify rulesAudit system set to immutableReboot system or remove "-e 2" rule
High CPU usageToo many audit rulesReduce rule scope and use specific file paths
Missing network eventsArchitecture-specific rules missingInclude both b32 and b64 rules for syscalls
Permission denied on logsIncorrect log file permissionssudo chmod 640 /var/log/audit/*

Next steps

Automated install script

Run this to automate the entire setup

Need help?

Don't want to manage this yourself?

We handle infrastructure security hardening for businesses that depend on uptime. From initial setup to ongoing operations.