Logrotate Ubuntu Tutorial

28/12/2020

How to use Logrotate on Ubuntu

Logrotate is a a system utility tool that is used to manage log files on Ubuntu. When a large number of servers are handled by any system, then a large volume of log files are generated which consumes huge disk space. Logrotate is used to rotate, compress or remove log files automatically to save the disk space. Some functions of Logrotate are mentioned here:

  • It activates when log file size becomes large and reaches to a particular limit.
  • It creates new log file after rotating old files.
  • It compresses log files.
  • It executes shell script after log rotation.
  • It removes older rotated log files to save disk space.

In this tutorial, you will learn how to use this tool on Ubuntu 17.10.

Checking Logrotate Version

Logrotate is installed on Ubuntu by default. Run the following command to check it is installed or not. Here, the version of Logrotate is 3.11.0.

$ logrotate

Logrotate Configuration Files

The configuration information of Logrotate is stored in two places. One of them is the main configuration file which is located in /etc/logrotate.conf. It contains default settings and uses include statement to retrieve configuration information from another location.  Another place is a directory which contains all service and application configuration files and located in /etc/logrotate.d. When any new package installs in the system then the log rotation information for that package is stored in this location.

Open logrotate.conf from the terminal.

$ nano /etc/logrotate.conf

The following content will be shown. You can rotate log files hourly, daily, weekly, monthly and yearly. Here, weekly means that log files will be configured weekly. The next line indicates that log files are owned by root and syslog group users. Rotate 4 indicates that Logrotate will keep 4 weeks of log files backup and empty log files will be created after rotating old log files. If you set rotate 0 then all old log files will be removed.  If you want to compress log files then just uncomment the line of compress by removing hash symbol.

If you open the directory of /etc/logrotate.d then the following list will appear which contains others logrotate configuration files.

$ ls /etc/logrotate.d

Run the command to open apache2 configuration file. There are many types of rules which are used for configuration settings. Some of them are explained here. missingok indicates that no error message will be written if log file is missing and notifyempty indicates that if log file is empty then it will not rotate. Create 644 root root is used create log files immediately after the rotation, as root user and user group root with specific permission mode.

$ nano /etc/logrotate.d/apache2

Creating a sample configuration file

Suppose you want to create a configuration file in the location  /home/ubuntu/logrotate.conf where the settings for logrotate will be set as: log files will rotate monthly, rotate for 10 times, compress, omit error message if any log file is missing and create log file after removing old ones. Open any editor, add the following settings options and save the file. Here, ubuntu is logged in user’s name.

/home/Ubuntu/logs/*.log {
    monthly
    missingok
    rotate 10
    compress
    create
}

Run the following command to create a log file named mylog.log and initially log file is empty.

$ nano mylog.log

Run logrotate command to check the log entries are created or not.

$ logrotate /home/ubuntu/logrotate.conf –state /home/ubuntu/logrotate-state –verbose

The following output will appear after executing the above command for the first time.

Now, open logrotate-state file to examine which is added in the file. Since no log files are generated yet, so the following output will appear.

$ nano home/ubuntu/logrotate-state

Run the following command to rotate log file forcefully. But no change will appear here according to the previous output because rotation interval is set as monthly and the time period is not exceeded here.

$ logrotate /home/ubuntu/logrotate.conf –state /home/ubuntu/logrotate-state
 –verbose –force

Many other logrotate options are available to configure log files. You will get the list of logrotate options after executing the following command:

$ man logrotate

Some others mostly used logrotate options are, size, copytruncate, postrotate, maxage and compresscmd. These options are shortly explained here.

size:

This option is used to set the limit of log file size and logrotate will rotate log files when the file size reaches or exceed the limit.

copytruncate:

This option is used to create a copy of the original log files and truncate the original log file size to 0. So that the service related to that particular log file can write log properly in the original file.

postrotate:

This option is used to run custom shell script after completing log file rotation.

maxage:

This option is used to set time duration for storing old log file information. It sets the maximum values in days to keep log files and all log files will be removed when the maxage values exceeds.

compresscmd:

You can compress log files by using different commands. This option is used to specify the type of compression command.

After reading this tutorial, you will be able to use Logrotate with different options for managing your log files properly.

More Information

Logrotate Man Page

ONET IDC thành lập vào năm 2012, là công ty chuyên nghiệp tại Việt Nam trong lĩnh vực cung cấp dịch vụ Hosting, VPS, máy chủ vật lý, dịch vụ Firewall Anti DDoS, SSL… Với 10 năm xây dựng và phát triển, ứng dụng nhiều công nghệ hiện đại, ONET IDC đã giúp hàng ngàn khách hàng tin tưởng lựa chọn, mang lại sự ổn định tuyệt đối cho website của khách hàng để thúc đẩy việc kinh doanh đạt được hiệu quả và thành công.
Bài viết liên quan

Ubuntu Print Screen

Windows is perhaps the easiest and the most user-friendly interfaces I have ever come across. Its usage reminds us of how...
29/12/2020

How to install Grub Customizer on Ubuntu 17.04 Zesty

Grub Customizer is a graphical user interface that allows you to name and configure the GRUB2/BURG boot entry settings as...
12/02/2020

How to Lock Screen on Ubuntu

The lock screen is an essential security feature of any operating system. Ubuntu is a Linux distro and that also puts a...
29/12/2020
Bài Viết

Bài Viết Mới Cập Nhật

Mua proxy v4 chạy socks5 để chơi game an toàn, tốc độ cao ở đâu?
18/05/2024

Thuê mua proxy Telegram trọn gói, tốc độ cao, giá siêu hời
18/05/2024

Thuê mua proxy Viettel ở đâu uy tín, chất lượng và giá tốt? 
14/05/2024

Dịch vụ thuê mua proxy US UK uy tín, chất lượng số #1
13/05/2024

Thuê mua proxy Việt Nam: Báo giá & các thông tin MỚI NHẤT
13/05/2024