Monitor System Logs with Logwatch
Updated by Elle Krout Written by Elle Krout
Logwatch is a log parsing program that analyzes and generates daily reports on your system’s log activity. Logwatch does not provide real time alerts, but instead will create a digest organized by systems for ease of review. More advanced users can also pair Logwatch with custom analysis scripts to fine-tune their reports.
By default, Logwatch uses Sendmail to send digests.
NoteThe steps required in this guide require root privileges. Be sure to run the steps below as root. If logged in as a superuser, it is recommended that yousu
into root. For more information on privileges see our Users and Groups guide.
Install Logwatch and Sendmail
Arch Linux
Update your system:
pacman -Syu
Install Logwatch and Postfix, to replace the default Sendmail, which is not in Arch’s repositories.
pacman -S logwatch postfix
Logwatch will prompt you to select which cron provider to use. Select the default, cronie.
Note
Other SMTP clients can also be used for delivering Logwatch messages.Edit the
/etc/postfix/main.cf
file to add your domain information, and allow for send-only mail, replacinghostname.example.com
with your own hostname and domain:- /etc/postfix/main.cf
-
1 2
myhostname = hostname.example.com inet_interfaces = loopback-only
Note
Both A/AAAA, and MX records will need to be set for your domain.Edit
/etc/postfix/aliases
to uncommentroot
and alias it toroot@hostname.example.com
, replacinghostname.example.com
with your own hostname and domain:- /etc/postfix/aliases
-
1
root: root@hostname.example.com
Run
newaliases
after editing the aliases list.Start postfix:
systemctl start postfix
CentOS 7
Update your system:
yum update
Install Logwatch and Sendmail:
yum install logwatch sendmail
Start Sendmail:
systemctl start sendmail
Debian
Update your system:
apt-get update && apt-get upgrade
Install Logwatch and Sendmail:
apt-get install logwatch sendmail-bin sendmail
Fedora
Update your system:
dnf update
Install Logwatch and Sendmail:
dnf install logwatch sendmail
Start Sendmail:
systemctl start sendmail
Ubuntu
Update your system:
apt-get update && apt-get upgrade
Install Logwatch and Sendmail:
apt-get install logwatch sendmail
Configure Logwatch
The default configuration file for Logwatch is located at /usr/share/logwatch/default.conf/logwatch.conf
. This file contains information on which directories for Logwatch to track, how the digest is output, where the digest is sent to, and which services of which to keep track.
The following settings are the most comment configuration changes that will need to be made. Others can be found in the logwatch.conf
file, explained in the comments.
NoteIf Logwatch initially does not appear to run, within thelogwatch.conf
file, change theDetails
setting toMed
.
Log Directories
By default, Logwatch digests will include all logs contained within /var/log
. If any other directories contain logs, such as website directories, they can be added by including additional LogDir
lines. For example:
- /usr/share/logwatch/default.conf/logwatch.conf
-
1 2
LogDir = /var/log LogDir = /var/www/example.com/logs
Print Logwatch Digest to Console
The default Logwatch configuration will output the digest to your Linode’s console. This is defined with the Output
variable, which is set to stdout
by default. This option is feasible if you are only planning on manually running Logwatch, but does not save or send the logs to you for later perusal.
Email Logwatch Digest
The Logwatch digest can be sent to local users or external email addresses, in plain text or HTML formats.
NotePrior to sending mail externally or locally ensure you have Sendmail installed on the Linode. If you choose to use a different MTA client, change the
mailer
line in the Logwatch configuration file to contain the directory of your chosen MTA, or alias/usr/sbin/sendmail
to your MTA.If using Arch, and you followed the above install instructions, Sendmail is already aliased to msmtp.
Change the
Output
value tomail
. If you wish to receive the messages in HTML format change theFormat
value tohtml
.Change the
MailTo
address to a valid email address, or local account user. For example, to send mail to theroot
user change the line to read:- /usr/share/logwatch/default.conf/logwatch.conf
-
1
MailTo = root
Change the
MailFrom
value to a valid email address, or to a local user. This can also be left asLogwatch
.
Save Logwatch Digest to File
Logwatch digests can also be saved to a file on your system.
Change the
Output
value tofile
.Find and uncomment (remove the hashmark [#]) the
Filename
value. Set the path and filename in which you wish to save your Logwatch digests.
Run Logwatch
Run Logwatch Manually
Logwatch can be run manually at any time by inputting the logwatch
command to your console. This command can be appended with a number of options to change the default output to suit your needs:
--detail
: Can be set to low, med, high, or any numerical values between 1 and 10. Defines how detailed the report will be.--logdir
: The directory containing the log files you wish to gain reports on.--service
: The service definition that you wish to report on.--output
: How you want the file to be sent: Standard output (stdout
), mail, or file.--format
: Plain text or HTML.--mailto
: The local user or email address to send the report to.
Run Logwatch through Cron
Logwatch often works best when configured to run daily and send or save a report to view later. This can be achieved by setting Logwatch up to run as a cronjob.
Open the crontab:
crontab -e
Add a line for Logwatch. The following code is configured to run at 00:30 each day:
- /etc/crontab
-
1
30 0 * * * /usr/sbin/logwatch
For more information on adjusting your crontab scheduling, reference our guide on Scheduling Tasks with Cron.
Join our Community
Find answers, ask questions, and help others.
This guide is published under a CC BY-ND 4.0 license.