Syslog and log rotation

Intermediate · 24 min

How rsyslog sorts messages by facility and priority into files and to other hosts, and how logrotate keeps those files from filling the disk.

Before thisjournald and journalctlsystemd timers

Better Stack · 24 min

Syslog and log rotation

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Better StackReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Syslog tags every message with a facility (who: kern, auth, mail, daemon, local0-7) and a priority (how bad: emerg to debug). rsyslog rules match on that pair and send the message somewhere.
  • On systemd machines journald receives logs first. rsyslog reads them from the journal or from a socket journald forwards to, and writes the classic /var/log text files.
  • Fresh Debian 12 and later installs do not include rsyslog, so there is no /var/log/syslog. The journal is the log.
  • A single @ forwards over UDP, @@ over TCP. TCP is the sensible default for remote logging.
  • logrotate's default renames the file and creates a new one. The program keeps writing to the renamed file until it is told, usually by SIGHUP in postrotate, to reopen.
  • copytruncate copies then empties the file in place, for programs that cannot reopen. It can lose lines written between the copy and the truncate.
  • A writer that did not open its log in append mode leaves a run of zero bytes at the front after a copytruncate.

Logs as files, and files that grow

Before the journal, Linux logging was a daemon called syslogd listening on a socket, /dev/log. Programs sent it a line, and its config file decided which file the line went into. That model is still here. rsyslog is its modern form, and many tools, from security scanners to log shippers, still expect text files under /var/log.

Text files have one problem the journal manages for you: they grow until the disk is full. So the syslog world comes with a second tool, logrotate. This lesson covers both. The journald lesson covered the journal itself, and the devops log aggregation lesson covers shipping logs at scale.

Facility and priority

Every syslog message carries two labels, defined in syslog(3):

  • Facility, roughly who sent it: kern, user, mail, daemon, auth, authpriv, cron, and local0 to local7 reserved for your own programs.
  • Priority, how serious: emerg, alert, crit, err, warning, notice, info, debug. These are the same eight levels journalctl -p uses.

An rsyslog rule is selector action, where the selector is facility.priority. I downloaded Debian 13’s rsyslog package (8.2504.0) and unpacked it on this Mac. Its /etc/rsyslog.conf, comments removed:

