Last modified: December 05, 2024

This article is written in: 🇺🇸

Log Files, Journals, and Logging Systems

Understanding how logging works in Linux is like learning the language your system uses to communicate. Logs are the detailed records that your system keeps about its activities, and they are invaluable for troubleshooting, monitoring performance, and ensuring security. Let's embark on a journey to demystify log files, journals, and the various logging systems used in Linux.

What is a log?

+-----------------------------------------------------------+
| LOG FILE                                                  |
|-----------------------------------------------------------|
| TIMESTAMP        | SEVERITY  | SERVICE   | MESSAGE        |
|-----------------------------------------------------------|
| 2023-08-01 09:00 | INFO      | myapp     | Server started |
| 2023-08-01 09:01 | WARNING   | myapp     | High CPU usage |
| 2023-08-01 09:02 | ERROR     | myapp     | Server crashed |
+-----------------------------------------------------------+

Why Logging Matters

Imagine trying to fix a car without knowing what's wrong. Logs provide the clues needed to diagnose and fix issues. They record everything from system errors and warnings to user activities and application events. By analyzing logs, you can:

The Landscape of Linux Logging Methods

Linux offers several methods for logging, each with its own set of features suited to different needs. The primary logging methods include plain text log files, journald, and rsyslog.

Plain Text Log Files

At the heart of Linux logging is the traditional plain text log file. These are simple text files where the system writes log messages. They're stored in the /var/log directory and are straightforward to read and parse.

Example of a Plain Text Log File (/var/log/syslog):

Aug  1 09:00:01 myhostname CRON[12345]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Aug  1 09:01:22 myhostname sshd[12346]: Accepted password for user from 192.168.1.100 port 54321 ssh2
Aug  1 09:02:33 myhostname sshd[12347]: Failed password for invalid user admin from 192.168.1.101 port 54322 ssh2

In this snippet:

Journald: The Systemd Journal

With the introduction of systemd, journald became the default logging system for many Linux distributions. It stores logs in a binary format, enabling more efficient storage and richer metadata.

Visual Representation of Journald Workflow:

+--------------------------------+
|        systemd-journald        |
+---------------+----------------+
                |
                | Collects Logs from:
                |
+---------------+----------------+
|               |                |
|           Systemd Units        |
|          (Services, etc.)      |
|               |                |
|           Kernel Messages      |
|               |                |
|           Applications         |
|               |                |
+---------------+----------------+
                |
                v
+--------------------------------+
|          Journal Files         |
|   (/run/log/journal/ or        |
|     /var/log/journal/)         |
+--------------------------------+

Features of journald:

Rsyslog

Rsyslog is a powerful logging system that extends the capabilities of traditional syslog. It supports various input and output modules, enabling complex log processing tasks.

Features of Rsyslog:

Exploring Common System Log Files

Logs are typically stored in the /var/log directory. Let's explore some of the most important log files you should be familiar with.

/var/log/syslog

This is the primary log file where most system messages are recorded. It includes messages from the kernel, system services, and applications.

Viewing /var/log/syslog:

sudo tail /var/log/syslog

Sample Output:

Aug  1 10:15:42 myhostname NetworkManager[1234]: <info>  [1596274542.1234] device (eth0): state change: activated -> deactivating
Aug  1 10:15:42 myhostname avahi-daemon[5678]: Withdrawing address record for 192.168.1.10 on eth0.
Aug  1 10:15:45 myhostname kernel: [12345.678901] eth0: Link is Down

/var/log/auth.log

This file records authentication-related events, such as logins and sudo usage.

Viewing Authentication Logs:

sudo grep "Failed password" /var/log/auth.log

Sample Output:

Aug  1 11:00:01 myhostname sshd[23456]: Failed password for invalid user admin from 192.168.1.101 port 54323 ssh2
Aug  1 11:05:12 myhostname sshd[23457]: Failed password for root from 192.168.1.102 port 54324 ssh2

/var/log/kern.log

Kernel logs contain messages from the Linux kernel, providing insights into hardware and system-level events.

Viewing Kernel Logs:

sudo tail /var/log/kern.log

