Chapter 11 · Lesson 4 of 10
Monitoring and logs

Centralize logs with rsyslog or Loki

Centralizing logs means shipping them from each server to one searchable place, either with rsyslog forwarding over TCP or with a collector that pushes to Grafana Loki.

By the end you will

Understand two ways to centralize logs and set up a minimal rsyslog forward or a Loki stack on a private network.

Time
30 min
Level
Intermediate
Check
4 questions
You need
  • Two Linux servers on a private network, or one server for testing
  • Docker Compose installed for the Loki example

How logs travel to one place

A collector on each server ships log lines to one store, so you can search every server from one place.
  1. Each app writes logs to stdout or a file on its own server, and a collector agent reads them.
  2. The collector adds labels such as host and job and ships the lines to one log store.
  3. You search every server at once from Grafana with a query such as error lines for one job.
  4. The store returns the matching lines in time order, even if the original server is gone.
Static diagram
  1. Appstdout and filesCollector
  2. Collectorlabels and linesLog store
  3. GrafanaqueryLog store
  4. Log storeresultsGrafana

Centralizing logs means sending a copy from every server to one searchable place, so they survive a rebuilt or dead host. Two common ways are rsyslog forwarding, which is a one-line rule over TCP, and a collector that pushes to Grafana Loki, where you search with LogQL. Either way, keep the receiving ports on a private network.

How it works#

Every server writes logs locally, as covered in log basics with journald and logrotate. A forwarder reads those logs and sends them over the network to a receiver, which stores them and offers search. The diagram above shows the path: source, collector, store, search.

Why bother? When a server crashes, fills its disk or is rebuilt, its local logs go with it, and that is the moment you need them. A central copy also lets you search one place instead of logging in to five machines, and keeps evidence if a server is compromised.

Two approaches fit a small setup:

  • rsyslog forwarding. rsyslog is the syslog daemon already installed on most Ubuntu and Debian servers. It can send messages to another rsyslog over TCP. No new software on the senders, and plain files on the receiver.
  • Loki with a collector. Grafana Loki stores log streams indexed by labels. A collector on each host tails files or the journal and pushes to Loki. You read them in Grafana with LogQL. Grafana documents Alloy as its collector; check the Grafana docs for the current recommended agent before you install one.

Step 1: Forward with rsyslog#

On the receiver (the central log server), create /etc/rsyslog.d/10-receive.conf:

output
module(load="imtcp")
input(type="imtcp" port="514")

template(name="PerHost" type="string"
         string="/var/log/remote/%HOSTNAME%/%PROGRAMNAME%.log")
if $fromhost-ip != '127.0.0.1' then ?PerHost
& stop

Create the directory and check the syntax before restarting:

bash
sudo mkdir -p /var/log/remote
sudo rsyslogd -N1
sudo systemctl restart rsyslog

On each sender, create /etc/rsyslog.d/90-forward.conf:

output
*.* @@203.0.113.10:514

@@ means TCP, a single @ means UDP. TCP is the better default because UDP drops messages silently. Restart rsyslog on the sender and check with logger "hello from web1". The line should appear under /var/log/remote/web1/ on the receiver.

Port 514 has no authentication or encryption here. Allow only your sender addresses through the firewall, for example:

bash
sudo ufw allow from 203.0.113.20 to any port 514 proto tcp

Do not open 514 to the whole internet. Anyone could read your logs’ destination or flood the disk. If senders are in different networks, put them on a VPN. Keep a second SSH session open while you change firewall rules, so you cannot lock yourself out. See set up a firewall with ufw.

Step 2: Run Loki and a collector#

A minimal Compose file for a test stack on a private host:

yaml
services:
  loki:
    image: grafana/loki:latest
    command: -config.file=/etc/loki/local-config.yaml
    ports:
      - "127.0.0.1:3100:3100"
    volumes:
      - loki-data:/loki
  grafana:
    image: grafana/grafana:latest
    ports:
      - "127.0.0.1:3000:3000"
    volumes:
      - grafana-data:/var/lib/grafana
volumes:
  loki-data:
  grafana-data:

Both ports bind to 127.0.0.1, so only the host itself reaches them. Pin the image tags to a specific version instead of latest before using this for real. Reach Grafana through an SSH tunnel or a reverse proxy with HTTPS. The Compose basics are in Docker Compose basics.

