Read logs with journalctl
Systemd collects kernel, service and authentication messages in a binary journal. journalctl queries it far more precisely than grep in /var/log/syslog. It is the first tool to open when a service refuses to start or stops without explanation.
Useful filters
journalctl -u nginx
journalctl -u nginx -f
journalctl -u nginx --since "1 hour ago"
journalctl -u nginx --since today --until "12:00"
journalctl -p err -b
journalctl -n 100 --no-pager-utargets a systemd unit.-ffollows the journal live, liketail -f.--sinceand--untilaccept a date, a time or a relative expression.-p errkeeps priorityerrand above (warning,err,crit,alert,emerg).-blimits to the current boot,-b -1to the previous one.--no-pageravoids openingless, which is useful in a script.
Filters combine:
journalctl -u my-app -b -p err --no-pagerFind a boot error
systemctl status my-app -l --no-pager
journalctl -u my-app -b -p err
journalctl -b -p errstatus shows the exit code and last lines. Typical messages are "Failed to start", "address already in use", a missing path or a permission denial. journalctl -b -p err lists every error from the current boot, including units you have not identified yet.
For a service that failed before the current boot:
journalctl -u my-app -b -1Since systemd 240, -g filters content: journalctl -u nginx -g 'error'. Otherwise pipe to grep, but remember the output is paginated by default.
Control storage and rotation
journalctl --disk-usageBy default Debian may keep the journal in memory (/run/log/journal), so it is lost on reboot. For persistent history:
sudo mkdir -p /var/log/journal
sudo systemd-tmpfiles --create --prefix /var/log/journal
sudo systemctl restart systemd-journaldThen cap the size in /etc/systemd/journald.conf:
[Journal]
SystemMaxUse=500M
SystemMaxFileSize=50MThen sudo systemctl restart systemd-journald. To shrink immediately:
sudo journalctl --vacuum-size=200M
sudo journalctl --vacuum-time=14dThese delete the oldest entries. Keep enough history to diagnose an incident that started days ago.
Access and privacy
A regular user only sees their own messages. To read the whole journal, add them to the systemd-journal or adm group, then log in again:
sudo usermod -aG systemd-journal utilisateurLogs can contain tokens, URLs with parameters or email addresses. Never paste raw output on a public forum without reviewing it and masking secrets. Restricting read access is also a security measure.
Verify
After fixing a service, filter the last five minutes and confirm there is no error:
journalctl -u my-app --since "5 min ago" -p warning
journalctl --disk-usageIf the journal grows abnormally, look for a service writing in a loop before raising the limit.
Advertisement
Was this article helpful?
Read next
- Schedule tasks with cronWrite a reliable crontab, log its output and choose between cron and systemd timers.
- Monitor and free disk spaceMeasure real usage, find the biggest consumers and clean up without breaking a service.
- Manage services with systemdEssential systemctl commands, a custom unit for an application and reading its logs.