Sub topic 10
Building the Central Log Receiver
Assume you can edit files with sudo, manage systemd services, open a firewall port, and read basic rsyslog rules. This chapter configures a central server that accepts syslog messages over TCP and stores them in separate files for each sending host and program.
Receiver design
Each client sends a message to the receiver's TCP port 514. The receiver's imtcp input accepts the connection. A ruleset sends each message to omfile, which writes to a dynamic filename.
The template below creates paths such as:
/var/log/remote/web01/sshd.log
/var/log/remote/db01/postgresql.logThe hostname and program name come from fields in each syslog message.
Configure rsyslog
Create a receiver configuration file:
# /etc/rsyslog.d/10-receiver.conf
module(load="imtcp")
template(name="PerHostFile" type="string"
string="/var/log/remote/%HOSTNAME%/%programname%.log")
ruleset(name="remote") {
action(type="omfile"
dynaFile="PerHostFile"
createDirs="on"
dirCreateMode="0750"
fileCreateMode="0640")
}
input(type="imtcp" port="514" ruleset="remote")imtcp is the rsyslog input module for TCP syslog. The PerHostFile template uses properties from each message. createDirs="on" allows rsyslog to create a new directory when a previously unseen host sends a message.
The ruleset is attached only to the TCP input. This prevents local messages from being mixed into the remote log tree. Keep the directory under /var/log so normal log access controls and filesystem monitoring apply.
Validate the configuration before restarting:
sudo rsyslogd -N1
sudo systemctl restart rsyslog
sudo systemctl status rsyslog --no-pagerrsyslogd -N1 parses the configuration without starting a second daemon. Fix every reported error before restarting the service.
Open the network path
The receiver must allow inbound TCP port 514. For a host using firewalld, restrict access to the client network when possible:
sudo firewall-cmd --permanent \
--add-rich-rule='rule family="ipv4" source address="10.20.0.0/16" port port="514" protocol="tcp" accept"
sudo firewall-cmd --reloadA firewall rule does not replace client-side configuration. Each client must forward messages to the receiver's hostname or address and port 514.
| Component | Receiver setting |
|---|---|
| Transport | TCP |
| Port | 514 |
| Input module | `imtcp` |
| Storage root | `/var/log/remote` |
| File pattern | Hostname and program name |
Check the result
From a client, send a message with a distinctive program tag:
logger --tcp --server loghost.example.com --port 514 \
--tag receiver-test "message from $(hostname)"On the receiver, inspect the generated directory and file:
sudo find /var/log/remote -type f -name 'receiver-test.log' -print
sudo tail -n 5 /var/log/remote/*/receiver-test.logIf the file is absent, check the receiver's service journal, confirm that TCP port 514 is reachable, and verify that the client's hostname resolves to the intended receiver.
Exercise
Why does the configuration attach the remote ruleset to the imtcp input instead of writing every rsyslog message to the dynamic template?
Show answer
Attaching the ruleset to the input limits the per-host files to messages received from remote TCP clients. Local receiver messages remain under the receiver's normal local logging rules.