0Pricing
Linux Server Deployment & SSH Mastery · Lesson

Log Rotation and Archiving

Implement `logrotate` to manage log file sizes, prevent disk exhaustion, and ensure efficient log archiving policies.

Log Rotation and Archiving is a free Linux Server Deployment & SSH Mastery lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Linux Server Deployment & SSH Mastery learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Log Rotation Matters

Server logs are essential for debugging and monitoring, but they can grow very large over time. If left unchecked, they can quickly fill up your disk space, leading to server instability or even crashes.

This is where log rotation comes in. It's a critical process for managing log file sizes efficiently.

Introducing `logrotate` Utility

Linux systems use a utility called logrotate to automate the management of log files. It's designed to:

  • Rotate logs (rename old ones)
  • Compress old logs to save space
  • Remove ancient log files
  • Run custom scripts after rotation

Most Linux distributions have logrotate pre-installed and configured to run daily via a cron job.

How `logrotate` Finds Instructions

logrotate reads its configuration from specific files. There are two main places:

  • /etc/logrotate.conf: The main configuration file, often containing global settings and default values.
  • /etc/logrotate.d/: A directory where individual applications and services place their own specific log rotation rules. Each file in this directory is treated as a separate configuration.

This modular approach makes it easy to manage rules for different services.

Setting Log Rotation Frequency

One of the first things to define is how often logs should be rotated. logrotate offers several directives for this:

  • daily: Rotate logs every day.
  • weekly: Rotate logs once a week.
  • monthly: Rotate logs once a month.
  • yearly: Rotate logs once a year.

Choose a frequency that balances log detail retention with disk space usage.

Keeping a Number of Old Logs

The rotate directive tells logrotate how many old log files to keep before deleting the oldest ones. For example, rotate 4 means it will keep the current log file, plus four rotated (archived) versions.

Once the limit is reached, the oldest rotated log is removed when a new rotation occurs.

Compressing Old Log Files

To save even more disk space, logrotate can compress old log files. The compress directive does exactly this, typically using gzip.

When this is enabled, your rotated logs will appear as .gz files (e.g., myapp.log.1.gz). This is highly recommended for most server logs.

Rotating by Size, Not Just Time

Sometimes, logs can grow very quickly, and waiting for a daily or weekly rotation isn't enough. The size directive allows you to rotate logs if they exceed a certain file size, regardless of the time frequency.

  • size 100k: Rotate if log file is larger than 100 kilobytes.
  • size 10M: Rotate if log file is larger than 10 megabytes.
  • size 1G: Rotate if log file is larger than 1 gigabyte.

If both a time frequency (e.g., daily) and size are specified, rotation happens if either condition is met.

Example `logrotate` Configuration

Here's a simple example of a configuration file you might find in /etc/logrotate.d/ for a hypothetical application's log file:

This config will rotate the log daily, keep 7 old copies, and compress them.

/var/log/my_app/access.log {
    daily
    rotate 7
    compress
    notifempty
    missingok
}

Manually Running `logrotate`

While logrotate usually runs automatically via cron, you can test or force a rotation manually. The main command is logrotate /etc/logrotate.conf.

To force a rotation (useful for testing your configurations), use the -f flag:

  • logrotate -f /etc/logrotate.d/my_app: Forces rotation for a specific config file.
  • logrotate -f /etc/logrotate.conf: Forces rotation for all configured logs.

Use the -d (debug) flag to see what it would do without actually rotating anything.

Quick Check: Log Retention

Consider the following logrotate configuration for /var/log/webserver/error.log:

/var/log/webserver/error.log { weekly rotate 4 compress notifempty }

If this configuration has been running for several weeks, how many *compressed* historical log files would you expect to find at any given time?

Recap: Mastering Log Management

In this lesson, we explored how to manage server logs effectively using logrotate. We learned that:

  • Log rotation prevents disk exhaustion.
  • logrotate uses configuration files in /etc/logrotate.conf and /etc/logrotate.d/.
  • Key directives include daily/weekly/monthly for frequency, rotate for retention, compress for saving space, and size for conditional rotation.

Proper log management is a cornerstone of stable server operations!

Frequently asked questions

Is the “Log Rotation and Archiving” lesson free?

Yes — the full text of “Log Rotation and Archiving” is free to read here on the web, and the Linux Server Deployment & SSH Mastery course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Linux Server Deployment & SSH Mastery course, upgrade to CoddyKit PRO.

What will I learn in “Log Rotation and Archiving”?

Implement `logrotate` to manage log file sizes, prevent disk exhaustion, and ensure efficient log archiving policies. You practise Linux Server Deployment & SSH Mastery with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Linux Server Deployment & SSH Mastery?

No prior experience is required. Linux Server Deployment & SSH Mastery on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Log Rotation and Archiving” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Linux Server Deployment & SSH Mastery lesson?

Yes. Every Linux Server Deployment & SSH Mastery lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. System Monitoring Tools
  2. Understanding System Logs
  3. Log Rotation and Archiving
  4. Centralized Monitoring and Alerting
← Back to Linux Server Deployment & SSH Mastery