Set up centralized logging with rsyslog and logrotate for security events

Intermediate 45 min Apr 14, 2026 240 views
Ubuntu 24.04 Debian 12 AlmaLinux 9 Rocky Linux 9

Configure a centralized rsyslog server to collect security events from multiple systems, implement automated log rotation with logrotate, and set up filtering and alerting for critical security incidents across your infrastructure.

Prerequisites

  • Multiple Linux servers for centralized logging
  • Email system configured for alerts
  • Network connectivity between servers
  • Sufficient disk space for log storage

What this solves

Centralized logging consolidates security events from multiple servers into a single location, making it easier to monitor, analyze, and respond to security incidents. This tutorial shows you how to configure rsyslog for secure log forwarding, implement automated log rotation to manage disk space, and set up filtering and alerting for critical security events.

Step-by-step configuration

Update system packages

Start by updating your package manager to ensure you have the latest security patches and package versions.

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

Install rsyslog and dependencies

Install rsyslog and necessary packages for centralized logging. Most systems have rsyslog pre-installed, but we'll ensure all required components are available.

sudo apt install -y rsyslog rsyslog-gnutls logrotate mailutils
sudo dnf install -y rsyslog rsyslog-gnutls logrotate mailx

Configure rsyslog server

Configure the central rsyslog server to receive logs from remote clients. This configuration enables UDP and TCP reception on port 514 with security filtering.

# Enable UDP and TCP reception
module(load="imudp")
input(type="imudp" port="514")

module(load="imtcp")
input(type="imtcp" port="514")

Create directories for remote logs

$CreateDirs on $FileOwner syslog $FileGroup adm $FileCreateMode 0640 $DirCreateMode 0755

Template for remote log file organization

$template RemoteHost,"/var/log/remote/%HOSTNAME%/%$YEAR%-%$MONTH%-%$DAY%.log"

Template for security events

$template SecurityEvents,"/var/log/security/%HOSTNAME%/security-%$YEAR%-%$MONTH%-%$DAY%.log"

Security event filtering

:msg, contains, "authentication failure" ?SecurityEvents :msg, contains, "Failed password" ?SecurityEvents :msg, contains, "Invalid user" ?SecurityEvents :msg, contains, "sudo:" ?SecurityEvents :msg, contains, "su:" ?SecurityEvents :msg, contains, "COMMAND=" ?SecurityEvents & stop

General remote logging

if $fromhost-ip != '127.0.0.1' then ?RemoteHost & stop

Local logging continues normally

.;auth,authpriv.none /var/log/syslog authpriv.* /var/log/auth.log

Create log directories

Create the directory structure for centralized logs with proper ownership and permissions for security.

sudo mkdir -p /var/log/remote /var/log/security
sudo chown syslog:adm /var/log/remote /var/log/security
sudo chmod 755 /var/log/remote /var/log/security
Never use chmod 777. It gives every user on the system full access to your log files. Instead, use specific ownership and minimal permissions for security.

Configure firewall rules

Open the necessary firewall ports for rsyslog communication. Use specific rules instead of disabling the firewall.

sudo ufw allow from 203.0.113.0/24 to any port 514
sudo ufw reload
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" port protocol="udp" port="514" accept'
sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="203.0.113.0/24" port protocol="tcp" port="514" accept'
sudo firewall-cmd --reload

Configure rsyslog client systems

Configure client systems to forward logs to the central server. Add this configuration to each client system you want to monitor.

# Forward all logs to central server
.                             @@203.0.113.10:514

Forward security events with high priority

authpriv.* @@203.0.113.10:514 auth.* @@203.0.113.10:514

Local logging continues

& /var/log/messages & /var/log/auth.log

Restart rsyslog services

Restart rsyslog on both server and client systems to apply the new configuration.

sudo systemctl restart rsyslog
sudo systemctl enable rsyslog
sudo systemctl status rsyslog

Configure logrotate for centralized logs

Set up automated log rotation to manage disk space and maintain log history. This prevents log files from consuming all available storage.

/var/log/remote//.log {
    daily
    rotate 30
    compress
    delaycompress
    missingok
    notifempty
    create 0640 syslog adm
    sharedscripts
    postrotate
        /usr/lib/rsyslog/rsyslog-rotate
    endscript
}

