dotlinux guide

A Guide to Syslog Security and Configuration on Linux

In the realm of Linux system administration, syslog stands as a cornerstone for centralized logging, offering visibility into system events, application behavior, and security incidents. Whether troubleshooting a misbehaving service, auditing user actions, or complying with regulatory standards (e.g., GDPR, HIPAA), syslog provides the raw data needed to maintain system health and security. However, misconfigured or insecure syslog setups can expose sensitive information, enable log tampering, or leave critical events undetected. This guide demystifies syslog, covering its fundamental concepts, configuration best practices, security hardening techniques, and advanced use cases. By the end, you’ll be equipped to implement a robust, secure syslog infrastructure that protects your logs and maximizes their utility.

Table of Contents

Understanding Syslog: Core Concepts

Syslog is a standard protocol for collecting and forwarding log messages from devices and applications. It was originally defined in RFC 3164 (obsoleted by RFC 5424) and is widely supported across operating systems, network devices, and software.

Syslog Message Structure

A syslog message consists of three main parts (per RFC 5424):

  • PRI (Priority): A numerical value calculated as (Facility * 8) + Severity, indicating the message source and urgency.
  • Header: Includes a timestamp, hostname/IP, and application name.
  • Message: The actual log content, which may include structured data (key-value pairs) for machine readability.

Facilities and Severities

  • Facility: Identifies the source of the log (e.g., kernel, user processes, mail system). Common facilities include:

    Facility CodeNameDescription
    0kernKernel messages
    1userUser-level messages
    2mailMail system
    3daemonSystem daemons (e.g., sshd)
    4authAuthentication (e.g., sudo)
    5syslogSyslog daemon itself
    6lprLine printer subsystem
    7newsNetwork news subsystem
  • Severity: Indicates the urgency of the message (lower numbers = more critical):

    Severity CodeNameDescription
    0emergEmergency (system unusable)
    1alertAction required immediately
    2critCritical conditions
    3errErrors
    4warningWarnings
    5noticeNormal but significant
    6infoInformational
    7debugDebugging messages

Key Syslog Components on Linux

Linux systems use syslog daemons to collect, process, and forward logs. The most common implementations are:

1. rsyslog

The default syslog daemon on most Linux distributions (Debian, RHEL, Ubuntu). It is highly configurable, supports encryption (TLS), and integrates with modern tools.

2. systemd-journald

Part of the systemd init system, journald collects logs in a binary format for efficient storage and querying. It works alongside rsyslog (which can forward journald logs to files or remote servers).

3. Legacy syslogd

Older, minimalistic daemon with limited features (rarely used today).

Syslog Configuration Basics

We’ll focus on rsyslog (the most widely adopted) for configuration examples. Its primary config file is /etc/rsyslog.conf, with additional rules in /etc/rsyslog.d/.

Core Configuration Elements

  • Modules: Loaded to enable features (e.g., imuxsock for local logging, imtcp for TCP reception).
  • Templates: Define log message formats (e.g., JSON, RFC 5424).
  • Rules: Specify where to forward logs (e.g., files, remote servers) based on facility/severity.

Example 1: Basic Local Logging

To log all auth (facility 4) messages of severity info (6) or higher to /var/log/auth.log:

# /etc/rsyslog.d/auth.conf
auth.info /var/log/auth.log

Example 2: Filter by Severity

Log only critical (crit, 2) and emergency (emerg, 0) messages from all facilities to /var/log/critical.log:

# /etc/rsyslog.d/critical.conf
*.=crit;*.=emerg /var/log/critical.log

Example 3: Using Templates

Format logs as JSON for easier parsing by tools like Elasticsearch:

# /etc/rsyslog.d/json-template.conf
template(name="JsonFormat" type="list") {
  constant(value="{")
  constant(value="\"timestamp\":\"")     property(name="timereported" dateFormat="rfc3339")
  constant(value="\",\"hostname\":\"")   property(name="hostname")
  constant(value="\",\"facility\":\"")   property(name="syslogfacility-text")
  constant(value="\",\"severity\":\"")   property(name="syslogseverity-text")
  constant(value="\",\"message\":\"")    property(name="msg" format="json")
  constant(value="\"}\n")
}

# Apply template to all logs
*.* /var/log/json-logs.log;JsonFormat

Syslog Security Best Practices

Logs are critical for incident response and compliance—securing them is non-negotiable.

1. Restrict Log File Permissions

Ensure logs are readable only by authorized users (e.g., root). Use chmod and chown to enforce this:

# Set ownership to root:adm (admin group)
sudo chown root:adm /var/log/auth.log

# Allow read access only for owner/group, no access for others
sudo chmod 640 /var/log/auth.log

2. Prevent Log Tampering

  • Append-Only Mode: Use chattr +a to make log files append-only (prevents deletion/modification):
    sudo chattr +a /var/log/auth.log
  • Remote Logging: Forward logs to a centralized, immutable log server (see “Advanced Configuration”).

3. Encrypt Remote Logs with TLS

Unencrypted syslog traffic (UDP/TCP) is vulnerable to eavesdropping. Use TLS to encrypt logs in transit with rsyslog.

Step 1: Generate TLS Certificates

Use openssl to create a CA, server, and client certificates:

# Create CA key and cert
openssl genrsa -out ca.key 2048
openssl req -new -x509 -days 365 -key ca.key -out ca.crt

