هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
Know it before you install
A log stream = all lines with the same set of labels. You select streams with labels ({job="nginx", host="web1"}) and then filter the text (|= "error"). Few labels with few values = fast and cheap; a label with thousands of values (user ID, request ID) = slow and expensive.
| Item | Loki | Alloy (agent) |
|---|---|---|
| Package | loki (Grafana repository) | alloy (Grafana repository) |
| Service | loki.service | alloy.service |
| Config | /etc/loki/config.yml | /etc/alloy/config.alloy |
| Data | /var/lib/loki/ (you set it) | /var/lib/alloy/ (positions) |
| Ports | 3100/tcp (HTTP API), 9096/tcp (gRPC) | 12345/tcp (UI, localhost) |
Install on RHEL, Rocky, Alma and Fedora
Add the Grafana repository (the same as in the Grafana guide), then install Loki on the log server and Alloy on every server:
Install on Ubuntu and Debian
mkdir -p /etc/apt/keyrings
wget -q -O - https://apt.grafana.com/gpg.key | gpg --dearmor > /etc/apt/keyrings/grafana.gpg
echo "deb [signed-by=/etc/apt/keyrings/grafana.gpg] https://apt.grafana.com stable main" > /etc/apt/sources.list.d/grafana.list
apt-get update
apt-get install -y loki
apt-get install -y alloyFirst configuration: Loki with local storage and retention
The package config is a demo that keeps data in /tmp. Replace it with a real single-server config:
auth_enabled: false
server:
http_listen_address: 0.0.0.0
http_listen_port: 3100
grpc_listen_port: 9096
common:
path_prefix: /var/lib/loki
storage:
filesystem:
chunks_directory: /var/lib/loki/chunks
rules_directory: /var/lib/loki/rules
replication_factor: 1
ring:
kvstore:
store: inmemory
schema_config:
configs:
- from: 2026-01-01
store: tsdb
object_store: filesystem
schema: v13
index:
prefix: index_
period: 24h
limits_config:
retention_period: 744h
ingestion_rate_mb: 8
ingestion_burst_size_mb: 16
compactor:
working_directory: /var/lib/loki/compactor
retention_enabled: true
delete_request_store: filesystem| Setting | Why |
|---|---|
auth_enabled: false | One tenant (one team). Loki has no login then — protect the port. |
path_prefix: /var/lib/loki | Data survives reboots (not /tmp). |
schema: v13, store: tsdb | The current index format. |
retention_period: 744h + retention_enabled | Logs older than 31 days are deleted by the compactor. |
ingestion_rate_mb | Limits, so one noisy app can not flood Loki. |
mkdir -p /var/lib/loki && chown -R loki: /var/lib/loki
systemctl enable --now loki
systemctl restart loki
curl -s http://127.0.0.1:3100/ready
ready
firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="10.0.0.0/16" port port="3100" protocol="tcp" accept'
firewall-cmd --reloadFor big setups use object storage (S3, Azure Blob, GCS) and the distributed mode or the Helm chart in Kubernetes.
Send logs with Alloy
On every server, tell Alloy which logs to read and where Loki is:
local.file_match "varlogs" {
path_targets = [
{"__path__" = "/var/log/nginx/*.log", "job" = "nginx", "host" = constants.hostname},
{"__path__" = "/var/log/secure", "job" = "auth", "host" = constants.hostname},
]
}
loki.source.file "varlogs" {
targets = local.file_match.varlogs.targets
forward_to = [loki.write.default.receiver]
}
loki.source.journal "journal" {
forward_to = [loki.write.default.receiver]
labels = {"job" = "journal", "host" = constants.hostname}
}
loki.write "default" {
endpoint {
url = "http://loki.example.com:3100/loki/api/v1/push"
}
}Alloy must be allowed to read the logs (on Ubuntu the auth log is /var/log/auth.log):
usermod -aG adm,systemd-journal alloy
systemctl enable --now alloy
systemctl restart alloy
journalctl -u alloy -n 20Connect Grafana and search
In Grafana: Connections → Data sources → Add → Loki, URL http://loki.example.com:3100 (or provision it like the Prometheus data source). Then Explore → Loki.
The LogQL you use every day:
{job="nginx", host="web1"}
{job="nginx"} |= "error"
{job="nginx"} != "healthcheck" |~ "5[0-9]{2}"
{job="auth"} |= "Failed password"
{job="journal"} | json | level="error"
sum by (host) (count_over_time({job="auth"} |= "Failed password" [5m]))
sum by (status) (rate({job="nginx"} | pattern `<_> - - <_> "<_> <_> <_>" <status> <_>` [5m]))From the terminal: logcli query '{job="nginx"} |= "error"' --addr=http://loki.example.com:3100 --since=1h.
Real uses at work:
- One search for the logs of all servers and containers during an incident.
- Metrics from logs: failed logins per host, 5xx per minute — and alerts on them (Loki ruler → Alertmanager).
- Logs and metrics side by side in one Grafana dashboard.
Make it safe
Security checklist:
- Loki with
auth_enabled: falsehas no login: firewall 3100 to the agents' networks and Grafana only; or put a proxy with auth/TLS in front. - Use HTTPS (
loki.write→https://…) when logs cross networks. - Do not log secrets and personal data; drop or mask them in Alloy (
loki.processstages) before they leave the server. - Retention set and working (the compactor), so the disk does not fill.
- Few labels with few values; never IDs as labels.
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
| No logs in Grafana | Alloy can not reach Loki, or can not read the files. | journalctl -u alloy; curl http://loki:3100/ready from the server; check groups adm, systemd-journal. |
permission denied for /var/log/… in the Alloy log | The alloy user may not read the file. | Add it to adm; check file modes. |
entry too far behind / out of order | Old lines or wrong time on a server. | Fix the clock (chrony); do not re-send old files. |
Ingestion rate limit exceeded (429) | An app logs too much. | Raise ingestion_rate_mb, or drop noisy lines in Alloy. |
Queries are slow / too many outstanding requests | Huge time range or high-cardinality labels. | Shorter range; better label selectors; remove ID labels. |
| Disk fills although retention is set | Compactor retention off or wrong delete_request_store. | retention_enabled: true in compactor; check the Loki log. |
| Loki will not start after editing | YAML or a field from another Loki version. | journalctl -u loki; loki -config.file=/etc/loki/config.yml -verify-config. |
Next steps
- Quick how-tos: Search logs with LogQL, Ship logs with Fluent Bit.
- Command page: logcli. Another log shipper: Fluent Bit.
- The UI: Install and set up Grafana. Alerts from logs: Alertmanager.
- At work: Logs by labels, Loki in DevOps.
- Practise reading logs in an incident: Lab: investigate an SSH brute-force attack from the logs.
1Why should a user ID never be a Loki label?
Every value makes a new stream; thousands of values make Loki slow and expensive. Filter IDs in the text instead.
2Which query counts failed SSH logins per host in 5 minutes?
sum by (host) (count_over_time({job="auth"} |= "Failed password" [5m])).