searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

journal使用简介

2024-05-16 09:46:08
20
0

背景

  • 是什么?
    • Linux journal是systemd的一个组件,用于捕获和存储系统日志信息
    • Linux journal是systemd体系中负责日志管理的部分,由journald服务处理。
  • 解决什么问题?
    • journal旨在解决传统日志系统中存在的一些限制和问题。
      • 由于系统日志以二进制形式存储,这让日志的解析和使用变得更加灵活,比如可以以文本、JSON等多种格式进行转译以满足不同的需求。
      • journal按时间顺序存储事件,使得追踪单个事件变得容易。
      • journald还提供丰富的过滤和搜索选项,帮助系统管理员快速定位和解决问题。
  • 如何使用?
    • journalctl命令是与journal交互的主要工具,允许管理员查看和操作这些日志信息。

和rsyslog区别

journalctl 和 syslog 都是 Linux 系统中用于处理和查看系统日志的工具,我们从以下几个方面来对比下两者的区别:

  • 存储方式:
    • syslog 使用文本文件来存储日志信息,可以通过文本编辑器查看,通常存储在 /var/log/syslog 或 /var/log/message
    • journalctl 使用 systemd-journald 服务来存储二进制格式的日志文件。通常存储在 /run/log/journal/ 或 /var/log/journal/
  • 日志内容:
    • syslog 使用文本格式,记录在文本文件中。日志信息包含时间戳、主机名、应用程序名等
    • journalctl 使用二进制格式,可以存储更多的元数据,例如进程 ID、用户 ID、SELinux 上下文等。这使得日志更加结构化,支持更高级的查询和过滤
  • 查询和过滤:
    • syslog 的查询和过滤通常使用命令行工具(例如 grep)或者专用的工具(例如 logrotate)
    • journalctl 提供了更丰富和强大的查询和过滤功能,可以按时间、服务单元、日志级别等多个条件进行过滤。这使得查找和分析特定事件更加方便
  • 实时查看:
    • syslog 通常使用 tail 命令实时查看日志文件的末尾
    • journalctl 可以使用 -f 或 --follow 选项来实时查看最新的日志
  • 服务和依赖关系:
    • syslog 是一个通用的日志服务,可以由多个日志守护进程(如 rsyslog、syslog-ng)实现
    • journalctl 是 systemd 系统中的一部分,依赖于 systemd-journald 服务
  • 配置文件的路径:
    • syslog 的配置文件在 /etc/rsyslog.conf
    • journalctl 的配置文件在 /etc/systemd/journald.conf
  • 总体而言,journalctl 是 systemd 系统的一部分,提供了更现代化、结构化和强大的日志管理功能,但这并不意味着 syslog 是过时的。在一些系统中,两者可能同时存在,而且一些工具和服务可能仍然使用传统的 syslog。选择使用哪一个取决于系统的需求和管理员的偏好。

常见命令

查看已启动的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'.

查某服务-最新日志

[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服务的日志
  • -f
    • 类似tail -f,实时显示日志条目,即随着新条目的产生而不断更新显示

查某服务-某时间点后-某关键字日志

journalctl -u sshd --since "2024-05-15" | grep Connection
或
journalctl -u sshd --since today | grep Connection

查看内核日志(不显示应用日志)

journalctl -k -f
  • -k --dmesg
    • Show kernel message log from the current boot

查看系统本次启动的日志

journalctl -b -f

0条评论
作者已关闭评论
q****n
20文章数
0粉丝数
q****n
20 文章 | 0 粉丝
q****n
20文章数
0粉丝数
q****n
20 文章 | 0 粉丝
原创

journal使用简介

2024-05-16 09:46:08
20
0

背景

  • 是什么?
    • Linux journal是systemd的一个组件,用于捕获和存储系统日志信息
    • Linux journal是systemd体系中负责日志管理的部分,由journald服务处理。
  • 解决什么问题?
    • journal旨在解决传统日志系统中存在的一些限制和问题。
      • 由于系统日志以二进制形式存储,这让日志的解析和使用变得更加灵活,比如可以以文本、JSON等多种格式进行转译以满足不同的需求。
      • journal按时间顺序存储事件,使得追踪单个事件变得容易。
      • journald还提供丰富的过滤和搜索选项,帮助系统管理员快速定位和解决问题。
  • 如何使用?
    • journalctl命令是与journal交互的主要工具,允许管理员查看和操作这些日志信息。

和rsyslog区别

journalctl 和 syslog 都是 Linux 系统中用于处理和查看系统日志的工具,我们从以下几个方面来对比下两者的区别:

  • 存储方式:
    • syslog 使用文本文件来存储日志信息,可以通过文本编辑器查看,通常存储在 /var/log/syslog 或 /var/log/message
    • journalctl 使用 systemd-journald 服务来存储二进制格式的日志文件。通常存储在 /run/log/journal/ 或 /var/log/journal/
  • 日志内容:
    • syslog 使用文本格式,记录在文本文件中。日志信息包含时间戳、主机名、应用程序名等
    • journalctl 使用二进制格式,可以存储更多的元数据,例如进程 ID、用户 ID、SELinux 上下文等。这使得日志更加结构化,支持更高级的查询和过滤
  • 查询和过滤:
    • syslog 的查询和过滤通常使用命令行工具(例如 grep)或者专用的工具(例如 logrotate)
    • journalctl 提供了更丰富和强大的查询和过滤功能,可以按时间、服务单元、日志级别等多个条件进行过滤。这使得查找和分析特定事件更加方便
  • 实时查看:
    • syslog 通常使用 tail 命令实时查看日志文件的末尾
    • journalctl 可以使用 -f 或 --follow 选项来实时查看最新的日志
  • 服务和依赖关系:
    • syslog 是一个通用的日志服务,可以由多个日志守护进程(如 rsyslog、syslog-ng)实现
    • journalctl 是 systemd 系统中的一部分,依赖于 systemd-journald 服务
  • 配置文件的路径:
    • syslog 的配置文件在 /etc/rsyslog.conf
    • journalctl 的配置文件在 /etc/systemd/journald.conf
  • 总体而言,journalctl 是 systemd 系统的一部分,提供了更现代化、结构化和强大的日志管理功能,但这并不意味着 syslog 是过时的。在一些系统中,两者可能同时存在,而且一些工具和服务可能仍然使用传统的 syslog。选择使用哪一个取决于系统的需求和管理员的偏好。

常见命令

查看已启动的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'.

查某服务-最新日志

[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服务的日志
  • -f
    • 类似tail -f,实时显示日志条目,即随着新条目的产生而不断更新显示

查某服务-某时间点后-某关键字日志

journalctl -u sshd --since "2024-05-15" | grep Connection
或
journalctl -u sshd --since today | grep Connection

查看内核日志(不显示应用日志)

journalctl -k -f
  • -k --dmesg
    • Show kernel message log from the current boot

查看系统本次启动的日志

journalctl -b -f

文章来自个人专栏
云技术专栏
20 文章 | 1 订阅
0条评论
作者已关闭评论
作者已关闭评论
0
0