Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Linux auditd Command

Linux auditd Command

Linux Command Reference Linux Command Reference


auditd is the auditing daemon on Linux systems and a core component of the Linux auditing framework. The main function of auditd is to monitor and record system activities, including:

These audit logs are very important for system security monitoring, compliance checks, and troubleshooting.


Core components of auditd

The auditd daemon

A continuously running daemon responsible for collecting and storing audit events.

The auditctl tool

A command-line tool for configuring audit rules and controlling the auditing system.

The ausearch tool

A command-line tool for querying audit logs.

The aureport tool

Generates summary reports of audit logs.


Installing and starting auditd

Install auditd

On most Linux distributions, auditd is usually pre-installed. If you need to install it manually:

Example

Ubuntu/Debian

sudo

apt-get install

auditd

CentOS/RHEL

sudo

yum install

audit

Start and enable the auditd service

Example

Start the service

sudo

systemctl start auditd

Enable startup on boot

sudo

systemctl

enable

auditd

Check the service status

sudo

systemctl status auditd


auditd configuration file

The main configuration file of auditd is located at /etc/audit/auditd.conf, containing the following important parameters:

Parameters Description Default value
log_file Audit log file path /var/log/audit/audit.log
max_log_file Maximum size of a single log file (MB) 8
num_logs Number of log files to retain 5
flush Log writing mode INCREMENTAL
freq If flush=INCREMENTAL, how often to synchronize 20

After modifying the configuration, you need to restart the service:

sudo systemctl restart auditd


Details of the auditctl command

auditctl is the main tool for configuring audit rules.

Basic Syntax

auditctl [选项] [规则]

Common options

Option Description
-l List all current rules
-D Delete all rules
-s Display the audit system status
-R Load rules from a file

Rule types

File system rules: Monitor file/directory access

Example

Monitor read, write, and attribute changes of the /etc/passwd file

auditctl

-w

/

etc

/

passwd

-p

rwxa

-k

passwd_access

System call rules: Monitor specific system calls

Example

Monitor all commands using sudo

auditctl

-a

always,

exit

-F

arch

=b64

-S

execve

-F

path

=

/

usr

/

bin

/

sudo

-k

sudo_cmds

User rules: Monitor specific user behavior

Example

Monitor file deletion by users with UID greater than 500

auditctl

-a

always,

exit

-S

unlink

-S

unlinkat

-S

rename

-S

renameat

-F

auid

&

gt;=

500

-F

auid

!

=

4294967295

-k

delete_files


Audit log analysis

The ausearch command

Used to query audit logs.

Example

Find logs with a specific keyword

ausearch

-k

passwd_access

Find logs from a specific time

ausearch

-ts

today

ausearch

-ts

10

:00:00

-te

11

:00:00

Find logs for a specific user

ausearch

-ua

1000

The aureport command

Generates summary reports of audit logs.

Example

Generate a user login report

aureport

-l

Generate a file access report

aureport

-f

Generate a summary report of all events

aureport

--summary


Practical Application Examples

Example 1: Monitor sensitive files

Example

Monitor the /etc/shadow file

auditctl

-w

/

etc

/

shadow

-p

wa

-k

shadow_mod

View related logs

ausearch

-k

shadow_mod

|

less

Example 2: Monitor user privilege escalation

Example

Monitor all commands using sudo or su

auditctl

-a

always,

exit

-F

arch

=b64

-S

execve

-F

path

=

/

usr

/

bin

/

sudo

-k

priv_esc

auditctl

-a

always,

exit

-F

arch

=b64

-S

execve

-F

path

=

/

usr

/

bin

/

su

-k

priv_esc

Generate a privilege escalation report

aureport

--start

today

--event

--summary

-i

|

grep

priv_esc

Example 3: Monitor SSH logins

Example

Monitor successful and failed SSH logins

auditctl

-a

always,

exit

-F

arch

=b64

-S

execve

-F

path

=

/

usr

/

sbin

/

sshd

-k

sshd_login

View SSH login records

ausearch

-k

sshd_login

|

grep

'acct="user"'

|

grep

'res=success'


Best Practices

Set up log rotation properly: Ensure logs do not fill up the disk

Example

Edit /etc/audit/auditd.conf

max_log_file =

50

num_logs =

10

Manage audit rules centrally: Store rules in a file

Example

Create the rule file /etc/audit/rules.d/my.rules

-w

/

etc

/

passwd

-p

wa

-k

passwd_changes

-w

/

etc

/

group

-p

wa

-k

group_changes

Load rules

auditctl

-R

/

etc

/

audit

/

rules.d

/

my.rules

Review logs regularly: Set up a cron job to analyze logs periodically

Example

Generate a report daily and send it to the administrator

0

0

/

usr

/

sbin

/

aureport

--summary

--start

yesterday

--end

now

|

mail

-s

"Daily Audit Report"

admin

@

example.com

Protect audit logs: Prevent logs from being tampered with

Example

chmod

600

/

var

/

log

/

audit

/

audit.log

chown

root:root

/

var

/

log

/

audit

/

audit.log


Troubleshooting common problems

Problem 1: The auditd service fails to start

Solution:

  1. Check the configuration file syntax:auditd -f /etc/audit/auditd.conf
  2. View the system logs:journalctl -u auditd

Problem 2: No audit logs are generated

Solution:

  1. Confirm the service is running:systemctl status auditd
  2. Check whether the rules are loaded:auditctl -l
  3. Verify kernel support:grep "audit" /boot/config-$(uname -r)

Problem 3: Log files are too large

Solution:

  1. Adjust the log size and count: modify /etc/audit/auditd.conf The max_log_file and num_logs
  2. Set up log compression: add compress = yes to the configuration file

Summary

auditd is a powerful auditing tool for Linux systems; with proper configuration, it can:

Mastering auditd is essential for system administrators and security professionals; it helps you better understand and protect your Linux system.


Linux Command Reference Linux Command Reference