Install Logrotate in Linux (CentOS, RHEL, Fedora, Ubuntu, Debian)
Logs are very useful, especially when you are troubleshooting an issue. Logs have information about all the events for a service or for whole server. We have all kinds of logs for the servers & separate logs for the services also, like web server or MySQL etc.
These logs ought be managed as they keep on growing in size with time, which not only makes it difficult to analyze them but they also occupy disk space on the system. If left unchecked they can grow to GB is size (it’s true, not lying). So to manage the logs & keep them under check, we use logrotate utility on Linux machines.
As the name suggests, logrotate is used to manage & rotate the logs based on their size, date etc. With the logrotate utility, we can archive old logs, delete them after a certain number of logs have been reached & can start new log files afresh.The system usually runs logrotate once a day, and checks configurations that can be customized on a per-directory or per-log basis.
In this tutorial, we will learn to install logrotate on various Linux distributions like RHEL/CentOS, Fedora, Ubuntu/Debian & will than also discuss some configuration that can be used with logrotate.”
(Recommended Read: Complete guide to install Zabbix on CentOS7 )
On most Linux distributions, logrotate is installed by default but if that’s not the case than it can be easily installed using the default package manager for the distribution as it’s available on the default repositories. Install it on various Linux distributions using the following commands,
# yum install logrotate
# apt-get install logrotate
# dnf install logrotate
The main configuration file for logrotate is located at ‘/etc/logrotate.conf’ but we can also create configuration for each service separately in the folder ‘/etc/logrotate.d/’. We will not be using the main configuration file but you can use it for reference, the only thing we need to make sure it that the config file must contain the following parameter,
# sudo vim /etc/logrotate.conf
If its not present than make an entry & save the file. We will now take an example & will rotate the logs for web server installed at ‘/data/apache2/’ location.
Firstly create a new file for logrotation in the folder ‘/etc/logrotate.d’ with any name, for example ‘apache’,
# cd /etc/logrotate.d
# vim apache
& make the following entries to the file,
sudo /data/apache2/bin/apachectl -k graceful >> /dev/null
Here, we are rotating all logs fr
om ‘/data/apache/logs‘ directory,
they will be rotated daily, other options that can be used here are weekly, monthly or yearly,
‘missingok’ means that errors are not to be written logs,
than we will keep last 50 logs with ‘rotate’,
‘notifempty’ means that logs are not to be rotated if empty,
delaycompress is active, an archived log is compressed the next time that the log is rotated. We can also use ‘nocompress’ & ‘compress’,
The ‘postrotate’ command tells logrotate that the script to run, starts on the next line, and the ‘endscript’ command says that the script is done,
‘sharedscript’ tells logrotate to check all the logs for that configuration block before running the ‘postrotate’ script
Logs can also be rotated based on size, to use it mention following in the logrotate config file,
Once the file has been created, save it. By default, logrotate automatically configures a cron job scheduled to be run daily. You can check the same at ‘/etc/cron.daily/logrotate’.
To make sure that logrotate is working fine without any issues or debug the logrotate configuration, run the following command from terminal,
# /usr/sbin/logrotate -d /usr/local/etc/logrotate.d/apache
Once the configuration has been checked, we can also force run the script with the following command,
# /usr/sbin/logrotate -f /usr/local/etc/logrotate.d/apache
With this we end out tutorial on how to install logrotate & use it. Please feel free to send in your suggestions & questions to comment box below.