RackNerd Billboard Banner

How To Use Logrotate For Managing Log Files In Ubuntu Linux

Log files are essential for tracking what happens on your Ubuntu system. But if you don’t manage them, they can eat up disk space and become hard to sift through. That’s where Logrotate comes in. Logrotate is a built-in tool in Ubuntu that automatically rotates, compresses, and deletes old log files. Here’s how to use it to keep your system tidy and your logs under control.

What Is Logrotate?

Logrotate is a utility that manages log files created by system processes and applications. It helps by:

  • Rotating logs (renaming and archiving)
  • Compressing old logs to save space
  • Removing logs after a set period
  • Emailing logs to administrators

Logrotate runs automatically, usually via a daily cron job.


How Logrotate Works

Logrotate reads configuration files that tell it which logs to rotate, when, and how. The main configuration file is at /etc/logrotate.conf, but most settings live in /etc/logrotate.d/ as separate files for each service or application.


Basic Logrotate Configuration

1. Check If Logrotate Is Installed

Logrotate comes pre-installed on most Ubuntu systems. To check, run:

logrotate --version

If it’s not installed:

sudo apt update
sudo apt install logrotate

2. Understand The Configuration Structure

  • /etc/logrotate.conf: Main config file. It can include global settings and references to other config files.
  • /etc/logrotate.d/: Directory with individual config files for system services like Apache, MySQL, etc.

3. Reading a Typical Logrotate Config

Here’s an example for Nginx logs (/etc/logrotate.d/nginx):

/var/log/nginx/*.log {
    daily
    missingok
    rotate 14
    compress
    delaycompress
    notifempty
    create 0640 www-data adm
    sharedscripts
    postrotate
        [ -f /var/run/nginx.pid ] && kill -USR1 `cat /var/run/nginx.pid`
    endscript
}

What do these options mean?

  • daily: Rotate the log files every day.
  • rotate 14: Keep 14 old log files before deleting.
  • compress: Compress older files with gzip.
  • notifempty: Don’t rotate empty files.
  • create: Create new log files with specified permissions and ownership.
  • postrotate/endscript: Run commands after rotating logs (here, telling Nginx to reopen log files).

Customizing Logrotate For Your Logs

Let’s say you have a custom app writing to /var/log/myapp.log. Create a file called /etc/logrotate.d/myapp:

/var/log/myapp.log {
    weekly
    rotate 8
    compress
    missingok
    notifempty
    create 0644 root root
}
  • This will rotate the log weekly, keep 8 old logs, and compress them.

Forcing Log Rotation Manually

You can force logrotate to test or apply settings:

sudo logrotate /etc/logrotate.conf --debug
sudo logrotate /etc/logrotate.conf --force
  • --debug just shows what would happen.
  • --force actually runs the rotation now.

Tips For Using Logrotate

  • Monitor your disk space: Set logrotate to run more often if logs grow fast.
  • Test custom configs: Use --debug to ensure you don’t break application logging.
  • Set up email alerts: Use the mail option if you need notifications about rotated logs.
  • Secure your logs: Make sure rotated files have the right permissions.

Wrapping Up

Logrotate is the easiest way to keep log files from taking over your Ubuntu server. With a few tweaks, you can make sure your logs are rotated, compressed, and pruned—automatically. Check your /etc/logrotate.d/ directory and tweak your configs as needed.

Got questions or want a sample config? Drop them in the comments below!


Keywords: Ubuntu, Logrotate, Linux log management, rotate logs, Ubuntu server logs, log file rotation

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
RackNerd Billboard Banner
© 2025 Computer Everywhere
Your Everyday Guide to the Digital World.
Terms of Service | Privacy Policy
Copy link