How to forward logs to a remote syslog server¶
A central log server keeps a copy of each machine's logs where an intruder or a failed disk on that machine can't remove them, and lets you search many machines in one place.
This guide configures a machine to send all its log messages to such a server over TCP, with a queue that holds messages while the server is unreachable.
It uses rsyslog on Debian, Ubuntu, the RHEL family and Gentoo, and syslog-ng on Arch.
Before you begin¶
- You need a shell with
sudoon the machine that sends the logs (the client). - You need a syslog server that accepts messages over TCP, usually on port 514, and its address.
Setting up the server is a separate task; on an
rsyslogserver it means loading theimtcpinput module and opening the port in its firewall. - Plain TCP syslog isn't encrypted or authenticated.
Use it only on a network you trust, such as a management network; across untrusted networks, use syslog over TLS (port 6514) instead.
This guide doesn't cover TLS: it needs certificates on both sides, and is configured with rsyslog's TLS network stream drivers (
osslorgtls) or syslog-ng'stransport("tls"); see their documentation.
Throughout this guide, replace this placeholder with your own value:
| Placeholder | Meaning | Example |
|---|---|---|
<SERVER> |
The IP address or host name of the central syslog server | 192.0.2.10 |
Steps¶
-
Check that the client can reach the server on TCP port 514:
If the command prints nothing, or
Connection refused, fix the network path or the server's firewall first: nothing you do on the client can work until this succeeds. -
Install the syslog daemon that will do the forwarding.
Ubuntu installs
rsyslogby default; Debian 13 doesn't, and keeps its logs only in the systemd journal. Install it where it's missing;apt-getkeeps an existing installation as it is:The journal passes every message on to
rsyslog(Debian and Ubuntu setForwardToSyslog=yesfor journald). On Debian, installingrsyslogalso starts writing local text files such as/var/log/syslogand/var/log/auth.log, which duplicate the journal on disk; the package's rule in/etc/logrotate.d/rsyslogrotates them iflogrotateis installed and scheduled (see How to rotate an application's logs).rsyslogis installed and running by default, and reads messages from the journal. Check:Arch keeps its logs only in the systemd journal, and
rsyslogisn't in its official repositories. Installsyslog-ng, which reads the journal and can forward over TCP:Gentoo leaves the choice of syslog daemon to the administrator; this guide starts from
sysklogd, as the lab does.sysklogd2.7 forwards messages only over UDP, which loses messages without notice when the server or the network is down. For TCP with a queue, replace it withrsyslog(ifrsyslogis already your syslog daemon, skip to step 3):This needs a synchronised ebuild repository (
emerge --sync). -
Configure forwarding.
Create
/etc/rsyslog.d/90-forward.conf:Create
/etc/rsyslog.d/90-forward.conf:Arch's
/etc/syslog-ng/syslog-ng.confdefines the sources_local, which reads the journal. Add a destination for the server and a log path to it at the end of the file:Create
/etc/rsyslog.d/90-forward.conf:In the
rsyslogrule, the selector*.*matches every facility and priority, and theomfwdaction sends the messages to<SERVER>over TCP. The queue settings give the action its own queue in memory, so a slow or unreachable server doesn't hold up local logging. Because the queue has a file name, it is disk-assisted: when the queue in memory fills up, and whenrsyslogstops (queue.saveOnShutdown="on"), messages are written to files in rsyslog's work directory, up to 100 MB.action.resumeRetryCount="-1"makesrsyslogretry indefinitely instead of dropping messages. If the server accepts only UDP, useprotocol="udp"instead (or@<SERVER>in the legacy syntax,transport("udp")for syslog-ng); messages sent while it is unreachable are then lost without notice. To forward less, use a narrower selector, such as*.warningorauth,authpriv.*(see Syslog facilities and priorities).syslog-ngkeeps messages for a TCP destination in memory while it reconnects; it retries every 10 seconds, as set bytime_reopen(10)in Arch's configuration. -
Check the configuration, then restart the daemon to apply it.
syslog-ng --syntax-onlyprints nothing when the configuration is valid:Check the configuration, then stop
sysklogdand startrsyslogin its place, at boot as well. Both daemons read the/dev/logsocket, so only one of them may run:sudo rsyslogd -N1 sudo rc-service sysklogd stop sudo rc-update del sysklogd default sudo rc-update add rsyslog default sudo rc-service rsyslog startGentoo's
rsyslogconfiguration, in/etc/rsyslog.d/50-default.conf, writes the local files differently fromsysklogd: for example, there is no/var/log/syslog, and daemon messages go to/var/log/daemon.log. Its rotation rules in/etc/logrotate.d/rsyslogtake effect only onceapp-admin/logrotateis installed and run regularly (see How to rotate an application's logs).
Verify¶
Send a test message, and check that the client has a TCP connection to the server:
$ logger -t forward-test "test message for the central log server"
$ ss -tn state established '( dport = :514 )'
Recv-Q Send-Q Local Address:Port Peer Address:Port
0 0 192.0.2.10:43822 192.0.2.10:514
The output lists one connection to port 514 of <SERVER>.
(In this example the client and the server are the same host, so the local and peer addresses are the same.)
Then, on the server, look for the message in the files where it stores remote messages; their location depends on the server's configuration.
For example, on an rsyslog server that writes each client's messages to its own file under /var/log/remote/:
$ sudo grep -r forward-test /var/log/remote/
/var/log/remote/client.example.com.log:Nov 7 14:02:11 client forward-test[2291]: test message for the central log server
Troubleshooting¶
- No connection in
ss, and the message never arrives - The daemon can't reach the server, or isn't running. Check the client's own log for its error:
sudo journalctl -u rsyslog -n 20(-u syslog-ng@defaulton Arch), orsudo tail /var/log/messageson Gentoo. Make sure step 1 succeeds. rsyslogd -N1reports an error with a line number- A quote or bracket is missing in
/etc/rsyslog.d/90-forward.conf. Compare it with the example; each parameter is writtenname="value". - Messages arrive late, all at once
- That is the queue at work: while the server was unreachable, messages were kept and are sent once the connection is back.
- Messages arrive twice
- The client has two forwarding rules, for example from an earlier
@@<SERVER>line in/etc/rsyslog.confor another file in/etc/rsyslog.d/. Search withgrep -r '<SERVER>' /etc/rsyslog.conf /etc/rsyslog.d/and keep one.
Related¶
- Reference: Syslog facilities and priorities, journalctl
- Explanation: Linux logging architecture
- How-to: How to search the logs for a problem, How to keep logs across reboots, How to rotate an application's logs, How to open a port in the firewall