Sample Output:

Aug  1 12:00:00 myhostname kernel: [13000.000000] CPU0: Temperature above threshold, cpu clock throttled
Aug  1 12:00:05 myhostname kernel: [13005.000000] CPU0: Temperature/speed normal

/var/log/dmesg

The dmesg command outputs messages from the kernel ring buffer, which is especially useful for diagnosing hardware issues.

Running dmesg:

dmesg | less

Sample Output:

[    0.000000] Initializing cgroup subsys cpuset
[    0.000000] Linux version 5.8.0-43-generic (buildd@lgw01-amd64-052) (gcc (Ubuntu 10.2.0-13ubuntu1) 10.2.0) #49-Ubuntu SMP ...
[    0.000000] Command line: BOOT_IMAGE=/boot/vmlinuz-5.8.0-43-generic root=UUID=...
[    0.000000] KERNEL supported cpus:
[    0.000000]   Intel GenuineIntel
[    0.000000]   AMD AuthenticAMD

Delving into Journald and journalctl

journalctl is the command-line tool for accessing logs stored by journald. It provides powerful filtering and querying capabilities.

Basic Usage of journalctl

Viewing All Logs:

journalctl

This command displays all journal entries, starting from the oldest.

Viewing Recent Logs:

journalctl -n 50

Shows the most recent 50 log entries.

Following Logs in Real-Time:

journalctl -f

Similar to tail -f, it streams new log entries as they occur.

Filtering Logs with journalctl

Filtering is where journalctl truly shines.

Filtering by Time Range:

journalctl --since "2023-08-01 08:00:00" --until "2023-08-01 12:00:00"

The --since and --until options accept natural language inputs like "1 hour ago" or "yesterday".

Filtering by Unit (Service):

journalctl -u apache2.service

Sample Output:

Aug  1 09:00:00 myhostname systemd[1]: Starting The Apache HTTP Server...
Aug  1 09:00:01 myhostname apachectl[1234]: AH00558: apache2: Could not reliably determine the server's fully qualified domain name...
Aug  1 09:00:01 myhostname systemd[1]: Started The Apache HTTP Server.

Advanced journalctl Usage

Filtering by Priority Level:

journalctl -p err

Shows logs with priority err (error) and higher (more severe).

Priority Levels:

Level Code Description
emerg 0 System is unusable.
alert 1 Immediate action required.
crit 2 Critical conditions.
err 3 Error conditions.
warning 4 Warning conditions.
notice 5 Normal but significant.
info 6 Informational messages.
debug 7 Debug-level messages.

Example of Priority Filtering:

journalctl -p warning -u ssh.service

Displays warnings and errors related to the SSH service.

Persistent Journals

By default, journald stores logs in memory, which means logs are lost on reboot. To make logs persistent:

I. Create Journal Directory:

sudo mkdir -p /var/log/journal

II. Set Permissions:

sudo systemd-tmpfiles --create --prefix /var/log/journal

III. Restart systemd-journald:

sudo systemctl restart systemd-journald

Now, logs are stored in /var/log/journal and persist across reboots.

Mastering Rsyslog for Log Management

Rsyslog offers extensive capabilities for collecting, processing, and forwarding log messages.

Understanding Rsyslog Configuration

Rsyslog configuration files are located at /etc/rsyslog.conf and in the /etc/rsyslog.d/ directory.

Basic Rsyslog Syntax:

facility.priority    action

Facilities:

Facility Description
auth, authpriv Authentication.
cron Cron jobs.
daemon System daemons.
kern Kernel messages.
mail Mail system.
syslog Internal syslog messages.
local0 to local7 Local use.

Priorities:

Level Description
emerg Emergency
alert Alert
crit Critical
err Error
warning Warning
notice Notice
info Informational
debug Debug

Example Configuration:

# Log all kernel messages to /var/log/kern.log
kern.*    /var/log/kern.log

# Log mail info messages to /var/log/mail.info
mail.info    /var/log/mail.info

# Log cron messages to /var/log/cron.log
cron.*    /var/log/cron.log

Filtering and Routing Logs

You can fine-tune which messages are logged where.

