Sub topic 1
Where logs come from: journald, syslog and files
A Linux server writes log data through three channels that overlap: the systemd journal, the syslog protocol, and plain files that applications manage themselves. Every later step in this tutorial depends on knowing which channel a message used. Rotation, retention and forwarding all act on a specific channel, and a mistake here produces gaps, duplicates, or a full disk.
Prerequisites
This chapter assumes you can do the following:
- Use the Linux command line and edit files as root or with
sudo. - Use basic systemd commands such as
systemctl statusandsystemctl restart. - Read hostnames, TCP ports and firewall rules at a basic level.
- Recognise the contents of
/var/log.
The working model
Four kinds of producers write logs:
- The kernel writes to an in-memory ring buffer. You read it with
dmesgorjournalctl -k. - systemd services write to standard output and standard error. systemd connects both streams to journald.
- Programs that call the syslog API (
syslog()in libc, or theloggercommand) send datagrams to the socket/dev/log. On systemd hosts this socket is owned by journald. - Programs that open their own log files, such as nginx, Apache, MySQL and many in-house applications. These bypass both journald and syslog.
The central component is systemd-journald, a daemon that collects the first three kinds into one structured, indexed store called the journal. rsyslog is a traditional syslog daemon. It reads messages and writes them to text files under /var/log according to rules. It can also forward them to other hosts. Later chapters use rsyslog for forwarding.
The systemd journal
The journal stores each entry as a set of fields, such as _SYSTEMD_UNIT, PRIORITY, _PID and MESSAGE. It stores them in a binary format, so you cannot read it with cat or grep. You query it with journalctl:
# The last 20 lines from one unit, without a pager
journalctl -u ssh.service -n 20 --no-pager
# Only errors and worse, from the current boot
journalctl -p err -b
# Kernel messages
journalctl -k
# Follow new entries live
journalctl -f
# How much space the journal uses
journalctl --disk-usageOn RHEL-family systems the SSH unit is sshd.service, not ssh.service.
Filtering by unit, priority or boot is the main advantage of the journal. Services do not need to be configured to log to a file. Anything that prints to stdout is captured and tagged with its unit name.
Persistent or volatile storage
Where the journal lives depends on the Storage= setting in /etc/systemd/journald.conf. The default is auto:
- If the directory
/var/log/journalexists, the journal is written there and survives reboots. - If it does not exist, the journal is written to
/run/log/journal, which is a memory-backed filesystem. All entries are lost on reboot.
Some distributions create /var/log/journal by default and others do not. If a host loses its logs after a crash, check this first. A rebooted server with a volatile journal has no record of what happened before the reboot. The next chapters show how to set Storage=persistent explicitly and how to cap the size.
Syslog and the role of rsyslog
Syslog is both a message format and a protocol. A syslog message carries a facility (the kind of source, such as auth or mail), a severity (from debug up to emerg), a timestamp, a hostname, a program name and the text. The next chapter covers facilities and severities in detail. For now, note that every syslog message has these two labels, and rsyslog rules use them to decide where messages go.
rsyslog receives messages from journald in one of two ways, depending on the distribution:
- It reads the journal directly with the
imjournalmodule. RHEL-family systems do this. - It reads a socket that journald forwards to, using
imuxsock. Debian-family systems do this.
You can see how your host is set up:
# Is the syslog socket present, and who owns it?
ls -l /dev/log
# Which input modules does rsyslog load?
grep -rhE 'imjournal|imuxsock' /etc/rsyslog.conf /etc/rsyslog.d/
# Is rsyslog running?
systemctl is-active rsyslog.serviceNot every system has rsyslog. A minimal installation may have only journald. In that case /var/log/syslog or /var/log/messages will not exist, and journalctl is the only local log reader.
Files in /var/log
The same event can appear in more than one place. An SSH login failure is in the journal under the SSH unit. rsyslog also writes it to an authentication file. The file name depends on the distribution:
| Purpose | Debian and Ubuntu | RHEL, Rocky, Alma, Fedora |
|---|---|---|
| General system messages | `/var/log/syslog` | `/var/log/messages` |
| Authentication and sudo | `/var/log/auth.log` | `/var/log/secure` |
| `/var/log/mail.log` | `/var/log/maillog` | |
| Kernel boot messages | `/var/log/kern.log` | in `/var/log/messages` |
Some files in /var/log are not text. wtmp and btmp record logins and failed logins in a binary format. Read them with last and lastb, not with cat. Lastlog and journal files are binary too.
Application directories such as /var/log/nginx/ or /var/log/mysql/ are written by the applications directly. Neither journald nor rsyslog sees those messages unless you configure it to. This matters for central logging. Forwarding everything rsyslog handles does not automatically include these files. You must tell rsyslog to read them (with the imfile module) or change the application to log through syslog.
Common errors
- Looking in the wrong place. An administrator greps
/var/log/syslogfor a service that only logs to the journal. Tryjournalctl -u <unit>first. - Assuming the journal is permanent. See the warning above.
- Assuming one copy. On a host where both journald and rsyslog store messages, the same event takes up space twice. Retention for each must be managed separately.
- Forgetting direct-to-file applications. These need their own rotation and their own forwarding.
- Running
journalctlwithout enough privilege. A normal user sees only their own entries unless they are in thesystemd-journaloradmgroup.
Seeing a message take both paths
You can follow one test message through the system with logger:
logger -t demo -p local4.notice "hello from the logging chapter"
# In the journal
journalctl -t demo -n 1 --no-pager
# In a file, if rsyslog is installed and a rule matches
sudo grep demo /var/log/syslog /var/log/messages 2>/dev/nullThe journal entry appears for certain. The file entry appears only if an rsyslog rule routes the local4 facility to a file. Many default configurations send unknown facilities to the general file, but not all of them. If the second command prints nothing, rsyslog has no matching rule. That is not a fault in journald.
Exercise
A server was rebooted after a crash. Running journalctl -b -1 shows "no persistent journal was found", yet /var/log/syslog still has entries from before the crash. What do these two facts tell you about the configuration, and what one-line check would confirm the cause?
Show answer
The journal is volatile: Storage=auto with no /var/log/journal directory means entries live in /run/log/journal and vanish at reboot. rsyslog is a separate component that writes text files to disk, so its history survived. Confirm with ls -d /var/log/journal, which fails when the directory is missing.
Exercise
nginx on a host writes to /var/log/nginx/access.log. You configure rsyslog to forward all of its input to a central server. Will the nginx access log arrive there? Why or why not?
Show answer
No. nginx opens and writes the file itself, so the messages never pass through journald or rsyslog. To forward them you must read the file with rsyslog's imfile module, or configure nginx to log to syslog.