Chapter 11 · Lesson 3 of 10
Monitoring and logs

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.

By the end you will

Read service logs with journalctl, cap journal size, and rotate a custom log file with logrotate.

Time
20 min
Level
Beginner
Check
4 questions
You need
  • 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:

bash
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 messages

Add -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:

bash
journalctl --disk-usage

On many Ubuntu installs the journal is persistent, stored under /var/log/journal. To set a cap, edit /etc/systemd/journald.conf:

ini
[Journal]
SystemMaxUse=500M

Apply it with sudo systemctl restart systemd-journald. To free space immediately without waiting, vacuum by size or age:

bash
sudo journalctl --vacuum-size=200M
sudo journalctl --vacuum-time=14d

Vacuuming 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:

output
/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:

output
    postrotate
        systemctl reload myapp >/dev/null 2>&1 || true
    endscript

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

bash
sudo logrotate -d /etc/logrotate.d/myapp

The -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 -f shows new lines when you trigger something.
  • journalctl --disk-usage is under your SystemMaxUse value after a restart of journald.
  • sudo logrotate -d /etc/logrotate.d/myapp prints no errors.
  • After a forced rotation you see app.log plus app.log.1, and later app.log.2.gz.
  • The app keeps writing to app.log and not to the rotated file.

Edge cases and gotchas#

  • Volatile journal. If /var/log/journal does not exist, logs live in memory under /run/log/journal and 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.1 after rotation. Use copytruncate or a postrotate reload.
  • copytruncate can lose lines. A few messages written during the copy may vanish. Prefer postrotate where the program supports it.
  • Permission errors on rotation. logrotate may refuse a directory writable by others. Add a su user group line or fix the mode, as the error message suggests.
  • Container logs are separate. journalctl will 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#

SymptomLikely causeFix
-- No entries --Wrong unit name or time rangeList 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 groupUse sudo, or add the user to systemd-journal
error: skipping "/var/log/myapp/app.log" because parent directory has insecure permissionsDirectory writable by group or othersFix the mode or add a su line to the config
Logs vanish after rebootJournal is volatileCreate /var/log/journal and restart journald
/var/log still hugeOld unrotated files or no config for that fileAdd a logrotate rule and run logrotate -d
Rotated file keeps growingApp holds the old file openUse copytruncate or a postrotate reload
logrotate: command not foundPackage missingInstall with sudo apt install logrotate
Config change ignoredSyntax error stops that fileRun logrotate -d and fix the reported line

Quick reference#

bash
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/myapp

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.

practice, nothing runs on your machine

Want a real machine? Free browser labs: Killercoda

Check what you learned4 questions
1Which command follows the logs of the unit myapp.service live?
2Which setting caps how much disk the persistent journal may use?
3When is copytruncate the right choice in logrotate?
4What does logrotate -d do?

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

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios