Knowledge
Bài tập: syslog tập trung với rsyslog
Thời lượng ước tính: 45 phút.
Triển khai hệ thống log tập trung với rsyslog trên 2 node Linux: một node làm syslog server nhận log qua cổng 514, một node làm client forward toàn bộ log về server và lưu tách theo host/program (/var/log/remote/<host>/<program>.log). Verify log client xuất hiện trên server; xử lý lỗi quyền ghi của rsyslog. Địa chỉ IP trong bài là địa chỉ minh hoạ thuộc dải dành cho tài liệu (RFC 5737): dattqh-nat = 192.0.2.21, dattqh-client = 192.0.2.22; khi làm, thay bằng IP thật của máy bạn.
Bước 1 — Phase 1 — Cấu hình rsyslog server nhận log (171)
Server cần load input module và mở cổng 514 trước (imtcp/imudp).
# Trên dattqh-nat (192.0.2.21)
sudo tee /etc/rsyslog.d/10-remote-server.conf >/dev/null <<'EOF'
module(load="imudp")
input(type="imudp" port="514")
module(load="imtcp")
input(type="imtcp" port="514")
template(name="RemoteHostFile" type="string"
string="/var/log/remote/%HOSTNAME%/%PROGRAMNAME%.log")
if ($fromhost-ip != "127.0.0.1") then {
action(type="omfile" dynaFile="RemoteHostFile"
fileCreateMode="0640" dirCreateMode="0755")
stop
}
EOF
sudo mkdir -p /var/log/remote
sudo rsyslogd -N1
sudo systemctl restart rsyslog
sudo ss -lntup | grep ':514'
Ghi chú: Quan sát: rsyslogd -N1 in "version 8.2312.0 … End of config validation run. Bye." không error. ss -lntup | grep :514 thấy rsyslogd LISTEN 0.0.0.0:514 + [::]:514 cho cả UDP và TCP.
Bước 2 — Phase 2 — Cấu hình rsyslog client forward (172)
Client cần một action omfwd trỏ server:514.
# Trên dattqh-client (192.0.2.22)
sudo tee /etc/rsyslog.d/90-forward.conf >/dev/null <<'EOF'
*.* action(type="omfwd"
target="192.0.2.21"
port="514"
protocol="tcp"
action.resumeRetryCount="100"
queue.type="linkedList"
queue.size="10000")
EOF
sudo rsyslogd -N1
sudo systemctl restart rsyslog
ss -tnp | grep ':514'
logger -p user.notice "LAB-SYSLOG-OK $(hostname) $(date +%H:%M:%S)"
Ghi chú: Quan sát: ss -tnp | grep :514 thấy ESTAB 192.0.2.22:<port> 192.0.2.21:514 users:(("rsyslogd",...)). Sau logger, message đi qua kết nối này tới server.
Bước 3 — Phase 3 — FAIL/FIX: bắt lỗi quyền + xác minh nhận log
Lưu ý rsyslog drop privilege xuống user syslog ($PrivDropToUser syslog trong /etc/rsyslog.conf).
# Trên server 171 — chẩn đoán khi /var/log/remote rỗng
sudo journalctl -u rsyslog -n 20 --no-pager | grep -i "could not open"
sudo ls -ld /var/log/remote
# FIX: cấp quyền cho user rsyslog drop xuống
sudo chown syslog:syslog /var/log/remote
sudo systemctl restart rsyslog
# Verify: sau khi client gửi lại, xem cây file
sudo find /var/log/remote -type f -ls
sudo grep -rh "LAB-SYSLOG-OK" /var/log/remote
Ghi chú: Quan sát: journalctl -u rsyslog trước fix có dòng omfile: creating parent directories … Permission denied [try https://www.rsyslog.com/e/2207]. Sau chown + client gửi lại: find /var/log/remote -type f liệt kê dattqh-client/sshd.log, systemd.log, … owner syslog:syslog mode 0640.
Bước 4 — Phase 4 (tuỳ chọn) — Kiểm chứng queue chống mất log khi server down
Queue cùng resumeRetryCount giữ message khi action forward fail.
# 1) Trên SERVER: dừng nhận
sudo systemctl stop rsyslog
# 2) Trên CLIENT: gửi mốc trong lúc server down
for i in 1 2 3; do logger -p user.notice "QUEUE-TEST $i $(date +%s)"; done
ss -tnp | grep ':514' # không còn ESTAB (server down)
# 3) Trên SERVER: bật lại
sudo systemctl start rsyslog
sleep 3
# 4) Trên SERVER: kiểm message đã resend
sudo grep -rh "QUEUE-TEST" /var/log/remote
Ghi chú: Quan sát: Lúc server down, client ss -tnp | grep :514 không còn ESTAB (hoặc SYN-SENT). Sau khi server lên lại ~vài giây, grep QUEUE-TEST trên server trả đủ cả 3 message — không mất, dù gửi lúc server offline.
Minh hoạ 1 — Server lắng nghe cổng 514
Validate config + ss -lntup: rsyslog server nghe cả UDP lẫn TCP cổng 514.
Minh hoạ 2 — Client forward log qua TCP
omfwd TCP tới 192.0.2.21:514 (ESTAB) + logger gửi message mốc.
Minh hoạ 3 — FAIL — permission denied (lỗi 2207)
Thư mục /var/log/remote thuộc root → user syslog không tạo subdir → message bị discard.
Minh hoạ 4 — FIX — chown syslog → log nhận được
Sau chown syslog:syslog: log client lưu theo cây /var/log/remote/<host>/<prog>.log.
Minh hoạ 5 — Demo thực tế — SSH vào client 172, server 171 nhận log sshd
SSH vào 192.0.2.22 (1 đăng nhập đúng + 1 sai mật khẩu): sự kiện sshd trên client forward TCP 514 về server, lưu tại /var/log/remote/dattqh-client/sshd.log — log tập trung bắt được cả Accepted hợp lệ lẫn Failed/Invalid user (dò mật khẩu), không cần đăng nhập vào từng máy.