Alpine 設定日誌至遠端 Log Server
本篇說明如何在 Alpine 系統上配置日誌轉發至遠端 Log Server,範例中使用的 Log Server 為 10.20.2.30。
環境需求
- Alpine 系統(本文以 Alpine 3.21 為例)
- Log Server IP:10.20.2.30(需確保已啟用 Syslog 服務並開放 UDP 514 端口)
設定步驟
1. 安裝 rsyslog 與停用預設 syslogd
apk add rsyslog rc-service syslog stop rc-update del syslog boot
2. 配置 rsyslog
- 為了避免誤操作導致原始配置文件損壞,先備份 `/etc/rsyslog.conf`:
cp /etc/rsyslog.conf /etc/rsyslog.conf.bk
- 編輯主配置文件 `/etc/rsyslog.conf`,確保已載入日誌模組:
vim /etc/rsyslog.conf
- 確認或添加以下模組:
: module(load="imuxsock") # 本地日誌 module(load="imklog" permitnonkernelfacility="on") # 核心日誌 :
3. 配置日誌轉發規則
- 創建一個新的配置文件來定義遠端日誌轉發規則:
mkdir -p /etc/rsyslog.d 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 服務並檢查狀態
- 應用更改並確認服務正常運行:
rc-service rsyslog start && rc-update add rsyslog
5. 驗證日誌轉發
- 在 Alpine 系統上生成測試日誌:
logger "Test message to log server"
- 檢查遠端 Log Server(10.20.2.30)是否接收到日誌訊息。
注意事項
- 確保遠端 Log Server 的防火牆允許 UDP 514 端口的流量。
- 若使用 rsyslog,檢查服務狀態
rc-service rsyslog status