/var/log/security//.log {
    daily
    rotate 90
    compress
    delaycompress
    missingok
    notifempty
    create 0640 syslog adm
    sharedscripts
    postrotate
        /usr/lib/rsyslog/rsyslog-rotate
        # Send alert email for security log rotation
        echo "Security logs rotated on $(hostname) at $(date)" | mail -s "Security Log Rotation" admin@example.com
    endscript
}

Set up security event alerting

Create a script to monitor security events and send alerts when critical events are detected.

#!/bin/bash

Security event monitoring script

LOG_DIR="/var/log/security" ALERT_EMAIL="admin@example.com" TEMP_FILE="/tmp/security-alerts.tmp" LAST_CHECK_FILE="/var/lib/rsyslog/last-security-check"

Create last check file if it doesn't exist

if [ ! -f "$LAST_CHECK_FILE" ]; then echo "$(date -d '1 minute ago' '+%Y-%m-%d %H:%M:%S')" > "$LAST_CHECK_FILE" fi LAST_CHECK=$(cat "$LAST_CHECK_FILE") CURRENT_TIME=$(date '+%Y-%m-%d %H:%M:%S')

Find recent security events

find "$LOG_DIR" -name "*.log" -newer "$LAST_CHECK_FILE" | while read -r logfile; do # Check for critical security events grep -E "(authentication failure|Failed password|Invalid user|sudo: .* : TTY=|COMMAND=)" "$logfile" | \ while IFS= read -r line; do echo "$logfile: $line" >> "$TEMP_FILE" done done

Send alert if events found

if [ -s "$TEMP_FILE" ]; then { echo "Security events detected between $LAST_CHECK and $CURRENT_TIME:" echo "" cat "$TEMP_FILE" echo "" echo "Please review these events and take appropriate action." } | mail -s "[SECURITY ALERT] Events detected on $(hostname)" "$ALERT_EMAIL" # Log the alert logger -t security-monitor "Security alert sent to $ALERT_EMAIL" fi

Update last check time

echo "$CURRENT_TIME" > "$LAST_CHECK_FILE"

Cleanup

rm -f "$TEMP_FILE"

Set script permissions and create directories

Make the security monitoring script executable and create required directories with proper ownership.

sudo chmod 755 /usr/local/bin/security-monitor.sh
sudo mkdir -p /var/lib/rsyslog
sudo chown syslog:adm /var/lib/rsyslog
sudo chmod 755 /var/lib/rsyslog

Configure automated monitoring with cron

Set up a cron job to run the security monitoring script every 5 minutes for near real-time alerting.

sudo crontab -e

Add this line to the crontab:

# Security event monitoring every 5 minutes
/5    * /usr/local/bin/security-monitor.sh >/dev/null 2>&1

Test logrotate configuration

Test the logrotate configuration to ensure it works correctly before relying on automated rotation.

sudo logrotate -d /etc/logrotate.d/rsyslog-remote
sudo logrotate -f /etc/logrotate.d/rsyslog-remote

Verify your setup

Test the centralized logging system to ensure logs are being received and processed correctly.

# Check rsyslog status
sudo systemctl status rsyslog

Test log forwarding from client

logger -p auth.info "Test security log message from $(hostname)"

Verify logs are received on server

sudo tail -f /var/log/remote/*/$(date +%Y-%m-%d).log

Check security log filtering

sudo tail -f /var/log/security/*/security-$(date +%Y-%m-%d).log

Test failed authentication (generates security event)

sudo su - nonexistentuser

Verify logrotate is working

sudo logrotate -v /etc/logrotate.d/rsyslog-remote

Check disk usage of log directories

sudo du -sh /var/log/remote /var/log/security

Common issues

SymptomCauseFix
Logs not being receivedFirewall blocking port 514Configure firewall rules: sudo ufw allow from trusted_network to any port 514
Permission denied writing logsIncorrect directory ownershipFix ownership: sudo chown syslog:adm /var/log/remote /var/log/security
Logrotate not workingMissing or incorrect permissionsCheck script permissions: sudo chmod 755 /usr/lib/rsyslog/rsyslog-rotate
Security alerts not sentMail system not configuredInstall and configure mail system: sudo dpkg-reconfigure exim4-config
High disk usageLogs not being rotatedForce rotation: sudo logrotate -f /etc/logrotate.conf
Client logs not forwardingNetwork connectivity issuesTest connectivity: telnet server_ip 514

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.