Sub topic 6
Running the components as systemd services
This chapter assumes you can edit files under /etc, you know basic systemctl commands, and you have already downloaded the node_exporter, prometheus, and alertmanager binaries. Grafana usually comes from a package and ships its own unit, so most of the work here concerns the three binaries.
Running a binary in a terminal is fine for a test. For a monitoring stack it is not enough. The stack must start at boot, restart after a crash, run without root privileges, and write its logs somewhere you can search. systemd provides all of this.
The pattern for every component
Each component gets the same four things:
- A dedicated system user with no login shell.
- A binary in
/usr/local/bin. - A configuration directory under
/etcand, if it stores data, a data directory under/var/lib. - A unit file in
/etc/systemd/system.
Create the users first. The --system flag makes an account without a home directory or password, and /usr/sbin/nologin blocks interactive login.
sudo useradd --system --no-create-home --shell /usr/sbin/nologin node_exporter
sudo useradd --system --no-create-home --shell /usr/sbin/nologin prometheus
sudo useradd --system --no-create-home --shell /usr/sbin/nologin alertmanager
sudo mkdir -p /etc/prometheus/rules /var/lib/prometheus
sudo mkdir -p /etc/alertmanager /var/lib/alertmanager
sudo chown -R prometheus:prometheus /etc/prometheus /var/lib/prometheus
sudo chown -R alertmanager:alertmanager /etc/alertmanager /var/lib/alertmanagerSeparate users limit the damage if one component is compromised. The Prometheus process can write only to its own data directory, and it cannot read Alertmanager's files.
node_exporter
The exporter needs no data directory because it keeps nothing on disk. The unit from the plan is enough:
[Unit]
Description=Prometheus Node Exporter
After=network-online.target
Wants=network-online.target
[Service]
User=node_exporter
ExecStart=/usr/local/bin/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.targetSave it as /etc/systemd/system/node_exporter.service. The sections do the following:
[Unit]describes the service and its ordering.After=network-online.targetstarts it once the network is up.Wants=pulls that target in without making the exporter fail if the target fails.[Service]sets the user, the command, and the restart policy.Restart=on-failurerestarts the process after a non-zero exit or a crash, but not after a cleansystemctl stop.[Install]says which target enables the unit.multi-user.targetis the normal state of a server without a graphical session.
Prometheus
Prometheus needs a data directory and a way to reload its configuration without a restart. A restart is costly because Prometheus replays its write-ahead log on startup, which can take a while on a large server.
[Unit]
Description=Prometheus
After=network-online.target
Wants=network-online.target
[Service]
User=prometheus
ExecStart=/usr/local/bin/prometheus \
--config.file=/etc/prometheus/prometheus.yml \
--storage.tsdb.path=/var/lib/prometheus \
--storage.tsdb.retention.time=15d \
--web.listen-address=127.0.0.1:9090
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
LimitNOFILE=65536
[Install]
WantedBy=multi-user.targetPoints worth knowing:
ExecReloadmakessystemctl reload prometheussendSIGHUP. Prometheus then re-readsprometheus.ymland the rule files. If the new file is invalid, it logs an error and keeps the old configuration.--web.listen-address=127.0.0.1:9090keeps the web interface off the public network. Grafana on the same host can still reach it. Securing remote access is covered in a later chapter.--storage.tsdb.retention.time=15dis the default. Writing it out makes the setting visible to the next administrator.LimitNOFILEraises the open-file limit, because Prometheus opens many files as data blocks accumulate.
Validate the configuration before every reload. promtool ships in the same archive as Prometheus.
promtool check config /etc/prometheus/prometheus.ymlThe command also checks the rule files named in rule_files, so one run covers both.
Alertmanager
Alertmanager follows the same pattern. It stores silences and notification state, so it needs a data path.
[Unit]
Description=Prometheus Alertmanager
After=network-online.target
Wants=network-online.target
[Service]
User=alertmanager
ExecStart=/usr/local/bin/alertmanager \
--config.file=/etc/alertmanager/alertmanager.yml \
--storage.path=/var/lib/alertmanager
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.targetCheck its configuration with amtool check-config /etc/alertmanager/alertmanager.yml.
Grafana
Install Grafana from the vendor's APT or RPM repository. The package creates the grafana user, the grafana-server.service unit, and the data directories, so you write no unit file. You only enable it:
sudo systemctl enable --now grafana-serverGrafana listens on port 3000 by default. Change settings in /etc/grafana/grafana.ini or with a drop-in file, not by editing the packaged unit. A package upgrade overwrites the packaged unit and discards your edits.
Starting and enabling
After you create or change any unit file, tell systemd to re-read its files. Then enable and start the services:
sudo systemctl daemon-reload
sudo systemctl enable --now node_exporter prometheus alertmanager
systemctl status prometheus --no-pagerenable makes the service start at boot. --now also starts it immediately. Without enable, the stack disappears after the next reboot, and nobody notices until a dashboard is empty.
Reading logs and status
systemd captures everything the process writes to standard output and standard error. Use journalctl:
journalctl -u prometheus -n 50 --no-pager
journalctl -u prometheus -fThe first command shows the last 50 lines. The second follows new lines as they arrive. Prometheus logs configuration errors here, for example a YAML mistake found during a reload.
Common failures
| Symptom in `systemctl status` | Likely cause | Fix |
|---|---|---|
| `status=203/EXEC` | Wrong path in `ExecStart`, or the binary is not executable | Check the path with `ls -l`, then `chmod +x` |
| `status=217/USER` | The `User=` account does not exist | Create the user with `useradd` |
| `permission denied` for a data directory in the log | The directory is owned by root | `chown` it to the service user |
| `address already in use` | Another process holds the port | Find it with `ss -ltnp`, then change the port or stop the other process |
| Service stays `activating (auto-restart)` | The process crashes at startup | Read the journal for the reason |
| Edits to a unit file have no effect | `daemon-reload` was skipped | Run `systemctl daemon-reload`, then restart |
On distributions with SELinux enforcing, such as RHEL and its derivatives, a binary in /usr/local/bin may fail to start even though the file permissions are correct. The audit log (ausearch -m avc -ts recent) shows the denial. Fix it with the correct file context or a local policy module. Do not disable SELinux.
Optional hardening
systemd can restrict a service further. These lines go in the [Service] section of the Prometheus unit. The first two make the system directories read-only, and the last three block privilege escalation and hide other users' home directories:
NoNewPrivileges=true
ProtectSystem=full
ProtectHome=true
PrivateTmp=true
ReadWritePaths=/var/lib/prometheusProtectSystem=full makes /usr, /boot, and /etc read-only for the service. Because /var/lib/prometheus is not under those paths, it stays writable. ReadWritePaths states the intent explicitly. Apply hardening one option at a time and restart after each, so you can tell which option breaks the service if something fails.
Exercise
You change --storage.tsdb.retention.time in /etc/systemd/system/prometheus.service from 15d to 30d, then run systemctl reload prometheus. The old retention still applies. What went wrong, and which commands fix it?
Show answer
A reload sends SIGHUP, which makes Prometheus re-read prometheus.yml and rule files only. Command-line flags are read once at process start, and systemd has not yet re-read the edited unit file. Run sudo systemctl daemon-reload so systemd loads the new ExecStart, then sudo systemctl restart prometheus so the process starts with the new flag.
Exercise
A new Alertmanager unit shows status=217/USER and the service keeps failing. What does that code mean, and what do you check?
Show answer
Exit status 217 means systemd could not switch to the account named in User=. Check that the alertmanager user exists with id alertmanager. If it does not, create it with useradd --system --no-create-home --shell /usr/sbin/nologin alertmanager, then restart the service.