Example: Ignore Debug Messages for a Specific Facility:

daemon.none    /var/log/daemon.log

This prevents debug messages from the daemon facility from being logged to /var/log/daemon.log.

Setting Up a Centralized Log Server with Rsyslog

Centralized logging is crucial for managing logs in environments with multiple servers.

Configuring the Log Server

I. Enable UDP Reception:

In /etc/rsyslog.conf, uncomment or add:

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

II. Enable TCP Reception (Optional):

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

III. Define Templates and Rules (Optional):

To organize logs by host:

template(name="RemoteLogs" type="string" string="/var/log/%HOSTNAME%/%PROGRAMNAME%.log")
*.* ?RemoteLogs

IV. Restart Rsyslog:

sudo systemctl restart rsyslog

Configuring Client Machines

I. Edit /etc/rsyslog.conf:

Add the following line at the end:

* 514

For TCP, use @@ instead of @.

II. Restart Rsyslog:

sudo systemctl restart rsyslog

Verifying Centralized Logging

On the log server, check if logs from clients are being received:

ls /var/log/

You should see directories or files corresponding to client hostnames.

Example Directory Structure:

/var/log/client1/
/var/log/client2/
/var/log/client3/

Logger

The logger command is a shell utility used to add messages to the system log.

Basic Usage

Sending a Simple Message:

logger "Backup completed successfully."

This appends the message to /var/log/syslog.

Including Tags:

logger -t backup_script "Backup failed due to insufficient disk space."

Adds a tag [backup_script] to the log entry for easy identification.

Advanced Options

Specifying Facility and Priority:

logger -p local0.notice "Application started."

Sending to a Remote Syslog Server:

logger -n logserver_ip -P 514 "Remote log message from client."

Incorporating into Scripts

Example Backup Script with Logging:

#!/bin/bash

backup_dir="/backup"
source_dir="/data"

if rsync -av "$source_dir" "$backup_dir"; then
    logger -t backup_script -p local0.info "Backup completed successfully."
else
    logger -t backup_script -p local0.err "Backup failed!"
fi

Managing Log Files with Logrotate

Over time, log files can grow large, consuming significant disk space. logrotate automates the rotation, compression, and removal of log files.

Understanding Configuration

Configurations are stored in /etc/logrotate.conf and /etc/logrotate.d/.

Sample logrotate Configuration for /var/log/syslog:

/var/log/syslog
{
    rotate 7
    daily
    missingok
    notifempty
    delaycompress
    compress
    postrotate
        invoke-rc.d rsyslog rotate > /dev/null
    endscript
}

Explanation:

Directive Description
rotate 7 Keeps 7 rotated logs.
daily Rotates logs daily.
missingok Doesn't report an error if the log file is missing.
notifempty Doesn't rotate empty logs.
delaycompress Delays compression until the next rotation.
compress Compresses rotated logs.
postrotate Runs commands after rotation.

Forcing Log Rotation

To manually force a log rotation:

sudo logrotate -f /etc/logrotate.conf

Verifying Log Rotation

Check the rotated and compressed log files:

ls /var/log/syslog*

Expected Output:

/var/log/syslog
/var/log/syslog.1.gz
/var/log/syslog.2.gz
/var/log/syslog.3.gz
...

Practical Scenarios and Applications

Let's apply our knowledge to real-world situations.

Scenario 1: Detecting Unauthorized SSH Attempts

Step 1: Analyze Authentication Logs

sudo grep "Failed password" /var/log/auth.log

Step 2: Block Malicious IP Addresses

sudo iptables -A INPUT -s malicious_ip -j DROP

Alternatively, install fail2ban to automate this process.

Step 3: Secure SSH Configuration

I. Change the Default SSH Port:

Edit /etc/ssh/sshd_config:

Port 2222

II. Disable Root Login:

PermitRootLogin no

III. Use Public Key Authentication:

PasswordAuthentication no

IV. Restart SSH Service:

sudo systemctl restart sshd

Scenario 2: Monitoring Disk Usage

Step 1: Check Disk Space with df:

df -h

Sample Output:

