Sub topic 8
rsyslog Configuration Basics
Prerequisites and the message path
This chapter assumes that you can edit files with sudo, use systemctl, and understand Linux facilities and severities such as auth, kern, daemon, info, and error.
rsyslog is a service that receives, filters, transforms, stores, and forwards log messages. It commonly writes text files under /var/log, but it can also send messages to another server or a database.
A Linux server can have both systemd-journald and rsyslog. Journald collects messages from the kernel, services, and standard output. Depending on the distribution, rsyslog reads messages from journald or from the local syslog socket. The exact connection is distribution-specific, so inspect the installed configuration before changing it.
Configuration files and processing order
The main configuration file is usually /etc/rsyslog.conf. Additional files normally live in /etc/rsyslog.d/. Files in that directory are commonly read in lexical filename order. Numeric prefixes make the order clear, such as 10-local.conf and 50-forward.conf.
A rule has two important parts:
- A selector or filter that decides which messages match.
- An action that says what to do with matching messages.
This traditional rule writes messages from the authpriv facility at any severity to /var/log/secure:
authpriv.* /var/log/secureThe asterisk after the period means every severity. A facility and severity separated by a semicolon create multiple selectors:
kern.*;mail.crit /var/log/system-important.logThis matches every kernel message and mail messages with severity crit or more urgent.
The special action stop prevents later rules from processing a message:
authpriv.* /var/log/secure
authpriv.* stopUse this only when you understand the complete rule order. A misplaced stop can silently prevent messages from reaching other files or forwarding actions.
RainerScript rules
Modern rsyslog configurations often use RainerScript, a structured syntax that is easier to extend. This example stores messages from one program in a separate file:
if $programname == "sshd" then {
action(type="omfile" file="/var/log/sshd-events.log")
}The omfile module writes to a file. Rsyslog has many modules, and the module name often identifies the input or output type. For example, imuxsock reads the local syslog socket and omfile writes files.
Important operating details
| Concern | Why it matters |
|---|---|
| Rule order | A message can match several rules, or be stopped early. |
| File permissions | Log files may contain sensitive authentication or application data. |
| Ownership | The rsyslog service must be able to create or write the destination file. |
| Rotation | After logrotate renames a file, rsyslog may need a reload or HUP to reopen it. |
| Syntax validation | A configuration error can prevent rsyslog from starting or reloading. |
Before restarting or reloading rsyslog, validate the complete configuration:
sudo rsyslogd -N1
sudo systemctl reload rsyslog
sudo systemctl status rsyslog --no-pagerA successful syntax check does not prove that a rule receives the messages you expect. Send a controlled test message with logger, then inspect the selected destination.
Exercise
You add local0.* /var/log/test.log, but the file stays empty. What should you check first?
Show answer
Check that the application actually sends messages with the local0 facility, that rsyslog is running, that the destination path is writable, and that no earlier rule stops the message. Use rsyslogd -N1 to check syntax and logger -p local0.info "test message" to generate a matching message.