Sub topic 10
Building the central log receiver
A central log receiver is one server that accepts syslog messages from many clients and stores them on disk, one file per host and program. This chapter builds that receiver with rsyslog and explains the parts that commonly go wrong.
It assumes you can edit files as root, use systemctl, and open a TCP port in a firewall. It also assumes the client side is already forwarding, as in the previous chapter, and that you know what a syslog facility and severity are.
What the receiver does
The receiver does three jobs:
- It listens on a network port.
- It hands each incoming message to a ruleset, which is a named list of rules.
- It writes the message to a file whose name is computed from the message itself.
The receiver configuration
Create this file on the central server:
# /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")Each part does one thing.
module(load="imtcp")loads the input module for plain TCP. rsyslog is modular, and nothing listens until you load a module for it.template(...)defines a string with placeholders, called properties.%HOSTNAME%is the host name in the message.%programname%is the tag of the sending program, such assshdordemo.ruleset(name="remote")groups the rules that apply to remote messages. Theactioninside it writes to a file usingomfile.dynaFiletellsomfileto compute the file name from the template for every message. A plainfile=option would use one fixed name.input(...)opens port 514 and binds it to theremoteruleset.
Why a separate ruleset
Messages that arrive through an input go to the ruleset named in that input. Here that is remote, so they skip the rules in your main configuration. Without the binding, remote messages would pass through the default rules and land in the receiver's own /var/log/syslog or /var/log/messages. Those files would mix local and remote events, and each remote line would then appear twice.
Prepare the server
Create the directory, check permissions, and open the port before you restart rsyslog.
sudo mkdir -p /var/log/remote
sudo chmod 750 /var/log/remote
# firewalld (RHEL family)
sudo firewall-cmd --permanent --add-port=514/tcp
sudo firewall-cmd --reload
# ufw (Debian family): allow only your client network
sudo ufw allow from 10.0.0.0/24 to any port 514 proto tcp
sudo rsyslogd -N1
sudo systemctl restart rsyslog
sudo ss -tlnp | grep ':514'rsyslogd -N1 checks the configuration syntax without starting the daemon. ss -tlnp should show rsyslogd listening on port 514.
Plain syslog over TCP has no authentication. Restrict the port to your client network in the firewall. Later in this tutorial you add TLS so that only hosts with a valid certificate can send.
Files, directories and permissions
rsyslog creates the per-host directories itself when a message for a new host arrives. It uses its own default modes, which may be more open than you want. You can set the modes on the action:
ruleset(name="remote") {
action(type="omfile" dynaFile="PerHostFile"
fileCreateMode="0640" dirCreateMode="0750")
}Which user owns the new files depends on the distribution. Debian and Ubuntu run rsyslog as the syslog user. RHEL-family systems usually run it as root. If you set fileOwner or fileGroup, make sure that account exists.
On systems with SELinux enforcing, writing outside the usual log paths can fail with a denial. Keep the store under /var/log, where the file contexts already allow it. If you move it, check sudo ausearch -m avc -ts recent and label the new path properly.
Template choices and their risks
The template decides how your store is organised, and it also decides what a hostile or misconfigured sender can do.
| Property | Meaning | Caveat |
|---|---|---|
| `%HOSTNAME%` | Host name from the message | The sender controls it. Clients with a wrong or default name such as `localhost` merge into one directory. |
| `%FROMHOST%` | Name from a reverse DNS lookup of the sender's address | Depends on DNS being correct. |
| `%FROMHOST-IP%` | Source IP address | Always present, but changes if a host's address changes. |
| `%programname%` | Program name from the tag | Can be empty or contain odd characters. |
Choose the host identifier deliberately. %HOSTNAME% gives readable directories, but a client can claim any name. If you need names you can trust, use %FROMHOST-IP%, or combine it with a name check. The next chapter covers host names and time in more detail.
A tag containing a / would change the path the template builds. Recent rsyslog versions replace control characters in properties, but you can make the template safer by lowercasing the value and replacing unwanted characters with %programname:::secpath-replace%. This option is documented in the rsyslog property replacer, so check that your version supports it.
Behaviour to expect
- File handles stay open.
omfilekeeps recently used files open. The cache size is set bydynaFileCacheSize, which defaults to 10 files. With hundreds of host and program combinations, rsyslog closes and reopens files constantly, so raise the value on a busy receiver. - Rotation needs a signal. Because the files stay open, a rotated file keeps receiving data until rsyslog is told to reopen it. A
HUPsent in the logrotatepostrotatestep does this, as you will see in the chapter on the central store. - Disk growth is yours to manage. One directory per host and one file per program multiplies quickly. Estimate the daily volume per host and plan capacity before you onboard many clients.
- Messages from clients with a clock far off look misplaced. The receiver writes the timestamp from the message, so files can contain lines out of order.
Check that it works
From any client that forwards to this server:
logger -t demo "test message from $(hostname)"On the central server:
sudo ls /var/log/remote/
sudo tail /var/log/remote/*/demo.logYou should see a directory named after the client and a demo.log containing the line. If nothing appears, check these in order:
ss -tlnpshows rsyslogd listening on 514.- The firewall allows the client's address.
sudo rsyslogd -N1reports no errors.- The client's forwarding action points at the right name and port.
Exercise
A central receiver writes its remote messages into /var/log/remote/HOST/PROGRAM.log, but every remote message also appears in the receiver's own /var/log/syslog. Which setting in the receiver configuration is most likely missing or wrong, and why?
Show answer
The input is not bound to the dedicated ruleset. The input line must carry ruleset="remote" so that messages from imtcp are processed only by the actions in that ruleset. Without the binding they go through the default ruleset, which also writes to the local syslog file.
Exercise
The receiver has been running for weeks. After logrotate rotates /var/log/remote/web1/nginx.log, new messages still go into the rotated file nginx.log.1 instead of a fresh nginx.log. What is the cause, and what fixes it?
Show answer
omfile keeps the old file handle open, so it keeps writing to the renamed file. The logrotate stanza needs a postrotate step that sends HUP to rsyslog, for example /usr/bin/systemctl kill -s HUP rsyslog.service, so rsyslog closes and reopens its files.