Sub topic 11
Time sync and host identity
This chapter assumes you can edit rsyslog configuration as root. It also assumes you have read the earlier chapters on forwarding clients and the central receiver, so you know what the PerHostFile template does. Forwarding already works at this point. The remaining problem is whether the stored lines can be trusted: do they carry the right time, and do they carry the right host name?
Why a central store needs both
A central log store is only useful if you can merge lines from many servers and read them as one sequence of events. That depends on two properties:
- Time: every line must carry a timestamp that means the same instant on every server.
- Identity: every line must be attributed to exactly one machine, and two machines must never share a name.
If either property fails, the logs still arrive but become misleading. A database error that appears to happen before the web request that caused it sends you after the wrong cause. Two cloned servers that both call themselves web write into the same file, and you cannot tell them apart.
Keep clocks synchronised
Servers drift. A hardware clock can gain or lose seconds per day, and a virtual machine that was paused or migrated can jump by minutes. Run a time-sync service on every client and on the central host.
Most distributions use one of two services:
chronyis a full NTP client and the default on RHEL-family systems. It corrects large offsets quickly and handles intermittent networks well.systemd-timesyncdis a lightweight SNTP client that ships with systemd. It is adequate for log correlation at the second level.
Check the state of the clock on each host:
timedatectl
chronyc tracking
chronyc sources -vIn the timedatectl output, look for System clock synchronized: yes. In chronyc tracking, the System time line shows the current offset from the reference. For log correlation, an offset under a few tens of milliseconds is fine. Offsets of seconds or more will reorder events that happen close together.
Point every server at the same sources. In /etc/chrony.conf (or /etc/chrony/chrony.conf on Debian-family systems):
# Use your own internal time servers where you have them
pool 2.pool.ntp.org iburst
makestep 1.0 3makestep 1.0 3 lets chrony step the clock during the first three updates if the offset is larger than one second. This matters for freshly booted virtual machines. After editing, run sudo systemctl restart chronyd (the unit is chrony on Debian-family systems).
Use one time zone, and timestamps that carry it
Two separate things can make a timestamp ambiguous: the time zone, and the timestamp format on the wire.
For the time zone, the simplest policy is to set every server to UTC:
sudo timedatectl set-timezone UTCWith UTC everywhere you never have to ask which zone a file used, and daylight-saving changes cannot produce an hour of repeated or missing lines. You can still display local time on demand with journalctl or your own tooling.
For the wire format, the traditional syslog timestamp looks like Nov 28 03:51:54. It has no year, no time zone, and no fractional seconds. If a client in one zone sends this form to a receiver in another, the receiver cannot recover the true instant. Whether this applies to you depends on the rsyslog version and the template in use, so state the format explicitly instead of relying on the default. Use a template that sends an RFC 3339 style timestamp with offset and sub-second precision:
# /etc/rsyslog.d/50-forward.conf on each client (addition to the action)
action(type="omfwd"
target="loghost.example.com" port="514" protocol="tcp"
template="RSYSLOG_SyslogProtocol23Format"
queue.type="LinkedList"
queue.filename="fwd_queue"
queue.maxDiskSpace="1g"
queue.saveOnShutdown="on"
action.resumeRetryCount="-1")RSYSLOG_SyslogProtocol23Format is the built-in RFC 5424 format. It includes the full timestamp with time zone, the host name, the application name and the process ID. The receiver parses it automatically.
Two timestamps, two meanings
rsyslog keeps two timestamps for every message:
timereportedis the time the sender put in the message.timegeneratedis the time the receiving rsyslog accepted it.
For a healthy pipeline the two differ by milliseconds. A large gap tells you something. If it is steady, the sender's clock is wrong. If it grows, messages are queued somewhere, for example in the client's disk queue during an outage. This template on the receiver writes both so you can compare them:
# /etc/rsyslog.d/20-skew.conf on the central server
template(name="SkewCheck" type="string"
string="%timegenerated:::date-rfc3339% %timereported:::date-rfc3339% %HOSTNAME% %syslogtag%%msg%\n")
ruleset(name="remote") {
action(type="omfile" file="/var/log/remote-skew.log" template="SkewCheck")
action(type="omfile" dynaFile="PerHostFile")
}This replaces the ruleset(name="remote") block from the receiver chapter. Do not define the ruleset twice. Use the skew log only while you diagnose, because it duplicates every message.
Host identity: where the name comes from
The central file path uses %HOSTNAME%. That value is not looked up by the receiver. It is whatever the sender wrote into the message, which normally comes from the client's own host name. rsyslog offers other properties, and they differ in how far you can trust them:
| Property | Source | Trust |
|---|---|---|
| `%HOSTNAME%` | Host field inside the message | Set by the sender. Can be wrong or forged. |
| `%FROMHOST%` | Reverse DNS lookup of the sender's address | Depends on DNS being correct. |
| `%FROMHOST-IP%` | Source IP address of the connection | Set by the network. Hard to forge over TCP. |
The sender's name is also used as a directory name. A client with a misconfigured or hostile host name can write into a folder that belongs to another server. If your network is not fully trusted, build the path from the connection address instead:
template(name="PerIpFile" type="string"
string="/var/log/remote/%FROMHOST-IP%/%programname%.log")IP-based paths are stable and safe, but they are hard to read and break when addresses change. A common compromise is to use %HOSTNAME% on a trusted network, and to add TLS with StreamDriverAuthMode="x509/name" (covered in the TLS chapter) so that only clients holding a certificate from your CA can connect at all.
Setting good host names
Each server needs a unique, stable name. Set it with hostnamectl:
sudo hostnamectl set-hostname web01.example.com
hostnamectl --staticFollow these rules:
- Use fully qualified names such as
web01.example.com. A short name likewebis ambiguous across domains. - Never leave the default
localhostorlocalhost.localdomain. Every unconfigured machine would write to/var/log/remote/localhost/. - By default rsyslog may strip the domain from the host name. To keep the full name, set
global(preserveFQDN="on")in the client's rsyslog configuration. - After cloning a virtual machine or image, change the host name before the first boot into the network. Also regenerate
/etc/machine-id, because tools that identify a host by that ID treat clones as the same machine.
How the pieces fit
The diagram traces one message from a client to the file where it ends up, and shows where time and identity are fixed.
Common failures
- Lines appear in the wrong order across hosts. Check
chronyc trackingon each server. One unsynchronised host is enough to cause this. - Timestamps are off by exactly whole hours. The hosts use different time zones and the wire format has no zone. Switch to UTC and the RFC 5424 template.
- Several servers write to the same file. They share a host name. Check
hostnamectlon each and fix the duplicates. - A directory named
localhostappears. A server was never given a host name. - A server logs with the wrong year, or its logs jump a day. Its clock was far off at boot. Enable
makestepor check that the NTP source is reachable from that network.
Exercise
Your central server has the directories /var/log/remote/web and /var/log/remote/db, but you know there are four web servers. What is the most likely cause, which rsyslog property would you use to tell the machines apart even if their names were wrong, and how would you check what the clients are sending?
Show answer
The four web servers almost certainly share the short host name "web" (for example because they were cloned from one image), so their messages all land in one directory. %HOSTNAME% is taken from the message and cannot tell them apart. Use %FROMHOST-IP% in the path template, because it comes from the TCP connection's source address. To fix the root cause, give each server a unique FQDN with hostnamectl set-hostname. To see what clients send, temporarily log with a template that includes %HOSTNAME% and %FROMHOST-IP% together, or compare timereported and timegenerated with the SkewCheck template.