module(load="imuxsock") # provides support for local system logging
module(load="imklog")   # provides kernel logging support
$FileOwner root
$FileGroup adm
$FileCreateMode 0640
$IncludeConfig /etc/rsyslog.d/*.conf
*.*;auth,authpriv.none		-/var/log/syslog
auth,authpriv.*			/var/log/auth.log
cron.*				-/var/log/cron.log
kern.*				-/var/log/kern.log
mail.*				-/var/log/mail.log
user.*				-/var/log/user.log
*.emerg				:omusrmsg:*

Read it top down. Everything goes to /var/log/syslog except auth messages (.none excludes a facility). Auth goes to its own file with group adm, mode 0640, because it holds login details. *.emerg is written to every logged-in user’s terminal. The leading - on a path, per the rsyslog docs, skips syncing the file after every write, trading a few lines lost in a crash for speed. Note that the auth file has no -.

Where rsyslog sits next to journald

On a systemd machine journald gets messages first. rsyslog gets them one of two ways, per journald.conf(5): journald forwards each message to a socket rsyslog listens on (ForwardToSyslog=yes), or rsyslog reads the journal files itself with its imjournal module. Upstream’s default for forwarding is off. Debian turns it on: the same Debian 13 systemd package ships /usr/lib/systemd/journald.conf.d/syslog.conf containing just ForwardToSyslog=yes, and the rsyslog config above loads imuxsock to receive it.

Here common wisdom is out of date. “Check /var/log/syslog” is in countless guides, but Debian’s bookworm release notes say that from Debian 12 rsyslog “is no longer installed by default”. On a fresh Debian 12 or 13 server there is no /var/log/syslog, and journalctl is the place to look. Install rsyslog when you want text files or need to forward to a syslog collector.

Sending logs to another host

Central logging means a log server that outlives the machine that failed. In rsyslog’s legacy syntax, documented under actions:

*.*  @logs.example.net:514     # one @: UDP
*.*  @@logs.example.net:514    # two @: TCP

UDP drops messages silently when the network or receiver is busy, so TCP is the sensible default. The modern form of the same rule is action(type="omfwd" target="logs.example.net" port="514" protocol="tcp"), and the rsyslog docs recommend it over the legacy form. Plain syslog over TCP is not encrypted, so cross-network forwarding wants TLS. I did not set up a server and client here.

logrotate and the open file problem

logrotate runs once a day, from a systemd timer on current Debian (the timers lesson showed that unit). Its global config from the Debian logrotate 3.22.0 package is short:

weekly
rotate 4
create
include /etc/logrotate.d

Weekly, keep four, and create a new empty file after rotating. Packages drop their own rules into /etc/logrotate.d. rsyslog’s:

/var/log/syslog
/var/log/mail.log
/var/log/kern.log
/var/log/auth.log
/var/log/user.log
/var/log/cron.log
{
	rotate 4
	weekly
	missingok
	notifempty
	compress
	delaycompress
	sharedscripts
	postrotate
		/usr/lib/rsyslog/rsyslog-rotate
	endscript
}

And that rsyslog-rotate script:

if [ -d /run/systemd/system ]; then
    systemctl kill -s HUP --kill-whom=main rsyslog.service
fi

Why the signal? A process writes to a file through a file descriptor, not a name. Renaming syslog to syslog.1 does not change what rsyslog’s descriptor points at, so it keeps writing into syslog.1. SIGHUP tells rsyslog to close and reopen its files by name, which gives it the new empty syslog. sharedscripts runs that once for all six files, not six times.

delaycompress leaves syslog.1 uncompressed until the next rotation, so anything written to it in the moments before the HUP lands in a plain file, not one that was just gzipped. missingok and notifempty skip missing and empty files without complaint.

copytruncate, and what it costs

Some programs cannot be told to reopen. For them copytruncate copies the file, then truncates the original to zero in place, so the descriptor stays valid. The man page warns that lines written between the copy and the truncate are lost.

I ran the three cases on this Mac with a small Python writer that logs a line every half second, rotating 1.2 seconds in. This is macOS, not Linux, but the file-descriptor behaviour shown is the same POSIX behaviour.

== 1. rename, then create a new app.log (logrotate's default)
-- app.log.1:
line 1
line 2
line 3
line 4
line 5
line 6
-- app.log:
       0
== 2. copytruncate, writer opened the file in append mode
-- app.log.1:
line 1
line 2
line 3
-- app.log:
line 4
line 5
line 6
== 3. copytruncate, writer opened the file without append mode
-- app.log.1:
line 1
line 2
line 3
-- app.log:
0000000   \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0  \0
0000020   \0  \0  \0  \0  \0   l   i   n   e       4  \n   l   i   n   e
0000040        5  \n   l   i   n   e       6  \n

Case 1 is why rename needs a signal: without one, every line went to the old file and the new one stayed empty. Case 2 is copytruncate working. Case 3 is the trap: a writer without O_APPEND keeps its own offset, so after the truncate it writes at byte 21, and the file starts with 21 zero bytes. Tools that read the file see binary junk at the top. Prefer create plus a reload signal. Use copytruncate only when there is no other way.

About the video

The embed is Better Stack’s “Logrotate - Log Management on Linux Servers”, February 2024, clipped to the first 23:31. Its chapters cover a logrotate overview, the main config file, per-service files in logrotate.d, choosing between create and copytruncate, a practical Apache example and scheduling. The clip stops before the final chapter, which wires rotation into Better Stack’s own monitoring product. It schedules logrotate with cron, while current Debian uses a timer. It does not cover rsyslog, so the first half of this lesson stands on its own, with Better Stack’s rsyslog video in deeper. English was judged from the channel and English auto-captions. I judged the content from its chapters, not a full watch.

What was run: the macOS rotation demo above, and downloading and reading Debian’s rsyslog, logrotate and systemd packages. rsyslog, logrotate and journald behaviour is from their documentation (logrotate 3.22.0 man page, rsyslog docs, systemd 262 man pages), checked 2026-11-28.

Where this fits

The rsyslog unit you saw in the last two lessons carries a long list of Protect...= and Private...= lines. What they do is next: sandboxing services with systemd.

Check yourself

  1. 01What does the rsyslog rule `auth,authpriv.* /var/log/auth.log` do?

    It writes every message from the auth and authpriv facilities, at any priority, to /var/log/auth.log.

  2. 02After logrotate renames app.log to app.log.1, the application keeps writing into app.log.1. Why, and what is the usual fix?

    The process still holds an open file descriptor to the same file, and renaming does not change it. A postrotate script sends a signal, often SIGHUP, so the program reopens its log path and gets the new file.

  3. 03When is copytruncate the right choice, and what does it cost?

    When the program cannot be told to reopen its log. The cost is a small window between copying and truncating in which written lines are lost, and possibly a zero-filled gap if the writer did not use append mode.

  4. 04What is delaycompress for?

    It leaves the most recent rotated file uncompressed until the next cycle, so a program that is still finishing writes to it does not write into a file that was just gzipped.

  5. 05How do you forward everything to a central log server over TCP port 514 in rsyslog's legacy syntax?

    `*.* @@logs.example.net:514`. One @ would be UDP.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark