Setting up logrotate

Written by Robert -

Assuming you have logrotate installed, it's very easy to set it up. Setting up logrotate has to be done for each file.

In this example, I set it up for files called dnsmasq.log, which, in my case, handles DHCP. This creates rather large log files. Since text files can easily be compressed, I advice to do so since it saves a lot of harddisk space. The config file should be placed under /etc/logrotate.d

The system I'm configuring this on is running sysvinit but it should work similar in systemd.

# let's start with the name of the log that needs to be rotated
/var/log/dnsmasq.log {
# If there isn't a file with that name, continue to the next file with missingok
# Don't rotate if the file is empty
# Compress the file using gzip
# Delaying is usefull if the file is in use while trying to rotate it. It will rotate it the next time it's needed
# What is the minimum filesize needed before a logrotate takes place?
size 1M
# create a new file. Noticed I only gave root rights to this file. No need to execute so 600 should be enough.
create 0600 root root

For this to work, you need to have a log file there that's bigger than 1MB in size. When that's the case, you can restart the logrotate service by typing the following commands:

Sysvinit and OpenRC:
service logrotate restart

systemctl restart logrotate