如何在 Ubuntu 20.04 上安裝 Logwatch

日誌觀察 是一個系統日誌分析器,並將該報告發送到電子郵件。 它生成一個總結日誌報告,其中包含 sshd – 身份驗證失敗, sudo – 會話打開、vsftp 失敗、後綴、登錄失敗、磁盤空間等。

Logwatch 還可以在控制台上打印報告。 可以使用 cron 安排此報告在特定日期和時間運行。

在本教程中,我們將學習如何 安裝日誌監視Ubuntu 20.04.

在 Ubuntu 上安裝 Logwatch

Logwatch 可以簡單地從 Ubuntu 存儲庫安裝。 首先更新系統。

$ sudo apt-get update

要安裝 Logwatch,請鍵入:

$ apt-get install logwatch

為 logwatch 工作創建一個臨時目錄:

$ sudo mkdir /var/cache/logwatch

日誌觀察命令

Logwatch 命令在控制台上顯示報告。

$ logwatch

您還可以使用 Logwatch 命令的選項來過濾報告:

$ logwatch --detail high --range yesterday --service vsftpd --service postfix --service zz-disk_space --format text

輸出:

################### Logwatch 7.5.2 (07/22/19) ####################
Processing Initiated: Sat Jul 10 11:13:03 2021
Date Range Processed: today
( 2021-Jul-10 )
Period is day.
Detail Level of Output: 10
Type of Output/Format: stdout / text
Logfiles for Host: li673-49
##################################################################
--------------------- Postfix Begin ------------------------
Summary ***
62.872K Bytes accepted 64,381
62.872K Bytes delivered 64,381
======== ==================================================
1 Accepted 100.00%
-------- --------------------------------------------------
1 Total 100.00%
======== ==================================================
1 Removed from queue 1 1 Delivered 1
Detail (1)
1 Delivered ------------------------------------------------------------------------------- 1 node49.server.com
=== Delivery Delays Percentiles ============================================================
0% 25% 50% 75% 90% 95% 98% 100%

Before qmgr 56.00 56.00 56.00 56.00 56.00 56.00 56.00 56.00
In qmgr 0.01 0.01 0.01 0.01 0.01 0.01 0.01 0.01
Conn setup 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Transmission 0.00 0.00 0.00 0.00 0.00 0.00 0.00 0.00
Total 56.00 56.00 56.00 56.00 56.00 56.00 56.00 56.00
============================================================================================
---------------------- Postfix End -------------------------
--------------------- vsftpd-messages Begin ------------------------
Failed FTP Logins:
(::ffff:66.249.64.177): anonymous - 2 Time(s)
(::ffff:66.249.64.179): anonymous - 1 Time(s)
---------------------- vsftpd-messages End -------------------------
--------------------- Disk Space Begin ------------------------
Filesystem Size Used Avail Use% Mounted on
/dev/sda 157G 16G 134G 11% /
/dev/loop0 56M 56M 0 100% /snap/core18/2066
/dev/loop1 56M 56M 0 100% /snap/core18/2074
/dev/loop2 70M 70M 0 100% /snap/lxd/19188
/dev/loop3 33M 33M 0 100% /snap/snapd/12159
/dev/loop4 68M 68M 0 100% /snap/lxd/20326
/dev/loop5 33M 33M 0 100% /snap/snapd/12398
---------------------- Disk Space End -------------------------
###################### Logwatch End #########################

自定義日誌觀察

Logwatch 配置文件存儲在 /usr/share/logwatch/default.conf/logwatch.conf 中。 為便於編輯,請將 logwatch.conf 複製到 /etc/logwatch/conf/。

$ sudo cp /usr/share/logwatch/default.conf/logwatch.conf /etc/logwatch/conf/

基本的定制是:

輸出 = file|mail|stdout MailTo = [email protected] MailFrom = [email protected] Detail = Low、Med、High 或數字 Service = All Service = service-name Range = Yesterday|Today|All Format = text |html

輸出 – 定義發送日誌觀察報告的位置

郵寄地址 – 指定發送報告的電子郵件地址。 對於多個收件人,請使用以空格分隔的電子郵件地址。

細節 – 這定義了報告的詳細程度。

服務 – 指定一個或多個要由 logwatch 解析的服務。 所有服務都列在 /usr/share/logwatch/scripts/services 下。 要選擇特定服務,請註釋掉 Service = All 並使用 Service = service-name。

範圍 – 定義報告的持續時間。

格式 – 報告可以是文本或 html 格式。

安排日誌觀察

使用 cron,您可以安排 logwatch 在特定時間和日期運行。

打開 crontab 文件:

# crontab -e

添加以下 cron 作業以安排 logwatch 在每天上午 8 點運行:

0 8 * * * /sbin/logwatch

結論

在本教程中,我們學習瞭如何在 Ubuntu 20.04 上安裝 Logwatch。 有關更多信息,請查看 man logwatch。