Exercise: centralized syslog with rsyslog

  1. Step 1 — Phase 1 — Configure the rsyslog server to receive logs (171)
  2. Step 2 — Phase 2 — Configure the rsyslog client to forward (172)
  3. Step 3 — Phase 3 — FAIL/FIX: catch the permission error + verify log reception
  4. Step 4 — Phase 4 (optional) — Verify that the queue prevents log loss when the server is down
  5. Illustration 1 — The server listens on port 514
  6. Illustration 2 — The client forwards logs over TCP
  7. Illustration 3 — FAIL — permission denied (error 2207)
  8. Illustration 4 — FIX — chown syslog → logs are received
  9. Illustration 5 — Real-world demo — SSH into client 172, server 171 receives the sshd logs

Estimated time: 45 minutes.

Deploy centralized logging with rsyslog on 2 Linux nodes: one node acts as the syslog server and receives logs on port 514,

the other acts as the client and forwards all of its logs to the server, which stores them split by host/program (/var/log/remote/<host>/<program>.log).

Verify that the client's logs show up on the server, and fix rsyslog's write-permission error.

The IP addresses in this exercise are illustrative addresses from the range reserved for documentation (RFC 5737): dattqh-nat = 192.0.2.21, dattqh-client = 192.0.2.22; when you do the lab, replace them with the real IPs of your machines.

Step 1 — Phase 1 — Configure the rsyslog server to receive logs (171)

The server first needs to load the input modules and open port 514 (imtcp/imudp).

# On dattqh-nat (192.0.2.21)
sudo tee /etc/rsyslog.d/10-remote-server.conf >/dev/null <<'EOF'
module(load="imudp")
input(type="imudp" port="514")
module(load="imtcp")
input(type="imtcp" port="514")
template(name="RemoteHostFile" type="string"
         string="/var/log/remote/%HOSTNAME%/%PROGRAMNAME%.log")
if ($fromhost-ip != "127.0.0.1") then {
    action(type="omfile" dynaFile="RemoteHostFile"
           fileCreateMode="0640" dirCreateMode="0755")
    stop
}
EOF
sudo mkdir -p /var/log/remote
sudo rsyslogd -N1
sudo systemctl restart rsyslog
sudo ss -lntup | grep ':514'

Note: Observed: rsyslogd -N1 prints "version 8.2312.0 … End of config validation run.

Bye." with no errors. ss -lntup | grep :514 shows rsyslogd in LISTEN on 0.0.0.0:514 + [::]:514 for both UDP and TCP.

Step 2 — Phase 2 — Configure the rsyslog client to forward (172)

The client needs an omfwd action pointing at server:514.

# On dattqh-client (192.0.2.22)
sudo tee /etc/rsyslog.d/90-forward.conf >/dev/null <<'EOF'
*.* action(type="omfwd"
           target="192.0.2.21"
           port="514"
           protocol="tcp"
           action.resumeRetryCount="100"
           queue.type="linkedList"
           queue.size="10000")
EOF
sudo rsyslogd -N1
sudo systemctl restart rsyslog
ss -tnp | grep ':514'
logger -p user.notice "LAB-SYSLOG-OK $(hostname) $(date +%H:%M:%S)"

Note: Observed: ss -tnp | grep :514 shows ESTAB 192.0.2.22:<port> 192.0.2.21:514 users:(("rsyslogd",...)). After logger, the message travels over this connection to the server.

Step 3 — Phase 3 — FAIL/FIX: catch the permission error + verify log reception

Note that rsyslog drops privileges to the syslog user ($PrivDropToUser syslog in /etc/rsyslog.conf).

# On server 171 — diagnose when /var/log/remote is empty
sudo journalctl -u rsyslog -n 20 --no-pager | grep -i "could not open"
sudo ls -ld /var/log/remote

# FIX: grant permissions to the user rsyslog drops to
sudo chown syslog:syslog /var/log/remote
sudo systemctl restart rsyslog

# Verify: after the client sends again, look at the file tree
sudo find /var/log/remote -type f -ls
sudo grep -rh "LAB-SYSLOG-OK" /var/log/remote

Note: Observed: before the fix, journalctl -u rsyslog has the line omfile: creating parent directories … Permission denied [try https://www.rsyslog.com/e/2207].

After chown + the client sending again: find /var/log/remote -type f lists dattqh-client/sshd.log, systemd.log, … owned by syslog:syslog with mode 0640.

Step 4 — Phase 4 (optional) — Verify that the queue prevents log loss when the server is down

The queue together with resumeRetryCount keeps messages when the forwarding action fails.

# 1) On the SERVER: stop receiving
sudo systemctl stop rsyslog

# 2) On the CLIENT: send marker messages while the server is down
for i in 1 2 3; do logger -p user.notice "QUEUE-TEST $i $(date +%s)"; done
ss -tnp | grep ':514'   # no more ESTAB (server down)

# 3) On the SERVER: start it again
sudo systemctl start rsyslog
sleep 3

# 4) On the SERVER: check that the messages were resent
sudo grep -rh "QUEUE-TEST" /var/log/remote

Note: Observed: while the server is down, ss -tnp | grep :514 on the client no longer shows ESTAB (or shows SYN-SENT).

A few seconds after the server comes back up, grep QUEUE-TEST on the server returns all 3 messages — nothing is lost, even though they were sent while the server was offline.

Illustration 1 — The server listens on port 514

rsyslogd -N1 validates OK; ss shows LISTEN on UDP+TCP 514

Config validation + ss -lntup: the rsyslog server listens on both UDP and TCP port 514.

Illustration 2 — The client forwards logs over TCP

cat 90-forward.conf; ss ESTAB to 171:514; logger test

omfwd over TCP to 192.0.2.21:514 (ESTAB) + logger sending a marker message.

Illustration 3 — FAIL — permission denied (error 2207)

journal: Could not open dynamic file ... discarding message; find returns nothing

The /var/log/remote directory is owned by root → the syslog user cannot create subdirectories → the message is discarded.

Illustration 4 — FIX — chown syslog → logs are received

after chown syslog:syslog, find lists files, grep finds LAB-SYSLOG-OK

After chown syslog:syslog: the client's logs are stored in the tree /var/log/remote/<host>/<prog>.log.

Illustration 5 — Real-world demo — SSH into client 172, server 171 receives the sshd logs

grep sshd.log on server 171: Failed/Accepted/Invalid user forwarded from client 172

*SSH into 192.0.2.22 (1 successful login + 1 wrong password): the sshd events on the client are forwarded over TCP 514 to the server and stored at /var/log/remote/dattqh-client/sshd.log —

centralized logging captures both legitimate Accepted and Failed/Invalid user (password guessing) events, with no need to log in to each machine.

Exercise: centralized syslog with rsyslog