logrotate: Linux Log Rotation Settings and How to Test with -d, -f
logrotate is a Linux program that replaces ever-growing log files with new ones at a set interval or size and compresses or deletes the old files. On an AlmaLinux 9 server, separate log and config files were created in a test folder, previewed with -d and then rotated with -f, which showed how directives such as rotate, compress and dateext change the names and number of log files without touching the production config.
The output below was run under the root account on an AlmaLinux 9.8 server and on a Debian 12 server. The config files and timers in production were only read, and the rotation tests were done only on logs inside the test folder. The test folder path and times were replaced with example values.
1️⃣ How logrotate Works
logrotate is a command that a systemd timer runs once a day. When it runs, it checks the logs listed in the config files one by one, rotates them and exits. The installed version and default paths are checked with --version.
logrotate --versionlogrotate 3.18.0
Default mail command: /bin/mail
Default compress command: /bin/gzip
Default uncompress command: /bin/gunzip
Default compress extension: .gz
Default state file path: /var/lib/logrotate/logrotate.status
ACL support: yes
SELinux support: yesOn the AlmaLinux 9.8 server, logrotate was 3.18.0. It uses /bin/gzip for compression, and the time each log was last rotated is recorded in the state file /var/lib/logrotate/logrotate.status.
The once-a-day schedule is defined in logrotate.timer.
systemctl cat logrotate.timer# /usr/lib/systemd/system/logrotate.timer
[Unit]
Description=Daily rotation of log files
Documentation=man:logrotate(8) man:logrotate.conf(5)
[Timer]
OnCalendar=daily
AccuracySec=1h
Persistent=true
[Install]
WantedBy=timers.targetOnCalendar=daily is a schedule that runs every day at 00:00. According to the official manual, Persistent=true makes the timer run right away at the next boot if the server was off and missed the run time.
systemctl list-timers logrotate.timerNEXT LEFT LAST PASSED UNIT ACTIVATES
Fri 2026-10-30 00:00:00 UTC 14h left Thu 2026-10-29 00:00:00 UTC 9h ago logrotate.timer logrotate.service
1 timers listed.
Pass --all to see loaded but inactive timers, too.The NEXT column is the next run time, and the LAST column is the time it last ran. The logrotate.service that the timer runs contained a line that passes the production config file /etc/logrotate.conf.
grep ExecStart /usr/lib/systemd/system/logrotate.serviceExecStart=/usr/sbin/logrotate /etc/logrotate.confThe relationship between the timer, the config file and the state file is shown below.
2️⃣ Default logrotate.conf Settings
/etc/logrotate.conf is the file that sets the defaults applied to all logs. The default file on the AlmaLinux 9.8 server was read.
cat /etc/logrotate.conf# see "man logrotate" for details
# global options do not affect preceding include directives
# rotate log files weekly
weekly
# keep 4 weeks worth of backlogs
rotate 4
# create new (empty) log files after rotating old ones
create
# use date as a suffix of the rotated file
dateext
# uncomment this if you want your log files compressed
#compress
# packages drop log rotation information into this directory
include /etc/logrotate.d
# system-specific logs may be also be configured here.Excluding comments, there are five lines of directives. weekly rotates every week, and rotate 4 keeps up to four rotated files. create makes a new empty log with the same name right after rotation, and dateext appends a date instead of a number to the end of rotated file names. compress is commented out, so the default settings do not compress.
The include /etc/logrotate.d line reads in the files in that folder one by one. When a package is installed, a file holding that service's log paths and directives is created in this folder, and any directive not written in that file follows the defaults above.
As the comment at the top of the file says, global directives do not apply to an include that comes before them. To change a default, write it above the include line.
3️⃣ logrotate Configuration Directives
A per-service config lists directives one per line inside braces after the log path. A three-line test log, app.log, was created in the test folder with printf, and a test config, test.conf, was written to rotate that log.
cat /home/user/logtest/test.conf/home/user/logtest/logs/app.log {
daily
rotate 3
compress
delaycompress
missingok
notifempty
create 0640 root root
postrotate
echo "postrotate: app.log rotated"
endscript
}Commonly used directives are shown below.
| Directive | Task |
|---|---|
| daily · weekly · monthly | Rotation interval — day · week · month |
| rotate 3 | Keeps up to 3 rotated files and deletes older ones |
| size 1k | Rotates when the file exceeds 1KB, regardless of the interval (k · M · G) |
| compress | Compresses rotated files with gzip |
| delaycompress | Compresses the just-rotated file at the next rotation |
| missingok | Skips without an error if the log file is missing |
| notifempty | Does not rotate empty logs |
| create 0640 root root | Creates a new empty log right after rotation with the given permissions · owner · group |
| copytruncate | Copies the log to keep it, then empties the original file |
| dateext | Date instead of a number at the end of rotated file names |
| postrotate … endscript | Command to run after rotation finishes |
| su user group | User and group to use when rotating |
Between postrotate and endscript goes the shell command to run after rotation. In the official manual's example, this spot sends a HUP signal to syslogd so that it reopens its log files. The test config has a single echo line just to check whether it runs.
4️⃣ Testing with logrotate -d
-d is an option that does not actually rotate anything and only prints the process of reading the config and deciding which logs to rotate. -s is the option that sets the state file path. In the test, the state file in the test folder was specified so that its records would not mix with the production state file.
logrotate -d -s /home/user/logtest/state /home/user/logtest/test.confWARNING: logrotate in debug mode does nothing except printing debug messages! Consider using verbose mode (-v) instead if this is not what you want.
reading config file /home/user/logtest/test.conf
Reading state from file: /home/user/logtest/state
error: error opening state file /home/user/logtest/state: No such file or directory
Allocating hash table for state file, size 64 entries
Handling 1 logs
rotating pattern: /home/user/logtest/logs/app.log after 1 days (3 rotations)
empty log files are not rotated, old logs are removed
considering log /home/user/logtest/logs/app.log
Creating new state
Now: 2026-10-29 09:39
Last rotated at 2026-10-29 09:00
log does not need rotating (log has already been rotated)The WARNING on the first line notes that debug mode changes nothing. The error line appeared because the state file did not exist yet. -d does not create a state file, so running -d without a state file shows this line.
after 1 days (3 rotations) on the rotating pattern line is the result of reading daily and rotate 3. The last line was log has already been rotated. The Now line just above it showed 09:39, and the Last rotated at line showed 09:00. A log with no record in the state file takes the top of the current hour as its last rotation time like this, so a daily log becomes due for rotation only on the next day. That is why -f is used to force a rotation when checking results right after changing a config.
5️⃣ logrotate -f Forced Rotation and the -v Option
-f is an option that rotates right away regardless of interval and size conditions, and -v prints the rotation process line by line. -f and -v were run together with the same state file and config.
logrotate -f -v -s /home/user/logtest/state /home/user/logtest/test.confreading config file /home/user/logtest/test.conf
Creating stub state file: /home/user/logtest/state
Reading state from file: /home/user/logtest/state
Allocating hash table for state file, size 64 entries
Handling 1 logs
rotating pattern: /home/user/logtest/logs/app.log forced from command line (3 rotations)
empty log files are not rotated, old logs are removed
considering log /home/user/logtest/logs/app.log
Creating new state
Now: 2026-10-29 09:39
Last rotated at 2026-10-29 09:00
log needs rotating
rotating log /home/user/logtest/logs/app.log, log->rotateCount is 3
dateext suffix '-20261029'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
previous log /home/user/logtest/logs/app.log.1 does not exist
renaming /home/user/logtest/logs/app.log.3.gz to /home/user/logtest/logs/app.log.4.gz (rotatecount 3, logstart 1, i 3),
old log /home/user/logtest/logs/app.log.3.gz does not exist
renaming /home/user/logtest/logs/app.log.2.gz to /home/user/logtest/logs/app.log.3.gz (rotatecount 3, logstart 1, i 2),
old log /home/user/logtest/logs/app.log.2.gz does not exist
renaming /home/user/logtest/logs/app.log.1.gz to /home/user/logtest/logs/app.log.2.gz (rotatecount 3, logstart 1, i 1),
old log /home/user/logtest/logs/app.log.1.gz does not exist
renaming /home/user/logtest/logs/app.log.0.gz to /home/user/logtest/logs/app.log.1.gz (rotatecount 3, logstart 1, i 0),
old log /home/user/logtest/logs/app.log.0.gz does not exist
log /home/user/logtest/logs/app.log.4.gz doesn't exist -- won't try to dispose of it
renaming /home/user/logtest/logs/app.log to /home/user/logtest/logs/app.log.1
creating new /home/user/logtest/logs/app.log mode = 0640 uid = 0 gid = 0
running postrotate script
postrotate: app.log rotatedBecause of rotate 3, renaming lines that shift the numbers up by one through app.log.4.gz came first, followed by does not exist lines since there were no rotated files yet. Next, app.log was renamed to app.log.1, and a new empty app.log was created according to create 0640 root root. The last line is the output of the echo in postrotate.
ls -l /home/user/logtest/logs/app.log*-rw-r----- 1 root root 0 Oct 29 09:39 /home/user/logtest/logs/app.log
-rw-r--r-- 1 root root 128 Oct 29 09:39 /home/user/logtest/logs/app.log.1The new app.log was created with the 0640 permissions set in create, and app.log.1 kept the permissions of the original file.
A line was added to the end of app.log with printf.
printf '2026-10-29 10:00:01 request GET /index.html 200\n' >> /home/user/logtest/logs/app.logRunning it once more without -v then printed only the echo output from postrotate.
logrotate -f -s /home/user/logtest/state /home/user/logtest/test.confpostrotate: app.log rotatedls -l /home/user/logtest/logs/app.log*-rw-r----- 1 root root 0 Oct 29 09:39 /home/user/logtest/logs/app.log
-rw-r----- 1 root root 48 Oct 29 09:39 /home/user/logtest/logs/app.log.1
-rw-r--r-- 1 root root 96 Oct 29 09:39 /home/user/logtest/logs/app.log.2.gzBecause of delaycompress, the app.log.1 left by the first rotation was compressed in this rotation and became app.log.2.gz, while the just-rotated app.log.1 was left uncompressed.
The empty app.log was not rotated because of notifempty, even with -f.
logrotate -f -v -s /home/user/logtest/state /home/user/logtest/test.confreading config file /home/user/logtest/test.conf
Reading state from file: /home/user/logtest/state
Allocating hash table for state file, size 64 entries
Creating new state
Handling 1 logs
rotating pattern: /home/user/logtest/logs/app.log forced from command line (3 rotations)
empty log files are not rotated, old logs are removed
considering log /home/user/logtest/logs/app.log
Now: 2026-10-29 09:39
Last rotated at 2026-10-29 09:39
log does not need rotating (log is empty)After a line was added to app.log before each of two more rotations, the fourth file, app.log.4.gz, was deleted in the last rotation. Only the removing lines were picked out of the -v output to check this.
logrotate -f -v -s /home/user/logtest/state /home/user/logtest/test.conf 2>&1 | grep removingremoving old log /home/user/logtest/logs/app.log.4.gzls -l /home/user/logtest/logs/app.log*-rw-r----- 1 root root 0 Oct 29 09:39 /home/user/logtest/logs/app.log
-rw-r----- 1 root root 48 Oct 29 09:39 /home/user/logtest/logs/app.log.1
-rw-r----- 1 root root 68 Oct 29 09:39 /home/user/logtest/logs/app.log.2.gz
-rw-r----- 1 root root 66 Oct 29 09:39 /home/user/logtest/logs/app.log.3.gzIn line with rotate 3, only three rotated files remained: app.log.1, app.log.2.gz and app.log.3.gz. The contents of compressed files are checked with zcat.
zcat /home/user/logtest/logs/app.log.2.gz2026-10-29 11:00:01 request GET /index.html 200The state file recorded one line per log with the date and time of its last rotation.
cat /home/user/logtest/statelogrotate state -- version 2
"/home/user/logtest/logs/app.log" 2026-10-29-9:39:9The next time logrotate runs, it compares this time with an interval such as daily · weekly to decide whether to rotate.
🔲 logrotate Simulator
In the simulator below, test.conf, size.conf, date.conf and copy.conf in the test folder can be run with -d, -f and -v, and the rotated files can be checked in the logs folder.
6️⃣ logrotate size, dateext, copytruncate
size decides whether to rotate by file size instead of interval. size.conf, with size 1k set for the 1,515-byte test log size.log, was run without -f.
cat /home/user/logtest/size.conf/home/user/logtest/logs/size.log {
size 1k
rotate 2
missingok
}logrotate -v -s /home/user/logtest/state-size /home/user/logtest/size.confreading config file /home/user/logtest/size.conf
Creating stub state file: /home/user/logtest/state-size
Reading state from file: /home/user/logtest/state-size
Allocating hash table for state file, size 64 entries
Handling 1 logs
rotating pattern: /home/user/logtest/logs/size.log 1024 bytes (2 rotations)
empty log files are rotated, old logs are removed
considering log /home/user/logtest/logs/size.log
Creating new state
Now: 2026-10-29 09:28
Last rotated at 2026-10-29 09:00
log needs rotating
rotating log /home/user/logtest/logs/size.log, log->rotateCount is 2
dateext suffix '-20261029'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /home/user/logtest/logs/size.log.2 to /home/user/logtest/logs/size.log.3 (rotatecount 2, logstart 1, i 2),
old log /home/user/logtest/logs/size.log.2 does not exist
renaming /home/user/logtest/logs/size.log.1 to /home/user/logtest/logs/size.log.2 (rotatecount 2, logstart 1, i 1),
old log /home/user/logtest/logs/size.log.1 does not exist
renaming /home/user/logtest/logs/size.log.0 to /home/user/logtest/logs/size.log.1 (rotatecount 2, logstart 1, i 0),
old log /home/user/logtest/logs/size.log.0 does not exist
log /home/user/logtest/logs/size.log.3 doesn't exist -- won't try to dispose of it
renaming /home/user/logtest/logs/size.log to /home/user/logtest/logs/size.log.11024 bytes appeared on the rotating pattern line, and even though this was the first run with a newly created state file, the size exceeded 1KB, so it rotated right away. size.conf has no create, so no new size.log was created.
After size.log was recreated as a one-line file, running the same command did not rotate it.
logrotate -v -s /home/user/logtest/state-size /home/user/logtest/size.confreading config file /home/user/logtest/size.conf
Reading state from file: /home/user/logtest/state-size
Allocating hash table for state file, size 64 entries
Creating new state
Handling 1 logs
rotating pattern: /home/user/logtest/logs/size.log 1024 bytes (2 rotations)
empty log files are rotated, old logs are removed
considering log /home/user/logtest/logs/size.log
Now: 2026-10-29 09:28
Last rotated at 2026-10-29 09:28
log does not need rotating (log size is below the 'size' threshold)With dateext, a date instead of a number is added to the end of rotated file names. date.conf contains weekly, rotate 4, dateext and compress.
logrotate -f -v -s /home/user/logtest/state-date /home/user/logtest/date.confreading config file /home/user/logtest/date.conf
Creating stub state file: /home/user/logtest/state-date
Reading state from file: /home/user/logtest/state-date
Allocating hash table for state file, size 64 entries
Handling 1 logs
rotating pattern: /home/user/logtest/logs/date.log forced from command line (4 rotations)
empty log files are rotated, old logs are removed
considering log /home/user/logtest/logs/date.log
Creating new state
Now: 2026-10-29 09:28
Last rotated at 2026-10-29 09:00
log needs rotating
rotating log /home/user/logtest/logs/date.log, log->rotateCount is 4
dateext suffix '-20261029'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding old rotated logs failed
renaming /home/user/logtest/logs/date.log to /home/user/logtest/logs/date.log-20261029
compressing log with: /bin/gzipls -l /home/user/logtest/logs | grep date-rw-r--r-- 1 root root 56 Oct 29 09:28 date.log-20261029.gzDates are per day, so rotating again on the same day makes the names collide. After a new line was added to date.log and it was run once more with -f, it skipped the rotation and ended with exit code 1.
logrotate -f -v -s /home/user/logtest/state-date /home/user/logtest/date.confreading config file /home/user/logtest/date.conf
Reading state from file: /home/user/logtest/state-date
Allocating hash table for state file, size 64 entries
Creating new state
Handling 1 logs
rotating pattern: /home/user/logtest/logs/date.log forced from command line (4 rotations)
empty log files are rotated, old logs are removed
considering log /home/user/logtest/logs/date.log
Now: 2026-10-29 09:28
Last rotated at 2026-10-29 09:28
log needs rotating
rotating log /home/user/logtest/logs/date.log, log->rotateCount is 4
dateext suffix '-20261029'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
error: destination /home/user/logtest/logs/date.log-20261029.gz already exists, skipping rotationcopytruncate copies the log and keeps it as .1, then empties the contents of the original file. Since the original file stays in place, it is used for programs that cannot reopen their log files.
logrotate -f -v -s /home/user/logtest/state-copy /home/user/logtest/copy.confreading config file /home/user/logtest/copy.conf
Creating stub state file: /home/user/logtest/state-copy
Reading state from file: /home/user/logtest/state-copy
Allocating hash table for state file, size 64 entries
Handling 1 logs
rotating pattern: /home/user/logtest/logs/copy.log forced from command line (2 rotations)
empty log files are rotated, old logs are removed
considering log /home/user/logtest/logs/copy.log
Creating new state
Now: 2026-10-29 09:28
Last rotated at 2026-10-29 09:00
log needs rotating
rotating log /home/user/logtest/logs/copy.log, log->rotateCount is 2
dateext suffix '-20261029'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
renaming /home/user/logtest/logs/copy.log.2 to /home/user/logtest/logs/copy.log.3 (rotatecount 2, logstart 1, i 2),
old log /home/user/logtest/logs/copy.log.2 does not exist
renaming /home/user/logtest/logs/copy.log.1 to /home/user/logtest/logs/copy.log.2 (rotatecount 2, logstart 1, i 1),
old log /home/user/logtest/logs/copy.log.1 does not exist
renaming /home/user/logtest/logs/copy.log.0 to /home/user/logtest/logs/copy.log.1 (rotatecount 2, logstart 1, i 0),
old log /home/user/logtest/logs/copy.log.0 does not exist
log /home/user/logtest/logs/copy.log.3 doesn't exist -- won't try to dispose of it
copying /home/user/logtest/logs/copy.log to /home/user/logtest/logs/copy.log.1
truncating /home/user/logtest/logs/copy.logls -l /home/user/logtest/logs | grep copy-rw-r--r-- 1 root root 0 Oct 29 09:28 copy.log
-rw-r--r-- 1 root root 72 Oct 29 09:28 copy.log.1copy.log became 0 bytes, and its contents moved to copy.log.1. The official manual notes that lines written between the copy and the truncation can be lost, so for services that can be made to reopen their log files, create and postrotate are used.
7️⃣ Checking logrotate Errors
When logrotate does not rotate a log, run the same config with -d and check the last line under considering log. Four messages appeared in the tests.
| Last line under considering log | Meaning |
|---|---|
| log needs rotating | Due for rotation |
| log does not need rotating (log has already been rotated) | Interval not yet passed · log recorded in the state file for the first time |
| log does not need rotating (log is empty) | notifempty and the log is an empty file |
| log does not need rotating (log size is below the 'size' threshold) | Smaller than the size set in size |
When the log file is missing, missingok decides the result. miss.conf, which lists a missing file without missingok, ended with an error.
logrotate -f -s /home/user/logtest/state-miss /home/user/logtest/miss.conferror: stat of /home/user/logtest/logs/none.log failed: No such file or directorymiss2.conf, which adds missingok to the same path, left a does not exist -- skipping line and ended with exit code 0.
A log is also not rotated when the folder holding it has open permissions. A logs-open folder with 777 permissions was created inside the test folder, and rotating the log inside it produced the error below.
logrotate -f -s /home/user/logtest/state-open /home/user/logtest/open.conferror: skipping "/home/user/logtest/logs-open/open.log" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.As the error message suggests, the user and group to rotate as were set in the config with the su directive, and the same log was then rotated.
cat /home/user/logtest/open2.conf/home/user/logtest/logs-open/open.log {
su root root
daily
rotate 2
}logrotate -f -s /home/user/logtest/state-open /home/user/logtest/open2.confThis time it finished without errors, and open.log.1 appeared in the logs-open folder.
ls -l /home/user/logtest/logs-opentotal 4
-rw-r--r-- 1 root root 5 Oct 29 09:28 open.log.1If a directive is mistyped in a config file, logrotate discards only that line and runs with the remaining directives. bad.conf, with rotate mistyped as rotat, was read with -d.
logrotate -d -s /home/user/logtest/state-bad /home/user/logtest/bad.confWARNING: logrotate in debug mode does nothing except printing debug messages! Consider using verbose mode (-v) instead if this is not what you want.
reading config file /home/user/logtest/bad.conf
error: /home/user/logtest/bad.conf:3 unknown option 'rotat' -- ignoring line
Reading state from file: /home/user/logtest/state-bad
error: error opening state file /home/user/logtest/state-bad: No such file or directory
Allocating hash table for state file, size 64 entries
Handling 1 logs
rotating pattern: /home/user/logtest/logs/app.log after 1 days (no old logs will be kept)
empty log files are rotated, old logs are removed
considering log /home/user/logtest/logs/app.log
Creating new state
Now: 2026-10-29 09:33
Last rotated at 2026-10-29 09:00
log does not need rotating (log has already been rotated)The error line showed the file name and line number, and with the rotate line dropped, the config was read as (no old logs will be kept). Rotating as is would keep none of the old logs, so after fixing a config, check with -d starting from the rotating pattern line.
🔢 FAQ & Recommended Content
Log Cleanup
Creating a Shell Script to Delete Log Files (Date 20xx, Size)
An article on building a shell script that deletes old log files by date and size.
Disk Usage
du command: checking file disk usage
An article on how to check the disk space that folders and files take up with du.
Viewing Logs
Linux head command & tail command
An article on printing the beginning and end of a file with head and tail, and following a growing log with tail -f.
ℹ️ Affiliate Disclosure
This site's content contains affiliate links. When a visitor buys a product or service through one of them, the site receives a commission from the seller. The amount the buyer pays(it goes down during event discounts ↓)does not go up. Posted prices, discounts, and stock reflect the time of writing and may differ, so confirm with the seller before buying. Products are chosen and reviewed by our own standards, and commissions do not affect the order or content of reviews.