Each source host then needs a collector that tails logs and pushes them to Loki’s /loki/api/v1/push endpoint. Configure it from the Grafana Alloy documentation, with a label such as job="varlog" on files under /var/log. If senders live on other machines, Loki must be reachable from them, which means a private network or VPN, never a public port.

Step 3: Query with LogQL#

In Grafana, add Loki as a data source with the URL http://loki:3100 (the Compose service name), then open Explore. A query has a stream selector and optional line filters:

output
{job="varlog"}
{job="varlog"} |= "error"
{job="varlog"} != "healthcheck"

The first selects the stream, the second keeps lines containing error, the third drops noise. Choose labels that are few and stable, such as job and host. Putting a request ID in a label creates a huge number of streams and slows Loki.

Check it worked#

  • On the receiver, ls /var/log/remote/ lists the sender’s hostname after logger "test".
  • sudo ss -ltnp | grep 514 shows rsyslog listening, and only reachable from allowed hosts.
  • curl -s http://127.0.0.1:3100/ready on the Loki host returns ready once it has started.
  • In Grafana Explore, {job="varlog"} returns recent lines.
  • From a machine not on the allow list, a connection to port 514 or 3100 times out.

Edge cases and gotchas#

  • UDP loses messages. A single @ gives no delivery guarantee. Use @@ for TCP.
  • Plain syslog is unencrypted. On an untrusted path, use TLS or a VPN. Logs often contain usernames, IPs and sometimes secrets.
  • Receiver disk fills. Centralized logs still need rotation. Apply the same logrotate rules on the receiver and set retention in Loki.
  • Sender queues when the receiver is down. rsyslog can buffer in memory by default, and can lose messages on a long outage. Configure a disk-assisted queue if that matters to you.
  • High-cardinality labels. Per-request values as labels make Loki slow and large. Keep them in the log line.
  • A single central server is a single point. If it dies you still have local copies. Do not delete local logs until you trust the central store.

Troubleshooting#

SymptomLikely causeFix
Nothing arrives on the receiverFirewall blocks 514 or wrong IPCheck ufw status and test with nc -vz 203.0.113.10 514
rsyslogd: error: ... invalid from rsyslogd -N1Config syntax errorFix the line shown before restarting
connection refused in the sender’s journalReceiver not listeningConfirm imtcp is loaded and rsyslog restarted
Messages appear in the wrong directoryTemplate or hostname mismatchCheck %HOSTNAME% values and the template
Loki returns too many outstanding requestsQuery too heavyNarrow the time range and add label filters
Grafana shows Data source connected, but no labels were receivedNo logs pushed yetCheck the collector config and its logs
Port 3100 reachable from the internetPublished on all addressesBind to 127.0.0.1 or a private IP and fix the firewall
Disk full on receiverNo rotation or retentionAdd logrotate and set a retention period

Quick reference#

output
# sender: /etc/rsyslog.d/90-forward.conf
*.* @@203.0.113.10:514

# receiver: /etc/rsyslog.d/10-receive.conf
module(load="imtcp")
input(type="imtcp" port="514")
bash
sudo rsyslogd -N1 && sudo systemctl restart rsyslog
logger "hello from web1"
output
{job="varlog"} |= "error"

Next#

Start collecting numbers in Prometheus and node exporter basics.

Check what you learned4 questions
1In an rsyslog rule, what does @@ mean?
2Which is the safe way to expose port 514 or Loki's port 3100?
3In LogQL, what does |= "error" do?
4Why does Loki keep storage small?

Frequently asked questions

Why centralize logs?

Logs on a crashed or rebuilt server are lost or unreachable. A central copy survives the server, lets you search across hosts in one place and keeps evidence after an incident.

How do I forward logs with rsyslog?

On the sender add a rule like *.* @@203.0.113.10:514 to a file in /etc/rsyslog.d/. The double @@ means TCP, a single @ means UDP. The receiver must load imtcp and listen on that port.

What is Loki and how is it different from Elasticsearch?

Loki is a log store from Grafana Labs that indexes labels rather than the full text of each line. It is lighter to run, and you search by label first and then filter text with LogQL.

What is a LogQL query example?

{job="varlog"} |= "error" selects logs with the label job equal to varlog and keeps only lines containing the text error.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios