8.4.9 Lab: Configure Logging On Linux

4 min read

In this lab, you will learn how to configure logging on Linux systems. Logging is an essential aspect of system administration, as it helps monitor system activities, troubleshoot issues, and maintain security. Linux systems generate various logs that record events such as system startup, user logins, application errors, and security incidents. Understanding how to configure and manage these logs is crucial for maintaining a healthy and secure system.

Linux logging is primarily handled by the systemd-journald service and the rsyslog daemon. The systemd journal stores logs in a binary format, while rsyslog manages traditional text-based log files. Together, these tools provide a comprehensive logging solution for Linux systems Still holds up..

Before diving into the configuration, make sure to understand the different types of logs available on a Linux system. Common log files include:

  • /var/log/syslog or /var/log/messages: General system messages
  • /var/log/auth.log: Authentication-related events
  • /var/log/kern.log: Kernel messages
  • /var/log/dmesg: Boot-time kernel messages
  • /var/log/cron: Cron job logs
  • /var/log/maillog: Mail server logs

To begin configuring logging, you need to access the system's log configuration files. The primary configuration file for rsyslog is located at /etc/rsyslog.conf. This file defines the global settings for log processing, including the default log file locations and the rules for handling different types of messages.

Open the rsyslog configuration file using a text editor:

sudo nano /etc/rsyslog.conf

In this file, you will find various directives that control how logs are processed. Take this: the following line specifies the default log file for general messages:

*.info;mail.none;authpriv.none;cron.none                /var/log/messages

This directive tells rsyslog to log all messages with a priority of info or higher to the /var/log/messages file, excluding mail, authentication, and cron messages.

To create custom log rules, you can add new lines to the configuration file. Here's a good example: if you want to log all authentication-related messages to a separate file, you can add the following line:

authpriv.*                                              /var/log/auth.log

This rule directs all messages from the authpriv facility to the /var/log/auth.log file And it works..

After making changes to the configuration file, you need to restart the rsyslog service to apply the new settings:

sudo systemctl restart rsyslog

In addition to rsyslog, you can also use the journalctl command to interact with the systemd journal. The journal stores logs in a structured format, making it easier to filter and search for specific events. To view the entire journal, use the following command:

sudo journalctl

You can also filter logs based on various criteria, such as time, priority, or unit. To give you an idea, to view logs from the last hour, use:

sudo journalctl --since "1 hour ago"

To filter logs by priority, use the -p option followed by the desired priority level. Here's a good example: to view only error messages, use:

sudo journalctl -p err

The systemd journal also allows you to monitor logs in real-time using the -f option:

sudo journalctl -f

This command displays new log entries as they are generated, making it useful for monitoring system activity in real-time.

To confirm that logs are properly managed and do not consume excessive disk space, don't forget to configure log rotation. Log rotation is handled by the logrotate utility, which is configured through files in the /etc/logrotate.So d/ directory. Each file in this directory defines the rotation settings for a specific log file or group of log files.

Take this: the default logrotate configuration for rsyslog logs is defined in /etc/logrotate.In real terms, d/rsyslog. This file specifies how often logs should be rotated, how many archived logs to keep, and whether to compress old log files The details matter here..

To create a custom log rotation rule, you can add a new file to the /etc/logrotate.But d/ directory. Here's a good example: if you want to rotate the `/var/log/auth.

No fluff here — just what actually works Most people skip this — try not to..

/var/log/auth.log {
    weekly
    rotate 4
    compress
    delaycompress
    missingok
    notifempty
    create 640 root adm
}

This configuration ensures that the authentication log is rotated weekly, compressed, and retained for four weeks Not complicated — just consistent..

In addition to configuring log rotation, it's also important to monitor log files for specific events or patterns. You can use tools like grep to search for specific keywords or regular expressions in log files. To give you an idea, to search for failed login attempts in the authentication log, use:

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

This command displays all lines containing the phrase "Failed password," which typically indicates unsuccessful login attempts Most people skip this — try not to..

For more advanced log analysis, you can use tools like awk or sed to process and filter log data. These tools allow you to extract specific fields, perform calculations, or generate summaries based on log content.

So, to summarize, configuring logging on Linux systems is a fundamental skill for system administrators. By understanding how to manage log files, create custom log rules, and analyze log data, you can effectively monitor system activity, troubleshoot issues, and maintain security. The combination of rsyslog for traditional log management and the systemd journal for structured logging provides a powerful and flexible logging solution for Linux systems Worth keeping that in mind..

More to Read

What's New Today

Handpicked

See More Like This

Thank you for reading about 8.4.9 Lab: Configure Logging On Linux. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home