# 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.

URL: https://levelrail.com/learn/monitoring/centralize-logs-with-loki-or-rsyslog/
Updated: 2026-11-27

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](https://levelrail.com/learn/monitoring/log-basics-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`:

```text
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`:

```text
*.* @@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](https://levelrail.com/learn/glossary/#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](https://levelrail.com/learn/server-basics/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](https://levelrail.com/learn/glossary/#reverse-proxy)) with HTTPS. The Compose basics are in [Docker Compose basics](https://levelrail.com/learn/docker/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:

```text
{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

| Symptom | Likely cause | Fix |
| --- | --- | --- |
| Nothing arrives on the receiver | Firewall blocks 514 or wrong IP | Check `ufw status` and test with `nc -vz 203.0.113.10 514` |
| `rsyslogd: error: ... invalid` from `rsyslogd -N1` | Config syntax error | Fix the line shown before restarting |
| `connection refused` in the sender's journal | Receiver not listening | Confirm `imtcp` is loaded and rsyslog restarted |
| Messages appear in the wrong directory | Template or hostname mismatch | Check `%HOSTNAME%` values and the template |
| Loki returns `too many outstanding requests` | Query too heavy | Narrow the time range and add label filters |
| Grafana shows `Data source connected, but no labels were received` | No logs pushed yet | Check the collector config and its logs |
| Port 3100 reachable from the internet | Published on all addresses | Bind to `127.0.0.1` or a private IP and fix the firewall |
| Disk full on receiver | No rotation or retention | Add logrotate and set a retention period |

## Quick reference

```text
# 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"
```

```text
{job="varlog"} |= "error"
```

## Related lessons

- [Log basics with journald and logrotate](https://levelrail.com/learn/monitoring/log-basics-journald-and-logrotate/)): the local logs you are shipping.
- [Grafana dashboards for CPU, memory and disk](https://levelrail.com/learn/monitoring/grafana-dashboards-for-cpu-memory-disk/)): Grafana also reads Loki.
- [Private networking between services](https://levelrail.com/learn/deploy-operate/private-networking-between-services/)): keep log ports off the internet.
- [Read container logs](https://levelrail.com/learn/deploy-operate/read-container-logs/)): where container output starts.
- [Set up a firewall with ufw](https://levelrail.com/learn/server-basics/set-up-a-firewall-with-ufw/)): restrict who can reach the receiver.

## Next

Start collecting numbers in [Prometheus and node exporter basics](https://levelrail.com/learn/monitoring/prometheus-and-node-exporter-basics/)).

## 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

- [Grafana Loki documentation](https://grafana.com/docs/loki/latest/)
- [Grafana Alloy documentation](https://grafana.com/docs/alloy/latest/)
