====== Ubuntu 設定日誌至遠端 Log Server ====== 本篇說明如何在 Ubuntu 系統上配置日誌轉發至遠端 Log Server,範例中使用的 Log Server 為 **10.20.2.30**。 ===== 環境需求 ===== - Ubuntu 系統(本文以 Ubuntu 20.04 為例) - Log Server IP:10.20.2.30(需確保已啟用 Syslog 服務並開放 UDP 514 端口) - 安裝並啟用 `rsyslog` 服務 ===== 設定步驟 ===== ==== 1. 備份 rsyslog 配置文件 ==== * 為了避免誤操作導致原始配置文件損壞,先備份 `/etc/rsyslog.conf`: sudo cp /etc/rsyslog.conf /etc/rsyslog.conf.bk ==== 2. 檢查並啟用必要模組 ==== - 編輯主配置文件 `/etc/rsyslog.conf`,確保已載入日誌模組: sudo vim /etc/rsyslog.conf - 確認或添加以下模組: # 提供核心日誌支援並允許非核心 klog 訊息 module(load="imklog" permitnonkernelfacility="on") # 提供檔案日誌支援 module(load="imfile") ==== 3. 配置日誌轉發規則 ==== - 創建一個新的配置文件來定義遠端日誌轉發規則: sudo vim /etc/rsyslog.d/99-logserver.conf - 添加以下內容,將日誌轉發至遠端 Log Server: # 轉發訊息至 Log Server (10.20.2.30:514) # 規則 1:轉發所有 info 級別以上的日誌(排除 mail、authpriv 和 cron) if ($syslogseverity <= 6 and $syslogfacility-text != 'mail' and $syslogfacility-text != 'authpriv' and $syslogfacility-text != 'cron') then { action(type="omfile" File="/var/log/syslog") action(type="omfwd" Target="10.20.2.30" Port="514" Protocol="udp") } # 規則 2:轉發 SSH 相關日誌(auth 和 authpriv) if ($syslogfacility-text == "auth" or $syslogfacility-text == "authpriv") then { action(type="omfile" File="/var/log/auth.log") action(type="omfwd" Target="10.20.2.30" Port="514" Protocol="udp") } ==== 4. 重啟 rsyslog 服務並檢查狀態 ==== * 應用更改並確認服務正常運行:sudo systemctl restart rsyslog && systemctl status rsyslog ==== 5. 驗證日誌轉發 ==== - 在 Ubuntu 系統上生成測試日誌:logger "Test message to log server" - 檢查遠端 Log Server(10.20.2.30)是否接收到日誌訊息。 ===== 注意事項 ===== - 確保遠端 Log Server 的防火牆允許 UDP 514 端口的流量。 - 若 `rsyslog` 服務無法啟動,可檢查日誌:journalctl -u rsyslog - 若使用 TCP 協議,需將 `Protocol="udp"` 改為 `Protocol="tcp"`,並確保 Log Server 支援 TCP 連線。 {{tag>Ubuntu logserver rsyslog}}