Filesystem      Size  Used Avail Use% Mounted on
/dev/sda1        50G   45G  5.0G  90% /

The root filesystem is 90% full.

Step 2: Find Large Log Files

sudo du -sh /var/log/*

Sample Output:

1.2G    /var/log/apache2
500M    /var/log/mysql

Step 3: Rotate Logs Manually

If logrotate isn't configured properly, you might need to force rotation.

sudo logrotate -f /etc/logrotate.d/apache2

Step 4: Remove Old Logs

sudo find /var/log -type f -name "*.gz" -mtime +30 -delete

Deletes compressed log files older than 30 days.

Best Practices

Challenges

  1. Discuss the importance of logging in system administration, including its role in maintaining system health, identifying issues, and assisting with security auditing. Provide examples of how logging helps in daily administration tasks and long-term system monitoring.
  2. Research and describe Journald, its functions, and its advantages over traditional text-file-based logging systems. Explain how Journald works with systemd, highlighting features like binary storage, structured logging, and how it simplifies log management for modern systems.
  3. Explain how Rsyslog works and describe its configuration process, including how to set up centralized logging. Discuss severity levels, how they categorize log messages, and how they can be used to filter specific types of messages based on their importance or urgency.
  4. Use the logger command to create custom messages in the system logs. Experiment with different flags, such as specifying the facility or severity level, and explain how logger can be used to add entries manually or from within scripts for testing or informational purposes.
  5. Configure and use logrotate to automate log file management. Set up a basic configuration to rotate, compress, and delete log files on a schedule, and discuss how logrotate helps prevent logs from consuming excessive disk space. Explain the importance of log rotation in production systems.
  6. Research common log file formats, such as text-based, JSON, and binary formats, and compare their structures. Discuss the benefits and drawbacks of each format, considering factors like readability, compatibility with log analysis tools, and efficiency for storage and search.
  7. Set up and use log filters to selectively include or exclude specific log messages. Use either Rsyslog or Journald, and create a rule that filters messages based on criteria such as facility, severity level, or keywords. Document how filtering helps reduce noise in the logs and improves readability.
  8. Utilize log analysis tools like grep, journalctl, or awk to extract meaningful information from log files. Perform tasks such as searching for specific events, identifying patterns, and generating summary reports. Explain how log analysis helps administrators identify issues and monitor system health.
  9. Outline best practices for managing logs in a production environment. Discuss strategies for log retention, log security, and ensuring reliability and availability of log files. Include recommendations on how to securely store and transmit logs, especially for compliance purposes.
  10. Describe common logging-related issues in a Linux environment, such as missing logs, log file corruption, or disk space running out due to log growth. Explain steps for diagnosing and resolving each problem, including strategies for recovering lost logs and freeing up space.

Table of Contents

    Log Files, Journals, and Logging Systems
    1. What is a log?
    2. Why Logging Matters
    3. The Landscape of Linux Logging Methods
      1. Plain Text Log Files
      2. Journald: The Systemd Journal
      3. Rsyslog
    4. Exploring Common System Log Files
      1. /var/log/syslog
      2. /var/log/auth.log
      3. /var/log/kern.log
      4. /var/log/dmesg
    5. Delving into Journald and journalctl
      1. Basic Usage of journalctl
      2. Filtering Logs with journalctl
      3. Advanced journalctl Usage
      4. Persistent Journals
    6. Mastering Rsyslog for Log Management
      1. Understanding Rsyslog Configuration
      2. Filtering and Routing Logs
    7. Setting Up a Centralized Log Server with Rsyslog
      1. Configuring the Log Server
      2. Configuring Client Machines
      3. Verifying Centralized Logging
    8. Logger
      1. Basic Usage
      2. Advanced Options
      3. Incorporating into Scripts
    9. Managing Log Files with Logrotate
      1. Understanding Configuration
      2. Forcing Log Rotation
      3. Verifying Log Rotation
    10. Practical Scenarios and Applications
      1. Scenario 1: Detecting Unauthorized SSH Attempts
      2. Scenario 2: Monitoring Disk Usage
    11. Best Practices
    12. Challenges