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

URL: https://levelrail.com/learn/monitoring/log-basics-journald-and-logrotate/
Updated: 2026-11-27

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](https://levelrail.com/learn/deploy-operate/limit-docker-log-size/)) and [Docker logging drivers and rotation](https://levelrail.com/learn/docker/docker-logging-drivers-and-rotation/)). The general idea is the same: every log has a cap. See the [logs](https://levelrail.com/learn/glossary/#logs)) and [journalctl](https://levelrail.com/learn/glossary/#journalctl)) glossary entries for the terms.

## Step 1: Read the journal

The basic commands, building on [read logs with journalctl](https://levelrail.com/learn/server-basics/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](https://levelrail.com/learn/glossary/#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`:

```text
/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:

```text
    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](https://levelrail.com/learn/deploy-operate/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

```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
```

## Related lessons

- [Read logs with journalctl](https://levelrail.com/learn/server-basics/read-logs-with-journalctl/)): the basics of filtering the journal.
- [Limit Docker log size](https://levelrail.com/learn/deploy-operate/limit-docker-log-size/)): stop container logs filling the disk.
- [Fix a full disk on a Linux server](https://levelrail.com/learn/troubleshooting/fix-a-full-disk-on-a-linux-server/)): what to do when logs already ate the space.
- [Centralize logs with Loki or rsyslog](https://levelrail.com/learn/monitoring/centralize-logs-with-loki-or-rsyslog/)): send logs off the machine.
- [Uptime checks with a script and timer](https://levelrail.com/learn/monitoring/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](https://levelrail.com/learn/monitoring/centralize-logs-with-loki-or-rsyslog/)).

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

- [journalctl man page](https://man7.org/linux/man-pages/man1/journalctl.1.html)
- [journald.conf man page](https://man7.org/linux/man-pages/man5/journald.conf.5.html)
- [logrotate man page](https://man7.org/linux/man-pages/man8/logrotate.8.html)
