背景
- 是什(shen)么?
- Linux journal是systemd的一個組(zu)件,用于捕獲(huo)和存儲(chu)系統日志信息。
- Linux journal是(shi)systemd體系中(zhong)負責日(ri)志管理(li)的部分,由journald服務處理(li)。
- 解決什(shen)么(me)問題?
- journal旨(zhi)在(zai)解決傳統日志系統中存在(zai)的一些(xie)限(xian)制和(he)問題。
- 由于系統日(ri)志以二進制形式存(cun)儲,這(zhe)讓日(ri)志的(de)解析和(he)使用變得更加靈活,比如可(ke)以以文本、JSON等多種格式進行轉譯以滿足(zu)不同(tong)的(de)需求。
- journal按時間順序存儲事(shi)件,使得(de)追蹤單個事(shi)件變得(de)容(rong)易。
- journald還(huan)提供豐富的過濾和搜索選項,幫助系統管理員快速定位和解(jie)決問題。
- 如何使用?
- journalctl命令是(shi)與journal交互的主要工具,允許管理(li)員查(cha)看(kan)和操作這些日(ri)志(zhi)信息。
和rsyslog區別
journalctl 和 syslog 都是 Linux 系統中用于處(chu)理和查看系統日志的工(gong)具,我(wo)們從以下幾個方面來(lai)對比下兩者(zhe)的區別:
- 存儲方式:
- syslog 使用文(wen)本文(wen)件來存儲(chu)(chu)日志信息,可以通過文(wen)本編輯器(qi)查(cha)看,通常存儲(chu)(chu)在 /var/log/syslog 或 /var/log/message
- journalctl 使(shi)用 systemd-journald 服務來存儲二進制格式的日志文(wen)件。通常存儲在 /run/log/journal/ 或(huo) /var/log/journal/
- 日志內容:
- syslog 使(shi)用(yong)文本(ben)格式,記錄在(zai)文本(ben)文件中(zhong)。日志信息包含時間戳、主(zhu)機名、應用(yong)程序名等
- journalctl 使用二進制格式,可以存(cun)儲更多的(de)元數據,例如進程 ID、用戶 ID、SELinux 上下文等。這使得日志更加結構(gou)化,支持更高級(ji)的查(cha)詢和(he)過濾(lv)
- 查詢和過濾:
- syslog 的(de)查(cha)詢和過濾(lv)通常使(shi)用命令行工具(例如(ru) grep)或(huo)者(zhe)專用的(de)工具(例如(ru) logrotate)
- journalctl 提供了(le)更豐富和強(qiang)大的查(cha)詢(xun)和過濾功(gong)能(neng),可以按(an)時間(jian)、服務單(dan)元、日志(zhi)級別等多(duo)個條件進(jin)行過濾。這(zhe)使得查找和分析特定事件更加方便
- 實時查看:
- syslog 通(tong)常使(shi)用 tail 命令實時查看日志文件的(de)末尾(wei)
- journalctl 可以(yi)使用 -f 或 --follow 選項來實時查看最(zui)新的日志
- 服(fu)務和依賴關系:
- syslog 是一個通(tong)用的(de)日志服(fu)務,可(ke)以由多個日志守護進(jin)程(cheng)(如 rsyslog、syslog-ng)實現
- journalctl 是 systemd 系統中的一(yi)部分,依賴于 systemd-journald 服務
- 配(pei)置文件(jian)的路徑:
- syslog 的配置文件(jian)在 /etc/rsyslog.conf
- journalctl 的配置文件在 /etc/systemd/journald.conf
- 總體而(er)言,journalctl 是(shi) systemd 系統(tong)的(de)(de)一部(bu)分,提供了更(geng)現(xian)代(dai)化(hua)、結構(gou)化(hua)和強大的(de)(de)日志管理功能(neng),但這并不意味著 syslog 是(shi)過時的(de)(de)。在(zai)一些系統(tong)中,兩(liang)者可(ke)能(neng)同(tong)時存在(zai),而(er)且一些工具和服務可(ke)能(neng)仍然使(shi)用(yong)傳(chuan)統(tong)的(de)(de) syslog。選擇使(shi)用(yong)哪一個(ge)取決于系統(tong)的(de)(de)需求和管理員(yuan)的(de)(de)偏好。
常(chang)見命令(ling)
查看已啟動的service
systemctl list-units --type=service --state=active
UNIT                               LOAD   ACTIVE SUB     DESCRIPTION
aegis.service                      loaded active running Aegis Service
aliyun.service                     loaded active running Aliyun Assist
AssistDaemon.service               loaded active running AssistDaemon
atd.service                        loaded active running Job spooling tools
auditd.service                     loaded active running Security Auditing Service
chronyd.service                    loaded active running NTP client/server
cloud-config.service               loaded active exited  Apply the settings specified in cloud-config
cloud-final.service                loaded active exited  Execute cloud user/final scripts
cloud-init-local.service           loaded active exited  Initial cloud-init job (pre-networking)
cloud-init.service                 loaded active exited  Initial cloud-init job (metadata service crawler)
crond.service                      loaded active running Command Scheduler
dbus.service                       loaded active running D-Bus System Message Bus
getty@tty1.service                 loaded active running Getty on tty1
getty@tty2.service                 loaded active running Getty on tty2
gssproxy.service                   loaded active running GSSAPI Proxy Daemon
kmod-static-nodes.service          loaded active exited  Create list of required static device nodes for the current kernel
network.service                    loaded active running LSB: Bring up/down networking
polkit.service                     loaded active running Authorization Manager
postfix.service                    loaded active running Postfix Mail Transport Agent
rhel-dmesg.service                 loaded active exited  Dump dmesg to /var/log/dmesg
rhel-domainname.service            loaded active exited  Read and set NIS domainname from /etc/sysconfig/network
rhel-import-state.service          loaded active exited  Import network configuration from initramfs
rhel-readonly.service              loaded active exited  Configure read-only root support
rpcbind.service                    loaded active running RPC bind service
rsyslog.service                    loaded active running System Logging Service
serial-getty@ttyS0.service         loaded active running Serial Getty on ttyS0
sshd.service                       loaded active running OpenSSH server daemon
sysstat.service                    loaded active exited  Resets System Activity Logs
systemd-fsck-root.service          loaded active exited  File System Check on Root Device
systemd-journal-flush.service      loaded active exited  Flush Journal to Persistent Storage
systemd-journald.service           loaded active running Journal Service
systemd-logind.service             loaded active running Login Service
systemd-random-seed.service        loaded active exited  Load/Save Random Seed
systemd-remount-fs.service         loaded active exited  Remount Root and Kernel File Systems
systemd-sysctl.service             loaded active exited  Apply Kernel Variables
systemd-tmpfiles-setup-dev.service loaded active exited  Create Static Device Nodes in /dev
systemd-tmpfiles-setup.service     loaded active exited  Create Volatile Files and Directories
systemd-udev-trigger.service       loaded active exited  udev Coldplug all Devices
systemd-udevd.service              loaded active running udev Kernel Device Manager
systemd-update-utmp.service        loaded active exited  Update UTMP about System Boot/Shutdown
systemd-user-sessions.service      loaded active exited  Permit User Sessions
systemd-vconsole-setup.service     loaded active exited  Setup Virtual Console
tuned.service                      loaded active running Dynamic System Tuning Daemon
LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
43 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
查某服務-最(zui)新日(ri)志(zhi)
[root@iZwz93v6r4t2hixjvy9acoZ ~]# journalctl -u sshd -f
-- Logs begin at Tue 2024-04-09 10:35:44 CST. --
May 15 15:54:41 iZwz93v6r4t2hixjvy9acoZ sshd[7892]: Failed password for admin from 120.76.138.179 port 35866 ssh2
May 15 15:54:41 iZwz93v6r4t2hixjvy9acoZ sshd[7892]: Received disconnect from 120.76.138.179 port 35866:11: Bye Bye [preauth]
May 15 15:54:41 iZwz93v6r4t2hixjvy9acoZ sshd[7892]: Disconnected from 120.76.138.179 port 35866 [preauth]
May 15 18:14:21 iZwz93v6r4t2hixjvy9acoZ sshd[952]: Bad protocol version identification 'MGLNDD_112.74.62.175_22' from 52.160.38.231 port 36412
May 15 18:21:49 iZwz93v6r4t2hixjvy9acoZ sshd[2473]: Bad protocol version identification '\026\003\001' from 106.75.117.86 port 17348
May 15 18:21:50 iZwz93v6r4t2hixjvy9acoZ sshd[2474]: Bad protocol version identification 'GET / HTTP/1.1' from 106.75.117.86 port 17396
May 15 19:20:23 iZwz93v6r4t2hixjvy9acoZ sshd[13066]: Connection reset by 198.235.24.182 port 57536 [preauth]
May 15 20:15:57 iZwz93v6r4t2hixjvy9acoZ sshd[23226]: Accepted password for root from 36.111.36.149 port 13826 ssh2
May 15 20:20:33 iZwz93v6r4t2hixjvy9acoZ sshd[24091]: Did not receive identification string from 87.236.176.86 port 38419
May 15 20:20:34 iZwz93v6r4t2hixjvy9acoZ sshd[24092]: Connection closed by 87.236.176.86 port 51943 [preauth]
- -u sshd
- u是Unit縮寫,查sshd服(fu)務的日志
- -f
- 類似tail -f,實(shi)時顯示日志條(tiao)目(mu),即隨著(zhu)新條(tiao)目(mu)的產生而(er)不斷更新顯示
查某服(fu)務-某時間點后-某關(guan)鍵字日志
journalctl -u sshd --since "2024-05-15" | grep Connection
或
journalctl -u sshd --since today | grep Connection
查看內核(he)日志(不顯(xian)示應用(yong)日志)
journalctl -k -f- -k --dmesg
- Show kernel message log from the current boot

查看系(xi)統(tong)本次啟動的日志
journalctl -b -f