Scenario
You need to manage log growth and retention using logrotate. Your task is to create a custom rotation policy for a
sample log file and apply it manually so you can validate rotation and compression behavior before it runs on a schedule.
Uncontrolled log growth is a common cause of disk pressure. A clean rotation policy reduces risk while preserving evidence for incident response and troubleshooting.
Objective
- Ensure the logrotate package is installed.
- Create a test log file under
/var/log. - Define a custom rotation policy in
/etc/logrotate.d. - Force a rotation run with
logrotate -f. - Confirm rotated output exists and is compressed as expected.
- Review global defaults in
/etc/logrotate.conf.
What You’ll Practice
-
Installing and validating tooling via
apt,dnf, orpacman. -
Creating a drop-in logrotate policy under
/etc/logrotate.d. -
Interpreting common directives:
weekly,rotate,compress,missingok,notifempty. - Forcing a controlled rotation run to validate behavior.
- Verifying output files and understanding retention outcomes.
Walkthrough
# Pick the command that matches your distro:
# Debian/Ubuntu
sudo apt install logrotate -y
# RHEL/Fedora
sudo dnf install logrotate -y
# Arch
sudo pacman -S logrotate
logrotate is typically run on a schedule (often daily) to rotate logs according to policy. Installation ensures the binary, global config, and drop-in directory are present.
# Example outcomes (varies by distro):
logrotate is already the newest version.
# or
Nothing to do. Complete!
sudo touch /var/log/testapp.log
This creates a log target that mimics an application log under /var/log. You’ll attach a custom policy to this file
in the next step.
# Confirm the file exists:
ls -l /var/log/testapp.log
testapp.log.
sudo nano /etc/logrotate.d/testapp
# or
sudo vim /etc/logrotate.d/testapp
Drop-in policies under /etc/logrotate.d keep application-specific rules separated from global defaults. This one
rotates weekly, keeps 4 archives, compresses rotated output, ignores missing files, and skips empty logs.
/var/log/testapp.log {
weekly
rotate 4
compress
missingok
notifempty
}
sudo logrotate -f /etc/logrotate.d/testapp
The -f flag forces rotation regardless of whether the normal threshold has been met. This is how you validate a
policy safely during setup or troubleshooting.
# If nothing prints, that can still mean success.
# Use -v for verbose output if you want to see actions:
# sudo logrotate -vf /etc/logrotate.d/testapp
ls /var/log/testapp.log*
You should see the active log and at least one rotated file. With compress enabled, rotated output is typically gzip
compressed (.gz).
/var/log/testapp.log
/var/log/testapp.log.1.gz
/etc/logrotate.conf.
cat /etc/logrotate.conf
Global configuration sets baseline defaults and commonly includes /etc/logrotate.d for per-app rules. Knowing what’s
global helps you predict rotation behavior and troubleshoot retention issues.
# Example snippet (varies by distro):
weekly
rotate 4
create
include /etc/logrotate.d
Breakpoints
If the log is empty and you used notifempty, rotation may be skipped. Add a line to the log (for example
echo "test" | sudo tee -a /var/log/testapp.log) and force rotation again.
Re-run with verbose output: sudo logrotate -vf /etc/logrotate.d/testapp. Use this during validation and remove it
from automation.
Cleanup checklist
- Remove the test policy:
sudo rm -f /etc/logrotate.d/testapp - Remove the test log(s):
sudo rm -f /var/log/testapp.log*
Reference
logrotate: Rotate, compress, and retire log files according to policy./etc/logrotate.conf: Global logrotate defaults (often includes/etc/logrotate.d)./etc/logrotate.d/: Per-application rotation policies.-
logrotate -f <config>: Force a rotation run.-f: force rotation-v: verbose output
weekly: Rotate logs weekly.rotate <n>: Retainnarchives before removing the oldest.compress: Compress rotated logs (typically gzip).missingok: Do not error if the log file is missing.notifempty: Do not rotate empty logs.