The performance of a computer or server environment very much depends upon the system memory and disk usage. If something is consuming more disk space then it will lead to a system error. Likewise, increasing log file size must be controlled to reduce such risk.
Logrotate was introduced as a system utility that rotates, compresses the log files, and mails system logs. Such management of log files reduces disk space usage and prevents system errors.
In this article, we are going to discuss the installation process and the configuration of logrotate on Ubuntu 20.04 LTS server.
On Ubuntu, logrotate is installed by default but in case it is not installed, you can install it with the command as shown below.
$ sudo apt update $ sudo apt install logrotate
You can confirm the installation with the command as shown below.
$ logrotate --version
Configuration file for logrotate is created by logrotate daemon. There are two paths for such configuration as shown below.
/etc/logrotate.conf
It is the the configuration file generally created for the logrotate utility
/etc/logrotate.d/
It is the directory consisting of the specific rotation of the applications. By default, /etc/logrotate.conf is used but for each application to have different configuration, it can be set on /etc/logrotate.d/.
As you are aware that /etc/logrotate.conf is the default configuration file. Let's check the configuration file as shown on the screenshot below.
Check the config file with the command as shown below.
$ cat /etc/logrotate.conf
Output:
On the above screenshot, there is a configuration as include /etc/logrotate.d which means the configuration for specific applications can be set on this directory. Here, we are going to show the configuration for dpkg as shown below.
$ cd /etc/logrotate.d/
$ cat dpkg
To get details of each line of configuration, check the points discussed below. These configurations will replace the default configuration of /etc/logrotate.conf for specific applications like dpkg.
Let's say you have installed an application like nginx and its log file is created on /var/log/nginx/ then you can set up a logrotate configuration file for this specific app with the command as shown below.
Navigate to the logrotate directory
$ cd /etc/logorate.d/
Create a logrotate file with editor
$ vim nginx
/var/log/nginx/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
prerotate
if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
run-parts /etc/logrotate.d/httpd-prerotate; \
fi \
endscript
postrotate
invoke-rc.d nginx rotate >/dev/null 2>&1
endscript
} In the above configuration, we have set the rotation for 14 so 14 old log files will be kept, and the log file is compressed with the use of gzip. Another configuration used in the above file is almost explained in the logrotate configuration file section. You can run the newly created configuration with the sudo privilege user as shown below.
$ sudo logrotate -d /etc/logrotate.d/nginx
Here, log files are executed as shown in the screenshot below.
While installing the logrotate package, a crontab file is also created on the process inside /etc/cron.daily with the name logrotate. Check the screenshot as shown below for further details.
$ cat /etc/cron.daily/logrotate
In this article, you have learned how to install the logrotate package and check the default and specific application configuration file for logrotate with the implementation of crontab. Thank you!
Magento is a free and open-source e-commerce platform written in PHP. It is simple, easy…
ISPConfig is an open-source control panel that allows users to manage multiple servers from a…
As a Linux administrator, you may find it necessary to troubleshoot or test your Simple…
Ubuntu 24.04, like many modern Linux distributions, relies on the NetworkManager for managing network connections.…
Restic is a modern, open-source backup program designed for efficiency, security, and simplicity. It enables…
phpMyAdmin is a popular free tool written in PHP intended to administer MySQL and MariaDB…