Log basics on Linux: journald, journalctl and logrotate
Services write logs to stdout, which journald stores, or to files, which logrotate rotates; cap both so logs never fill the disk and read them with journalctl.
- Updated Nov 27, 2026
- Markdown
Read service logs with journalctl, cap journal size, and rotate a custom log file with logrotate.
- An Ubuntu or Debian server with sudo
On a Linux server, services either write logs to stdout, where systemd’s journald stores them, or write to files under /var/log. Read the first kind with journalctl -u name -f, cap its disk use with SystemMaxUse, and rotate the second kind with logrotate so a single log file never fills the disk.
How it works#
A program started by systemd has its stdout and stderr captured by journald. You read them with journalctl, filtered by unit, time or priority. Programs that open their own files, such as nginx access logs or a custom app, write plain text under /var/log. Nothing trims those files by itself. logrotate runs daily from a systemd timer (or cron on older systems), renames old logs, compresses them and deletes the oldest.
Containers add a third path: the Docker logging driver stores stdout from each container, and by default that file grows without limit. See limit Docker log size and Docker logging drivers and rotation. The general idea is the same: every log has a cap. See the logs and journalctl glossary entries for the terms.
Step 1: Read the journal#
The basic commands, building on read logs with journalctl:
journalctl -u myapp # all logs for one unit
journalctl -u myapp -f # follow live
journalctl -u myapp --since "1 hour ago"
journalctl -u myapp -p err # errors and worse only
journalctl -b # since the last boot
journalctl -k # kernel messagesAdd -n 100 to limit lines and --no-pager when piping to grep. --since accepts times such as "2026-11-27 09:00", yesterday or "30 min ago". For services run by systemd, this is the first place to look when something fails.
Step 2: Cap the journal#
Check the current size:
journalctl --disk-usageOn many Ubuntu installs the journal is persistent, stored under /var/log/journal. To set a cap, edit /etc/systemd/journald.conf:
[Journal]
SystemMaxUse=500MApply it with sudo systemctl restart systemd-journald. To free space immediately without waiting, vacuum by size or age:
sudo journalctl --vacuum-size=200M
sudo journalctl --vacuum-time=14dVacuuming deletes old archived entries and cannot be undone. Make sure you do not need them for an incident review first.
Step 3: Rotate a plain log file#
Suppose your app writes /var/log/myapp/app.log. Create /etc/logrotate.d/myapp:
/var/log/myapp/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
copytruncate
}What each line does: daily rotates once a day, rotate 14 keeps 14 old files, compress gzips them, delaycompress leaves the newest rotated file uncompressed, missingok ignores a missing file, and notifempty skips empty logs.
The last choice is how the program keeps writing. With copytruncate, logrotate copies the file and empties the original in place. This suits programs that hold the file open and cannot reopen it, but lines written during the copy can be lost. The alternative is a postrotate script that tells the program to reopen its file:
postrotate
systemctl reload myapp >/dev/null 2>&1 || true
endscriptUse postrotate when the program supports a reload or a signal such as SIGUSR1. Do not use both options together for the same file.
Step 4: Dry run before trusting it#
sudo logrotate -d /etc/logrotate.d/myappThe -d flag prints what logrotate would do and changes nothing. Read it for errors such as a wrong path or a permission problem. To force a real rotation once, use sudo logrotate -f /etc/logrotate.d/myapp, and check the result with ls -l /var/log/myapp/.
Check it worked#
journalctl -u myapp -fshows new lines when you trigger something.journalctl --disk-usageis under yourSystemMaxUsevalue after a restart of journald.sudo logrotate -d /etc/logrotate.d/myappprints no errors.- After a forced rotation you see
app.logplusapp.log.1, and laterapp.log.2.gz. - The app keeps writing to
app.logand not to the rotated file.
Edge cases and gotchas#
- Volatile journal. If
/var/log/journaldoes not exist, logs live in memory under/run/log/journaland disappear at reboot. Create the directory and restart journald to make it persistent. - Rotated file still being written. A program that keeps the old file handle open writes into
app.log.1after rotation. Usecopytruncateor apostrotatereload. - copytruncate can lose lines. A few messages written during the copy may vanish. Prefer
postrotatewhere the program supports it. - Permission errors on rotation. logrotate may refuse a directory writable by others. Add a
su user groupline or fix the mode, as the error message suggests. - Container logs are separate.
journalctlwill not show the contents of a container unless the journald driver is set. See read container logs. - Logs can hold secrets. Request bodies and tokens sometimes land in logs. Restrict permissions on log files and do not paste them publicly.
Troubleshooting#
| Symptom | Likely cause | Fix |
|---|---|---|
-- No entries -- | Wrong unit name or time range | List units with systemctl list-units and widen --since |
Hint: You are currently not seeing messages from other users and the system. | User not in the right group | Use sudo, or add the user to systemd-journal |
error: skipping "/var/log/myapp/app.log" because parent directory has insecure permissions | Directory writable by group or others | Fix the mode or add a su line to the config |
| Logs vanish after reboot | Journal is volatile | Create /var/log/journal and restart journald |
/var/log still huge | Old unrotated files or no config for that file | Add a logrotate rule and run logrotate -d |
| Rotated file keeps growing | App holds the old file open | Use copytruncate or a postrotate reload |
logrotate: command not found | Package missing | Install with sudo apt install logrotate |
| Config change ignored | Syntax error stops that file | Run logrotate -d and fix the reported line |
Quick reference#
journalctl -u myapp -f
journalctl -u myapp --since "1 hour ago" -p err
journalctl --disk-usage
sudo journalctl --vacuum-size=200M
sudo logrotate -d /etc/logrotate.d/myapp
sudo logrotate -f /etc/logrotate.d/myappRelated lessons#
- Read logs with journalctl: the basics of filtering the journal.
- Limit Docker log size: stop container logs filling the disk.
- Fix a full disk on a Linux server: what to do when logs already ate the space.
- Centralize logs with Loki or rsyslog: send logs off the machine.
- Uptime checks with a script and timer: a script whose logs you now know how to read.
Next#
Ship logs to one place in centralize logs with Loki or rsyslog.
Try it: Read the journal and cap its size
Follow a service, check how much disk the journal uses, then dry run a logrotate rule.
Want a real machine? Free browser labs: Killercoda
Check what you learned
Frequently asked questions
How do I see live logs for a systemd service?
Run journalctl -u service-name -f. The -u flag selects the unit and -f follows new lines as they arrive, like tail -f.
How much disk space does the systemd journal use?
Run journalctl --disk-usage to see it. Limit it with SystemMaxUse in /etc/systemd/journald.conf or shrink it now with journalctl --vacuum-size=200M.
What is the difference between copytruncate and postrotate in logrotate?
copytruncate copies the log and empties the original in place, which suits programs that cannot reopen files but can lose a few lines. postrotate runs a command, such as a reload, so the program reopens a fresh file.
How do I test a logrotate config without rotating?
Run logrotate -d /etc/logrotate.d/yourfile. The -d flag is a dry run that prints what would happen and changes nothing.
Sources
Keep going: guides and tutorials
- Our Health Check Failed and the Deploy Went Live Anyway A postmortem from our own dogfood run: a failed readiness probe still received traffic. What broke, why two correct parts added up to a bug, and the fix.
- Zero Downtime Deploy for Docker Apps, With Rollback How a Docker deploy waits for readiness before cutover, holds the previous release for rollback, rolls back on its own, and how to rehearse it.