Sub topic 2
How logrotate runs and remembers state
This chapter assumes you can edit files with sudo, restart a systemd service, open a TCP port in a firewall, and find log files in /var/log.
How logrotate runs and remembers state
logrotate renames each log file, starts a new empty one, and deletes the oldest generations. It does not run on its own. On most distributions, a systemd timer runs it once a day. Each run reads /etc/logrotate.conf and every file in /etc/logrotate.d/.
To decide whether a log is due, logrotate reads a state file, usually /var/lib/logrotate/logrotate.status. This file records the last rotation date for each log path. A log rotated earlier that day is skipped. To force a rotation, run sudo logrotate -f /etc/logrotate.conf.
Limiting journal size and age
The systemd journal stores logs in binary form under /var/log/journal. Without limits, it can grow until the disk fills. A drop-in file sets the limits:
# /etc/systemd/journald.conf.d/retention.conf
[Journal]
Storage=persistent
SystemMaxUse=500M
MaxRetentionSec=1monthStorage=persistent keeps logs across reboots. SystemMaxUse caps total disk use, and MaxRetentionSec deletes entries older than the given age. Apply the change with sudo systemctl restart systemd-journald, then check usage with journalctl --disk-usage.
rsyslog configuration basics
rsyslog is the syslog daemon. It reads messages and writes them to files or forwards them to other hosts. Its settings live in /etc/rsyslog.conf and /etc/rsyslog.d/*.conf.
Each rule has a selector and an action. The selector names a facility (such as auth or local0) and a severity (such as warning), joined by a dot. The action is a file path, @host:port for UDP, or @@host:port for TCP. For example, *.* @@loghost.example.com:514 sends every message over TCP. Check the syntax before restarting with sudo rsyslogd -N1.
Forwarding logs from client servers
Each client gets a forwarding rule:
# /etc/rsyslog.d/50-forward.conf on each client
action(type="omfwd"
target="loghost.example.com" port="514" protocol="tcp"
queue.type="LinkedList"
queue.filename="fwd_queue"
queue.maxDiskSpace="1g"
queue.saveOnShutdown="on"
action.resumeRetryCount="-1")The queue holds messages on disk while the central server is unreachable. action.resumeRetryCount="-1" tells rsyslog to retry indefinitely instead of giving up.
Building the central log receiver
The central server loads the TCP input and writes each client's messages to a separate file:
# /etc/rsyslog.d/10-receiver.conf on the central server
module(load="imtcp")
template(name="PerHostFile" type="string"
string="/var/log/remote/%HOSTNAME%/%programname%.log")
ruleset(name="remote") {
action(type="omfile" dynaFile="PerHostFile")
}
input(type="imtcp" port="514" ruleset="remote")Create /var/log/remote, allow TCP 514 through the firewall, and restart rsyslog. The %HOSTNAME% value comes from each incoming message, so every client must have a correct, unique hostname.
Exercise
A client cannot reach the central server for an hour. Which two settings in its forwarding rule keep the messages and keep retrying?
Show answer
The disk-backed queue keeps the messages: queue.type="LinkedList" with queue.saveOnShutdown="on". The retry setting is action.resumeRetryCount="-1", which retries indefinitely.