Sub topic 9
Forwarding logs from client servers
This chapter configures one client server to send its logs to a central host with rsyslog. It assumes you can edit files as root, use systemctl, and open a TCP port in a firewall. It also assumes you know how journald and rsyslog relate, from the earlier chapters on log sources and rsyslog basics. The receiving side comes in the next chapter. Here you only need to know that a host named loghost.example.com will listen on TCP port 514.
What the client does
A client forwards logs in three steps:
- Programs write messages to journald, to the syslog socket, or to files.
- rsyslog collects the messages. On most distributions it reads them from the journal (the
imjournalmodule) or from a socket that journald feeds (theimuxsockmodule). Check which one your distribution uses in/etc/rsyslog.conf. - rsyslog runs each message through its rules. A rule that contains a forwarding action sends the message over the network.
Forwarding adds a copy of each message. It does not move logs off the client. Local files and the journal keep working, and logrotate and journald limits still apply to them.
The forwarding rule
Put the client's forwarding rule in its own file under /etc/rsyslog.d/. rsyslog reads that directory in alphabetical order, so the 50- prefix places the file after default rules that start with lower numbers.
# /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")An action() that stands alone at the top level of a config file applies to every message. Each parameter does one job:
| Parameter | Meaning |
|---|---|
| `type="omfwd"` | The output module that forwards messages over the network. |
| `target`, `port`, `protocol` | Where to send and which transport to use. |
| `queue.type="LinkedList"` | An in-memory queue that holds messages between the rule and the network. |
| `queue.filename` | Setting a file name makes the queue disk-assisted. rsyslog spills to disk files with this name prefix when memory fills. |
| `queue.maxDiskSpace` | Upper limit for the queue files on disk, here 1 GB. |
| `queue.saveOnShutdown` | Writes unsent messages to disk when rsyslog stops, so a restart does not lose them. |
| `action.resumeRetryCount="-1"` | Retry without limit while the server is unreachable. |
Without the queue settings, a server outage that outlasts the retry limit means dropped messages. Without a queue limit, a long outage could fill the client's disk, which is why queue.maxDiskSpace is set. When the limit is reached, rsyslog discards new messages for this action instead of exhausting the disk.
Choosing what to forward
The rule above forwards everything. To forward only part of the stream, put the action inside a filter. Severity numbers run from 0 (emergency) to 7 (debug), so warnings and worse are 4 and below.
# /etc/rsyslog.d/50-forward.conf: forward warnings and worse only
if ($syslogseverity <= 4) then {
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")
}You can also filter by facility, for example $syslogfacility-text == "authpriv", or by program with $programname == "nginx". Forwarding everything is simpler to reason about, and the central server can then answer questions you did not plan for. Filtering saves bandwidth and central disk space. Choose filtering when your volume forces the issue.
Old syntax you will meet
Older configurations use a one-line form. A single @ means UDP and a double @@ means TCP.
*.* @@loghost.example.com:514This is equivalent to a basic omfwd action over TCP, but it has no place for queue settings. Prefer action() for anything you want to be reliable. Both forms work in the same file, so you do not have to convert old files at once.
Applying and checking the change
Validate the syntax before you restart. A typo in a file under /etc/rsyslog.d/ can stop rsyslog from starting at all, and then the client logs nothing locally either.
sudo rsyslogd -N1
sudo systemctl restart rsyslog
sudo systemctl status rsyslog --no-pager
logger -t demo "forwarding test from $(hostname)"
sudo ss -tn state established '( dport = :514 )'rsyslogd -N1 checks the configuration and exits. logger writes a test message through the normal syslog path, tagged demo. ss should show an established connection to port 514 after the first message goes out. If the server is down, no connection appears, and the queue files begin to grow in rsyslog's work directory, usually /var/spool/rsyslog.
Host identity and time
The central server names its files with %HOSTNAME% from each message, so the client's hostname must be unique and correct. Two servers both called localhost or ubuntu write into the same files and you cannot tell them apart. Set names with hostnamectl set-hostname. Keep the clock synchronised too, because the message timestamp comes from the client. Later chapters cover both.
Common errors
- Nothing arrives and no connection appears. Check name resolution (
getent hosts loghost.example.com) and test the port from the client withnc -vz loghost.example.com 514. A firewall on either side is the usual cause. - rsyslog fails to start after the edit. Run
rsyslogd -N1. It prints the file and line of the first error. Unbalanced parentheses and a missing quote inaction()are the typical mistakes. - Messages arrive from some programs but not others. Some services log only to their own files or to the journal, so rsyslog never sees them. Confirm with
journalctl -t programname, and check that rsyslog reads the journal on this distribution. - The client disk fills during a long outage.
queue.maxDiskSpaceis missing or too large for the partition that holds the work directory. Size it against free space. - Duplicate messages on the server. The same message is forwarded by two rules, for example a leftover
*.* @@line plus the new action. Search withgrep -r 'omfwd\|@@' /etc/rsyslog.conf /etc/rsyslog.d/. - The central server forwards to itself. Do not install this client rule on the receiver. Messages loop and multiply.
Exercise
A client forwards with the rule above. The central server is down for three hours, then comes back. Which settings make the messages from those three hours reach the server, and what happens if the outage lasts long enough to exceed the queue's disk limit?
Show answer
action.resumeRetryCount="-1" makes rsyslog retry forever instead of giving up, and the disk-assisted queue (queue.type="LinkedList" with queue.filename) stores the messages meanwhile. When the server returns, rsyslog sends the queued messages. If the queue reaches queue.maxDiskSpace (1 GB here), rsyslog discards new messages for that action until space frees up, so the oldest outage period is kept and the newest messages are lost.
Exercise
You add 50-forward.conf and restart rsyslog, but the service fails to start. Which command finds the problem without restarting, and why should you have run it first?
Show answer
sudo rsyslogd -N1 checks the configuration syntax and reports the file and line of the error without starting the service. Running it before the restart prevents rsyslog from going down, which would also stop local logging.