Managing Logfiles with Logrotate on Ubuntu 16.04

Any information system saves operations and actions in special text – logs. In order for the IS to save logs properly, and administrators to be able to them, they use specialized . In the article we will talk about one utility – Logrotate, which is installed on the 16.04 server OS.

Preliminary preparation

Logrotate archives log files automatically according to the specified parameters. If you do not compress the logs and do not delete old versions, then over time you will run out of space on your hard drive. To avoid this situation, system administrators use programs.

Before setting up logging, we connect to the server platform via and perform the following actions on behalf of the superuser.


Having connected to Ubuntu Server, we check the availability of the program with the command:

logrotate -- version

Important! As of mid-August 2019, release 3.15.0.

If instead of a response in the form of numbers, the user receives a message that the utility is not installed, then we will install Ubuntu using standard means.


The standard configuration of Logrotate is stored in two ways:

1. The /etc/logrotate.conf file stores some of the default configurations.  also contains archiving templates for non-system files.

2. The second path – /etc/logrotate.d/ is designed to save third-party parameters that the administrator sets on his own. Rotation templates for system utilities (apt, rsyslog, dpkg, etc.) are stored here.

After installation, the utility uses a standard pattern of weekly data rotation. According to the parameters, logs are archived that belong to the root user and the syslog system group.

Let’s go to the second file, which is responsible for the apt system command, in order to study the standard options.

cat /etc/logrotate.d/apt

There are two sections inside:

Screenshot #1. File contents.

As you can see, the data is stored in the form of identical blocks of conditions:

  • rotate 12 tells the utility to keep the last twelve logs;
  • the second line means the content is updated once a month;
  • compress is an archiving command, by default the standard utility of Linux systems is used – gzip, if you want to change it to another one, then specify the keys after the command;
  • the fourth parameter tells logrotate not to save an error message if there is no log file;
  • the last parameter cancels archiving if the log is empty.

If you need your own for rotation, then create it in the /etc/logrotate.d/ directory. Below is a test version of creating a rotation template.

Example #1

Initial: A virtual server exists. It is necessary to save logs in access.log and error.log. They are located in the /var/log/example-app/ directory. The server has a www-data user and a group with the same name.

Create a file with a text editor.

sudo vi /etc/logrotate.d/example-app

Paste the following commands into it:

Screenshot #2. File example.

Important! If you need detailed information about the command, then use the man utility. ,

man logrotate

After adding the information, save the changes. Testing the result:

sudo logrotate /etc/logrotate.conf –debug

Important! The working window of the terminal displays information about the operations currently performed by logrotate.

Example #2

Background: Logrotate is running under the account name Sammy. Log files are saved to the /home/sammy/logs/ directory. You need to set up archiving at an hourly interval.

Important! The settings will be done in a different directory than /etc/logrotate.d.

Create a new configuration using the vi editor.

vi /home/sammy/logrotate.conf

Let’s add the following information to it:

/home/sammy/logs/*.log {
rotate 24

The hourly and rotate 24 commands indicate that archiving occurs hourly. Close the file with saving.

For verification, we will create a test file in which the logs will be stored.

cd ~
mkdir logs
touch logs/access.log

Now let’s tell logrotate a new path to save the information.

logrotate /home/sammy/logrotate.conf --state /home/sammy/logrotate-state –verbose

After launching the utility, the following will appear on the monitor:

Screenshot #3. Test result.

For the logging program, this is a new file.

To run the utility hourly, let’s configure the cron job scheduler. Let’s open it:

crontab –e

Let’s put the last line in the form:

15 * * * * /usr/sbin/logrotate /home/sammy/logrotate.conf --state /home/sammy/logrotate-state

Save changes, close the file.

HostRooster® is a cloud platform, offering services from data centers globally. When can use the straightforward, economical cloud that they love, businesses expand more quickly. With predictable pricing, thorough documentation, and scalability to support business growth at any stage, HostRooster® has the cloud computing services you need. Startups, enterprises, and government agencies can use HostRooster® to lower costs, become more agile, and innovate faster.


Welcome to the world of DomainRooster, where roosters (and hens) rule the roost! We're a one-stop shop for all your entrepreneurial needs, bringing together domain names and website hosting, and all the tools you need to bring your ideas to life. With our help, you'll soar to new heights and hatch great success. Think of us as your trusty sidekick, always there to lend a wing and help you navigate the sometimes-complex world of domain names and web hosting. Our team of roosters are experts in their fields and are always on hand to answer any questions and provide guidance. So why wait? Sign up today and join the ranks of the world's greatest entrepreneurs. With DomainRooster, the sky's the limit! And remember, as the saying goes, "Successful people do what unsuccessful people are not willing to do." So don't be afraid to take that leap of faith - DomainRooster is here to help you reach for the stars. Caw on!