{"id":7177,"date":"2018-10-29T12:44:00","date_gmt":"2018-10-29T09:44:00","guid":{"rendered":"https:\/\/www.howtoforge.com\/tutorial\/ubuntu-logrotate\/"},"modified":"2018-10-29T12:44:00","modified_gmt":"2018-10-29T09:44:00","slug":"how-to-install-and-use-logrotate-to-manage-log-files-in-ubuntu-18-04-lts","status":"publish","type":"post","link":"https:\/\/afaghhosting.net\/blog\/how-to-install-and-use-logrotate-to-manage-log-files-in-ubuntu-18-04-lts\/","title":{"rendered":"How to Install and Use logrotate to Manage Log Files in Ubuntu 18.04 LTS"},"content":{"rendered":"<p>Log files are most important for Linux system security. the logrotate tool is specially designed to simplify the administration of log files on a Linux system that allows automatic rotation, compression, removal, and mailing of log files. You can easily set logrotate to handle a log file daily, weekly, monthly. The logrotate program used to rotate log files by removing the oldest ones from your system and creating new log files.<\/p>\n<p>In this tutorial, I will explain how to use logrotate to manage logs on Ubuntu 18.04 server.<\/p>\n<h2 id=\"requirements\">Requirements<\/h2>\n<ul>\n<li>A server running Ubuntu 18.04 server.<\/li>\n<li>A non-root user with sudo privileges.<\/li>\n<\/ul>\n<h2 id=\"install-logrotate\">Install Logrotate<\/h2>\n<p>By default, logrotate is available in the Ubuntu 18.04 LTS server. If not then you can install it by running the following command:<\/p>\n<p class=\"command\">sudo apt-get install logrotate -y<\/p>\n<p>Once the logrotate has been installed, you can check the version of logrotate using the following command:<\/p>\n<p class=\"command\">sudo logrotate<\/p>\n<p>Output:<\/p>\n<pre>logrotate 3.11.0 - Copyright (C) 1995-2001 Red Hat, Inc.&#13;\nThis may be freely redistributed under the terms of the GNU Public License&#13;\n&#13;\nUsage: logrotate [-dfv?] [-d|--debug] [-f|--force] [-m|--mail=command]&#13; [-s|--state=statefile] [-v|--verbose] [-l|--log=STRING] [--version]&#13; [-?|--help] [--usage] [OPTION...] <configfile>&#13;\n&#13;\n<\/configfile><\/pre>\n<h2 id=\"configure-logrotate\">Configure Logrotate<\/h2>\n<p>By default, logrotate&#8217;s main configuration file logrotate.conf is located at \/etc\/logrotate.conf. Other configuration files are available at \/etc\/logrotate.d. You can list out them with the following command:<\/p>\n<p class=\"command\">ls \/etc\/logrotate.d<\/p>\n<p>Output:<\/p>\n<pre>alternatives apport dpkg mysql-server rsyslog unattended-upgrades&#13;\napache2 apt lxd openproject ufw&#13;\n<\/pre>\n<p><strong>The main options of logrotate are listed below:<\/strong><\/p>\n<p><strong>missingok<\/strong> : Don&#8217;t raise an error if the log is missing<\/p>\n<p><strong>weekly<\/strong>: rotates the log files once a week<\/p>\n<p><strong>create<\/strong>: The old file is saved under a new name and a new file is created<\/p>\n<p><strong>compress<\/strong>: logrotate compress log files using gzip to save space<\/p>\n<p><strong>rotate<\/strong> <strong>4<\/strong>: logrotate rotates a given log four times before deleting it, so this keeps four weeks of logs online<\/p>\n<p><strong>notifyempty<\/strong>: Don&#8217;t rotate the log file when it is empty<\/p>\n<p>By default, logrotate runs on a daily basis by executing the shell script \/etc\/cron.daily\/logrotate. Let&#8217;s test the apache logrotation by running the following command:<\/p>\n<p class=\"command\">sudo logrotate -d \/etc\/logrotate.d\/apache2<\/p>\n<p>Output:<\/p>\n<pre>reading config file \/etc\/logrotate.d\/apache2&#13;\nReading state from file: \/var\/lib\/logrotate\/status&#13;\nAllocating hash table for state file, size 64 entries&#13;\n&#13;\nHandling 1 logs&#13;\n&#13;\nrotating pattern: \/var\/log\/apache2\/*.log after 1 days (14 rotations)&#13;\nempty log files are not rotated, old logs are removed&#13;\nconsidering log \/var\/log\/apache2\/access.log&#13;\nCreating new state&#13; Now: 2018-10-11 16:04&#13; Last rotated at 2018-10-11 16:00&#13; log does not need rotating (log has been already rotated)&#13;\nconsidering log \/var\/log\/apache2\/error.log&#13;\nCreating new state&#13; Now: 2018-10-11 16:04&#13; Last rotated at 2018-10-11 16:00&#13; log does not need rotating (log has been already rotated)&#13;\nconsidering log \/var\/log\/apache2\/other_vhosts_access.log&#13;\nCreating new state&#13; Now: 2018-10-11 16:04&#13; Last rotated at 2018-10-11 16:00&#13; log does not need rotating (log has been already rotated)&#13;\nnot running prerotate script, since no logs will be rotated&#13;\nnot running postrotate script, since no logs were rotated&#13;\n<\/pre>\n<p>By default, logrotate is run as a daily cron job. You can check \/etc\/cron.daily\/logrotate for more information about cron job. You can also configure the cron job for logrotate to run hourly, weekly and yearly.<\/p>\n<div class=\"authorbox\" readability=\"30\">\n<img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/how-to-install-and-use-logrotate-to-manage-log-files-in-ubuntu-18-04-lts.jpg\" alt=\"Hitesh Jethva\" title=\"\"> <\/p>\n<p><strong>About Hitesh Jethva<\/strong><\/p>\n<p>Over 8 years of experience as a Linux system administrator. My skills include a depth knowledge of Redhat\/Centos, Ubuntu Nginx and Apache, Mysql, Subversion, Linux, Ubuntu, web hosting, web server, Squid proxy, NFS, FTP, DNS, Samba, LDAP, OpenVPN, Haproxy, Amazon web services, WHMCS, OpenStack Cloud, Postfix Mail Server, Security etc.<\/p>\n<\/div>\n<div>\n<p><b>Share this page:<\/b><\/p>\n<p>\n<a href=\"https:\/\/www.facebook.com\/sharer.php?u=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fubuntu-logrotate%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/how-to-install-and-use-logrotate-to-manage-log-files-in-ubuntu-18-04-lts.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/intent\/tweet?url=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fubuntu-logrotate%2F&amp;text=How+to+Install+and+Use+logrotate+to+Manage+Log+Files+in+Ubuntu+18.04+LTS&amp;via=howtoforgecom&amp;related=howtoforgecom\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/how-to-install-and-use-logrotate-to-manage-log-files-in-ubuntu-18-04-lts-1.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/twitter.com\/howtoforgecom\/\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/how-to-install-and-use-logrotate-to-manage-log-files-in-ubuntu-18-04-lts-2.png\" height=\"20\" alt=\"\" title=\"\"><\/a><br \/>\n<a href=\"https:\/\/plus.google.com\/share?url=https%3A%2F%2Fwww.howtoforge.com%2Ftutorial%2Fubuntu-logrotate%2F\" target=\"_blank\" rel=\"nofollow noopener noreferrer\"><img decoding=\"async\" src=\"https:\/\/afaghhosting.net\/blog\/wp-content\/uploads\/2018\/10\/how-to-install-and-use-logrotate-to-manage-log-files-in-ubuntu-18-04-lts-3.png\" height=\"20\" alt=\"\" title=\"\"><\/a>\n<\/p>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Log files are most important for Linux system security. the logrotate tool is specially designed to simplify the administration of log files on a Linux system that allows automatic rotation, compression, removal, and mailing of log files. You can easily set logrotate to handle a log file daily, weekly, monthly. The logrotate program used to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[36],"tags":[],"class_list":["post-7177","post","type-post","status-publish","format-standard","hentry","category-36"],"_links":{"self":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7177","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/comments?post=7177"}],"version-history":[{"count":0,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/posts\/7177\/revisions"}],"wp:attachment":[{"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/media?parent=7177"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/categories?post=7177"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/afaghhosting.net\/blog\/wp-json\/wp\/v2\/tags?post=7177"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}