本篇說明如何在 Ubuntu 系統上配置日誌轉發至遠端 Log Server,範例中使用的 Log Server 為 10.20.2.30。
sudo cp /etc/rsyslog.conf /etc/rsyslog.conf.bk
sudo vim /etc/rsyslog.conf
# 提供核心日誌支援並允許非核心 klog 訊息 module(load="imklog" permitnonkernelfacility="on") # 提供檔案日誌支援 module(load="imfile")
sudo vim /etc/rsyslog.d/99-logserver.conf
# 轉發訊息至 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") }
sudo systemctl restart rsyslog && systemctl status rsyslog
logger "Test message to log server"
journalctl -u rsyslog