Last modified: June 01, 2025
This article is written in: 🇺🇸
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.
+-----------------------------------------------------------+
| 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 |
+-----------------------------------------------------------+
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:
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
.
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:
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 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:
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
journalctl
journalctl
is the command-line tool for accessing logs stored by journald
. It provides powerful filtering and querying capabilities.
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.
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.
journalctl
UsageFiltering 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.
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.
Rsyslog offers extensive capabilities for collecting, processing, and forwarding log messages.
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
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
.
Centralized logging is crucial for managing logs in environments with multiple servers.
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
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
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/
The logger
command is a shell utility used to add messages to the system log.
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.
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."
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
rsync
to perform the backup.Over time, log files can grow large, consuming significant disk space. logrotate
automates the rotation, compression, and removal of log files.
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. |
To manually force a log rotation:
sudo logrotate -f /etc/logrotate.conf
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
...
Let's apply our knowledge to real-world situations.
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
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.
Logwatch
or Splunk
to enhance efficiency and reduce manual oversight.Nagios
or Prometheus
to ensure rapid response to incidents.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.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.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.