# Server key and CSR
openssl genrsa -out server.key 2048
openssl req -new -key server.key -out server.csr
openssl x509 -req -in server.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out server.crt -days 365

# Client key and CSR (repeat for each client)
openssl genrsa -out client.key 2048
openssl req -new -key client.key -out client.csr
openssl x509 -req -in client.csr -CA ca.crt -CAkey ca.key -CAcreateserial -out client.crt -days 365

Step 2: Configure rsyslog Server (TLS)

# /etc/rsyslog.d/tls-server.conf
module(load="imtcp" StreamDriver.Name="gtls" StreamDriver.Mode="1" StreamDriver.AuthMode="anon")
input(type="imtcp" port="6514")  # TLS port (standard: 6514)

# Trust the CA
global(
  DefaultNetstreamDriverCAFile="/etc/rsyslog.d/ca.crt"
  DefaultNetstreamDriver="gtls"
)

Step 3: Configure rsyslog Client (TLS)

# /etc/rsyslog.d/tls-client.conf
global(
  DefaultNetstreamDriverCAFile="/etc/rsyslog.d/ca.crt"
  DefaultNetstreamDriver="gtls"
)

# Forward logs to server via TLS
action(
  type="omfwd"
  target="log-server.example.com"
  port="6514"
  protocol="tcp"
  StreamDriver="gtls"
  StreamDriverMode="1"  # Require TLS
  StreamDriverAuthMode="x509/name"
  StreamDriverPermittedPeers="log-server.example.com"  # Verify server cert
)

4. Authenticate Log Sources

Use TLS client authentication to ensure logs are only accepted from trusted clients. Modify the server config to require client certificates:

# Server: Require client certs
module(load="imtcp" StreamDriver.Name="gtls" StreamDriver.Mode="1" StreamDriver.AuthMode="x509/name")
input(type="imtcp" port="6514")

# Trust client certs signed by CA
global(
  DefaultNetstreamDriverCAFile="/etc/rsyslog.d/ca.crt"
  DefaultNetstreamDriverCertFile="/etc/rsyslog.d/server.crt"
  DefaultNetstreamDriverKeyFile="/etc/rsyslog.d/server.key"
)

5. Rotate Logs to Prevent Disk Bloat

Use logrotate to automatically compress, archive, and delete old logs. Example config for /var/log/auth.log:

# /etc/logrotate.d/auth
/var/log/auth.log {
  daily          # Rotate daily
  missingok      # Ignore if file missing
  rotate 7       # Keep 7 days of logs
  compress       # Compress old logs with gzip
  delaycompress  # Compress next rotation
  notifempty     # Don't rotate empty files
  create 640 root adm  # Recreate log file with permissions
}

Advanced Configuration: Remote Logging & Encryption

Centralized logging simplifies monitoring and security. Here’s how to set up a rsyslog server and client.

Remote Logging with TCP (Unencrypted, for Testing)

Server Config:

# /etc/rsyslog.d/remote-server.conf
module(load="imtcp")  # Load TCP input module
input(type="imtcp" port="514")  # Listen on TCP port 514

# Save remote logs to /var/log/remote/<client-ip>.log
template(name="RemoteLogs" type="string" string="/var/log/remote/%FROMHOST-IP%.log")
*.* ?RemoteLogs

Client Config:

# /etc/rsyslog.d/remote-client.conf
*.* action(type="omfwd" target="log-server.example.com" port="514" protocol="tcp")

Reliable Logging with RELP

For lossless log delivery (e.g., unstable networks), use the Reliable Event Logging Protocol (RELP) instead of TCP:

# Client: Forward logs via RELP
module(load="omrelp")
action(type="omrelp" target="log-server.example.com" port="2514")  # RELP port

Monitoring and Analyzing Syslogs

Collecting logs is useless without analysis. Use these tools to extract insights:

1. Basic Tools

  • journalctl: Query systemd-journald logs (e.g., show errors from sshd):
    journalctl -u sshd -p err
  • grep/awk: Filter logs (e.g., find failed SSH attempts):
    grep "Failed password" /var/log/auth.log

2. Enterprise-Grade Tools

  • ELK Stack: Elasticsearch (store), Logstash (process), Kibana (visualize).
  • Graylog: Open-source log management with alerting.
  • Splunk: Commercial tool for log analysis and SIEM.

Common Pitfalls and Troubleshooting

Pitfall 1: Misconfigured Firewalls

Ensure the log server allows inbound traffic on syslog ports (e.g., 514/TCP, 6514/TLS):

# Allow TLS syslog (6514) on UFW
sudo ufw allow 6514/tcp

Pitfall 2: Unencrypted Remote Logs

Always use TLS/RELP for remote logging—never send sensitive logs over UDP/TCP in plaintext.

Pitfall 3: Ignoring Log Rotation

Unrotated logs can fill disks, causing system instability. Test logrotate configs with:

sudo logrotate -d /etc/logrotate.d/auth  # Dry run

Troubleshooting rsyslog

  • Check status: sudo systemctl status rsyslog
  • View debug logs: Add $DebugLevel 2 to /etc/rsyslog.conf and restart.

Conclusion

Syslog is the backbone of Linux system visibility, but its value depends on proper configuration and security. By following this guide, you’ll:

  • Centralize logs for easier monitoring.
  • Secure logs against tampering and eavesdropping.
  • Comply with regulatory requirements (e.g., PCI-DSS, GDPR).

Remember: Logs are your first line of defense in incident response—treat them as critically as any other system resource.

References