Sub topic 8
rsyslog configuration basics
rsyslog is the syslog daemon on most Linux distributions. It receives messages, filters them by rules, and writes them to files, sends them to other hosts, or hands them to other programs. This chapter covers the working model of its configuration, the most common errors, and how to check your changes before you apply them.
Prerequisites
This chapter assumes you can edit files as root or with sudo, and manage services with systemctl. It also assumes you know the syslog facility and severity model from the earlier chapter, and that you know journald collects messages from services on a systemd host.
How rsyslog fits with journald
On a systemd host, journald usually collects messages first. rsyslog then gets them in one of two ways:
- imjournal reads messages from the journal. This is the default on Red Hat family systems.
- imuxsock listens on the
/run/systemd/journal/syslogsocket, which journald forwards to. This is the default on Debian and Ubuntu.
Either way, rsyslog is a second consumer of the same messages. The journal keeps its own copy. rsyslog turns messages into the plain text files in /var/log and, later in this tutorial, forwards them to a central host.
The configuration layout
rsyslog reads /etc/rsyslog.conf first. That file normally ends with an include line that loads every file in a drop-in directory:
include(file="/etc/rsyslog.d/*.conf" mode="optional")Files in /etc/rsyslog.d/ load in alphabetical order. This order matters, because rules run in the order rsyslog reads them. That is why packages and administrators use number prefixes such as 10-receiver.conf and 50-forward.conf. Prefer a new drop-in file over editing the main file. Package updates can overwrite rsyslog.conf but leave your drop-ins alone.
Three syntaxes
rsyslog accepts three configuration styles. You will see all of them in the wild.
| Style | Example | Notes |
|---|---|---|
| Basic (sysklogd) | `auth,authpriv.* /var/log/auth.log` | Short and still common for simple rules |
| Legacy directive | `$ModLoad imtcp` | Old style with a dollar sign. Avoid it in new files |
| RainerScript | `module(load="imtcp")` | Current style. Use it for anything non-trivial |
You can mix the styles in one file. For new work, use RainerScript for modules, inputs, templates and actions. Use the basic style only for short selector lines.
The building blocks
A working configuration is made of a few kinds of objects.
- Modules add features. Input modules (names start with
im) receive messages. Output modules (om) deliver them. Examples areimtcp,imudp,omfileandomfwd. - Inputs use a loaded module to start receiving, for example a TCP listener on a port.
- Rules decide what happens to a message. A rule has a filter and one or more actions.
- Actions do something with a message, such as write it to a file.
- Templates control the format of a message or the name of a file.
- Rulesets are named groups of rules. An input can be bound to one, so messages from that input skip the default rules.
Selectors: facility and severity
The basic filter is a selector in the form facility.priority. The priority matches that severity and every more severe one.
# Everything from the mail facility goes to one file
mail.* /var/log/maillog
# Warning and above from any facility, except mail and private auth
*.warning;mail.none;authpriv.none /var/log/messages
# Only errors and worse from the kernel
kern.err /var/log/kernel-errors.log
# Exactly one severity, using the = prefix
cron.=info /var/log/cron-info.logSeveral things are worth remembering:
.noneremoves a facility from the line it appears on.- A semicolon separates selectors, and a comma lists several facilities that share one priority.
- A leading
-before a file path (-/var/log/x) used to turn off syncing after every write. Modern rsyslog buffers by default, so you can leave it out. *.*matches everything.
Property-based and expression filters
When facility and severity are not enough, RainerScript can test any message property:
if $programname == 'sshd' then {
action(type="omfile" file="/var/log/sshd-only.log")
stop
}
if $msg contains 'Failed password' then {
action(type="omfile" file="/var/log/ssh-failures.log")
}
if $fromhost-ip startswith '10.20.' then {
action(type="omfile" file="/var/log/lan-hosts.log")
}The stop statement discards the message after the block, so no later rule sees it. Without stop, the message continues through every remaining rule, and it can be written to several files. This is a frequent surprise when the same line shows up in both /var/log/messages and a custom file. That is normal. Add stop if you want it in one place only.
Useful properties include $msg, $programname, $hostname, $fromhost-ip, $syslogfacility-text and $syslogseverity-text.
Templates
A template turns message properties into text. You use one to change the line format or to build a file path from the message.
template(name="PerProgram" type="string"
string="/var/log/apps/%programname%.log")
template(name="ShortFormat" type="string"
string="%timegenerated% %HOSTNAME% %syslogtag%%msg%\n")
action(type="omfile" dynaFile="PerProgram" template="ShortFormat")file= gives a fixed path. dynaFile= takes the name of a template and computes the path for each message. Directories in a dynamic path are created on demand. Ownership and mode of those files come from the global fileOwner, fileCreateMode and dirCreateMode settings, not from the action alone.
File permissions and privileges
rsyslog starts as root, opens its inputs, and then can drop privileges. The defaults on many systems create files as root:adm with mode 0640. Set them explicitly when another tool, such as a log reader or logrotate with its own create line, must agree:
global(
fileOwner="root"
fileGroup="adm"
fileCreateMode="0640"
dirCreateMode="0755"
)Always validate before restarting
A syntax error can stop rsyslog from starting, and then logging stops on that host. Check the configuration first.
sudo rsyslogd -N1-N1 runs a check at level 1 and exits. It prints errors with file names and line numbers and returns a non-zero exit status on failure. If it prints nothing alarming, apply the change:
sudo systemctl restart rsyslog
sudo systemctl status rsyslog --no-pagerThen send a test message and find it:
logger -p local4.notice -t demo "rsyslog basics test"
sudo grep demo /var/log/syslog /var/log/messages 2>/dev/nullThe file name depends on the distribution. Debian and Ubuntu use /var/log/syslog. Red Hat family systems use /var/log/messages. The 2>/dev/null hides the error for the file that does not exist.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| rsyslog fails to start after an edit | Syntax error, such as an unclosed brace or quote | Run `rsyslogd -N1` and fix the reported line |
| A rule never matches | An earlier rule used `stop`, or file order is not what you expect | Check file name prefixes and look for `stop` |
| Messages appear in two files | No `stop` after the first matching rule | Add `stop` inside the block |
| A new log file is not created | rsyslog cannot write to the directory (permissions or SELinux/AppArmor) | Check ownership, and check the security log for denials |
| Settings seem ignored | Mixed legacy `$` directives apply at the point they appear and can be overridden | Move to RainerScript and keep one definition |
| A module command fails | The module is not loaded before the input that needs it | Put `module(load=...)` before `input(...)` |
| A message is missing its custom tag | The sender used a different program name than you filtered on | Check `$programname` with a template that prints it |
A short worked example
This drop-in sends all local4 messages from one application to its own file and keeps them out of the general log.
# /etc/rsyslog.d/20-myapp.conf
if $syslogfacility-text == 'local4' then {
action(type="omfile" file="/var/log/myapp/app.log")
stop
}Because 20- loads before the default rules in many setups, the stop keeps these messages out of /var/log/syslog. If the default rules live in rsyslog.conf itself, this holds true for the same reason: the main file reads its include line at a specific point, so check where that line sits on your distribution.
Create the directory first, since omfile creates files but, with default settings, a missing parent directory may be created only for dynamic files:
sudo mkdir -p /var/log/myapp
sudo chown root:adm /var/log/myapp
sudo rsyslogd -N1 && sudo systemctl restart rsyslog
logger -p local4.info -t myapp "hello from myapp"
sudo tail -n 2 /var/log/myapp/app.logExercise
A drop-in file contains a rule that writes sshd messages to /var/log/sshd-only.log. The messages also still appear in /var/log/syslog. What change keeps them out of /var/log/syslog, and what must you check about file order for it to work?
Show answer
Add a stop statement inside the rule's block after the omfile action. It only helps if the drop-in is read before the rule that writes to /var/log/syslog, so the file name prefix (for example 20-) must sort before the default rules. Run rsyslogd -N1 before restarting.
Exercise
What is the difference between file= and dynaFile= in an omfile action?
Show answer
file= is a fixed path. dynaFile= names a template, and rsyslog evaluates that template for each message to choose the file, for example one file per program name.