Journalctl速查表,过滤systemd日志
在此journalctl速查表中,我将展示"用于过滤和查看systemd日志的各种示例",例如Linux启动消息。
本文journalctl速查表是在使用CentOS 7时编写的,因此可以肯定地说,它也全面涵盖了RHEL 7,Fedora,Oracle Enterprise Linux以及整个Red Hat操作系统家族,以及Novell的SLES和OpenSUSE。
Journalctl速查表
日志数据由Journal systemd-journald服务收集,存储和处理。
它基于从内核,用户进程,标准输出,系统服务的标准错误输出或者通过其本地API接收到的日志记录信息来创建和维护称为日志的二进制文件。
这些期刊经过结构化和索引化,可提供相对较快的查找时间。
要查看所有systemd-journald收集的数据,请使用journalctl
实用程序。
此命令提供了几种过滤数据的方法,它是一个功能非常强大的实用程序。
在本文journalctl速查表中,我们将尝试探索所有区域以过滤systemd日志
1.查看没有任何参数的systemctl日志文件
让我们从我们的journalctl速查表开始,要访问systemd-journald日志,请使用不带任何参数的journalctl工具。
对于日志的基本视图,请输入root:
# journalctl
此命令的输出是系统上生成的所有日志文件的列表,包括系统组件和用户生成的消息。
此输出的结构类似于/var/log/messages /
中使用的结构,但有一些改进:
2.查看日记日志运行时
我们可以使用journalctl -f
查看类似于tail -f
的systemd日志运行时。
这将打开systemd-journald的实时查看模式,使我们可以实时查看滚动的新消息。
使用Ctrl + C中断。
# journalctl -f
3.使用数字标识符列出所有Linux启动消息
查看与以前的引导有关的Linux引导消息;可以通过传递--list-boots选项来查看:
# journalctl --list-boots 0 60f1ed8ef60c49fa80e9fed06ab93dec Sun 2019-09-01 07:35:02 IST—Fri 2019-09-06 10:23:36 IST
根据前面的Linux启动消息输出,我们可以看到七个包含Linux启动消息的文件。
我们可以通过传递文件的偏移量来查看这些文件中的任何一个。
每个文件的偏移量是第一列中的值。
因为只有一个文件,所以让我们看一下偏移量为0的Linux启动消息:
# journalctl -b 0 -- Logs begin at Sun 2019-09-01 07:35:02 IST, end at Fri 2019-09-06 10:23:55 IST. - Sep 01 07:35:02 rhel-7.example CROND[28806]: (root) CMD (/opt/cntdb/bin/check_ams_ntpd_services) Sep 01 07:35:02 rhel-7.example rsyslogd[27197]: imjournal: journal reloaded... [v8.24.0-34.el7 try http://www.rsyslog.com/e/0 ] Sep 01 07:35:02 rhel-7.example systemd[1]: Removed slice User Slice of root. Sep 01 07:35:04 rhel-7.example su[28854]: (to dbmrun) root on none Sep 01 07:35:04 rhel-7.example systemd[1]: Started Session c1558 of user dbmrun.
这里的Linux启动消息始于Sun 2019-09-01
4.根据时间戳过滤系统日志
我们可以基于时间戳查看系统日志。
有各种参数可以过滤此类消息。
其中一些如下所示:
# journalctl --since today -- Logs begin at Thu 2019-09-05 15:04:51 IST, end at Fri 2019-09-06 10:25:39 IST. - Sep 06 00:00:01 rhel-7.example systemd[1]: Created slice User Slice of root. Sep 06 00:00:01 rhel-7.example systemd[1]: Started Session 1457 of user root. Sep 06 00:00:01 rhel-7.example systemd[1]: Started Session 1459 of user root. Sep 06 00:00:01 rhel-7.example systemd[1]: Created slice User Slice of provgw.
# date Fri Sep 6 10:25:57 IST 2019
检查systemd-journald日志中的特定日期和时间
# journalctl --since "2019-08-26 15:00:00" -- Logs begin at Thu 2019-08-22 15:08:47 IST, end at Fri 2019-09-06 14:07:28 IST. - Aug 26 15:00:01 rhel-7.example systemd[1]: Started Session 1844 of user root. Aug 26 15:00:01 rhel-7.example systemd[1]: Starting Session 1844 of user root. Aug 26 15:00:01 rhel-7.example systemd[1]: Started Session 1845 of user root. Aug 26 15:00:01 rhel-7.example systemd[1]: Starting Session 1845 of user root.
在一段时间之间检查systemd-journald日志
# journalctl --since "2019-08-26 15:00:00" --until "2019-08-27 15:00:00" -- Logs begin at Thu 2019-08-22 15:08:47 IST, end at Fri 2019-09-06 14:15:03 IST. - Aug 26 15:00:01 rhel-7.example systemd[1]: Started Session 1844 of user root. Aug 26 15:00:01 rhel-7.example systemd[1]: Starting Session 1844 of user root. <Output trimmed> Aug 27 14:56:02 rhel-7.example su[26926]: pam_unix(su-l:session): session opened for user root by oamsys(uid=1000) Aug 27 14:56:02 rhel-7.example unknown[26949]: bash root 26927: User root logged from 10.43.143.55 Aug 27 14:56:02 rhel-7.example unknown[27077]: bash root 26927: User root logged from 10.43.143.55 Aug 27 14:56:02 rhel-7.example unknown[27125]: bash root 26927: User root logged from 10.43.143.55
还可以检查昨天和今天之间的systemd-journald日志
# journalctl --since yesterday --until now -- Logs begin at Thu 2019-09-05 15:07:24 IST, end at Fri 2019-09-06 14:51:20 IST. - Sep 05 15:07:24 rhel-7.example GoExample[4148]: 2019/09/05 15:07:24 Test message 684 Sep 05 15:07:24 rhel-7.example GoExample[4148]: 2019/09/05 15:07:24 Test message 685 Sep 05 15:07:24 rhel-7.example GoExample[4148]: 2019/09/05 15:07:24 Test message 686
5.根据单元文件过滤消息(例如:systemd-journald)
我们可以传递-u选项,并指定我们要寻找的服务:
其中我们正在检查" systemd-journald"服务的所有日志
# journalctl -u systemd-journald -- Logs begin at Thu 2019-08-22 15:08:47 IST, end at Fri 2019-09-06 14:08:30 IST. - Aug 22 15:08:47 rhel-7.example systemd-journal[267]: Runtime journal is using 8.0M (max allowed 4.0G, trying to leave 4.0G free Aug 22 15:08:47 rhel-7.example systemd-journal[267]: Journal started Aug 22 15:08:52 rhel-7.example systemd-journal[267]: Journal stopped
检查SSHD服务日志
# journalctl -u sshd.service -- Logs begin at Thu 2019-08-22 15:08:47 IST, end at Fri 2019-09-06 14:09:19 IST. - Aug 22 15:09:05 rhel-7.example systemd[1]: Starting OpenSSH server daemon... Aug 22 15:09:05 rhel-7.example sshd[2034]: Server listening on 10.43.138.1 port 22. Aug 22 15:09:05 rhel-7.example systemd[1]: Started OpenSSH server daemon. Aug 22 15:22:33 rhel-7.example sshd[6734]: Connection from 10.43.143.55 port 53350 on 10.43.138.1 port 22
或者,我们也可以使用_SYSTEMD_UNIT
# journalctl _SYSTEMD_UNIT=sshd.service -- Logs begin at Thu 2019-08-22 15:08:47 IST, end at Fri 2019-09-06 14:23:30 IST. - Aug 22 15:09:05 rhel-7.example sshd[2034]: Server listening on 10.43.138.1 port 22. Aug 22 15:22:33 rhel-7.example sshd[6734]: Connection from 10.43.143.55 port 53350 on 10.43.138.1 port 22
6.根据二进制文件过滤日志
在前面的示例中,我们还得到了与sshd单元交互的systemd日志。
但是,如果我们只想要sshd二进制文件中的日志
# journalctl /sbin/sshd -- Logs begin at Thu 2019-08-22 15:08:47 IST, end at Fri 2019-09-06 14:17:04 IST. - Aug 22 15:09:05 rhel-7.example sshd[2034]: Server listening on 10.43.138.1 port 22. Aug 22 15:22:33 rhel-7.example sshd[6734]: Connection from 10.43.143.55 port 53350 on 10.43.138.1 port 22 Aug 22 15:22:33 rhel-7.example sshd[6734]: Postponed keyboard-interactive for oamsys from 10.43.143.55 port 53350 ssh2 [preauth]
7.过滤日志以获取更多详细信息
让我们继续我们的journalctl速查表。
我们还可以将-x
与journalctl
命令配合使用,以增加消息目录中的解释文本来增加日志行。
这将添加说明性帮助文本,以将消息记录在可用的输出中。
# journalctl -u sshd.service -x -- Logs begin at Thu 2019-08-22 15:08:47 IST, end at Fri 2019-09-06 14:18:31 IST. - Aug 22 15:09:05 rhel-7.example systemd[1]: Starting OpenSSH server daemon... -- Subject: Unit sshd.service has begun start-up -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel - -- Unit sshd.service has begun starting up. Aug 22 15:09:05 rhel-7.example sshd[2034]: Server listening on 10.43.138.1 port 22. Aug 22 15:09:05 rhel-7.example systemd[1]: Started OpenSSH server daemon. -- Subject: Unit sshd.service has finished start-up -- Defined-By: systemd -- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel - -- Unit sshd.service has finished starting up. - -- The start-up result is done. Aug 22 15:22:33 rhel-7.example sshd[6734]: Connection from 10.43.143.55 port 53350 on 10.43.138.1 port 22
8.根据流程PID过滤日志
要查看基于进程PID的系统日志,我们可以使用_PID
,如下所示:
# journalctl _PID=26730 -- Logs begin at Thu 2019-08-22 15:08:47 IST, end at Fri 2019-09-06 14:21:43 IST. - Aug 27 14:56:01 rhel-7.example sshd[26730]: Connection from 10.43.143.55 port 49391 on 10.43.138.1 port 22 Aug 27 14:56:01 rhel-7.example sshd[26730]: Postponed keyboard-interactive for oamsys from 10.43.143.55 port 49391 ssh2 [preauth] Aug 27 14:56:01 rhel-7.example sshd[26730]: Postponed keyboard-interactive/pam for oamsys from 10.43.143.55 port 49391 ssh2 [prea Aug 27 14:56:01 rhel-7.example sshd[26730]: Accepted keyboard-interactive/pam for oamsys from 10.43.143.55 port 49391 ssh2 Aug 27 14:56:01 rhel-7.example sshd[26730]: pam_unix(sshd:session): session opened for user oamsys by (uid=0) Aug 27 14:56:01 rhel-7.example sshd[26730]: User child is on pid 26733
9.根据优先级过滤日志
按消息优先级或者优先级范围过滤输出。
采用单个数字或者文本日志级别(即0/emerg和7/debug之间),或者采用FROM..TO
形式的一系列数字/文本日志级别。
日志级别是通常的系统日志级别,即" emerg"(0)," alert"(1)," crit"(2)," err"(3)," warning"(4)," notice"(5) ),"信息"(6),"调试"(7)。
其中我过滤了" emerg"优先级的日志。
# journalctl -p 0 -- Logs begin at Thu 2019-08-22 15:08:47 IST, end at Fri 2019-09-06 14:28:07 IST. - Sep 03 20:00:02 rhel-7.example dracut[15798]: Will not override existing initramfs (/tmp/test/initrd.img) without --force Sep 03 20:04:47 rhel-7.example dracut[30438]: Will not override existing initramfs (/tmp/test/initrd.img) without --force Sep 03 20:15:27 rhel-7.example dracut[17519]: Will not override existing initramfs (/tmp/test/initrd.img) without --force
其中我们正在过滤介于" emerg"(0)和" critical"(2)之间的范围的日志
# journalctl -p 0..2 -- Logs begin at Thu 2019-08-22 15:08:47 IST, end at Fri 2019-09-06 14:28:23 IST. - Aug 23 03:29:11 rhel-7.example binary-check[11670]: ALERT Load check failed, the load might have been compromised! Detail follows Aug 23 03:29:11 rhel-7.example binary-check[11671]: ALERT /usr/bin/dbus-cleanup-sockets: FAILED. This file has been modified! Aug 23 03:29:11 rhel-7.example binary-check[11672]: ALERT /usr/bin/dbus-daemon: FAILED. This file has been modified!
10.过滤内核消息
在此journalctl速查表中,仅查看内核生成的systemd日志,可以使用(-k)。
等效于(--dmesg
)。
在某种程度上,我们还可以查看来自内核的Linux启动消息。
# journalctl -k -- Logs begin at Thu 2019-08-22 15:08:47 IST, end at Fri 2019-09-06 14:31:08 IST. - Aug 22 15:08:47 rhel-7.example kernel: Initializing cgroup subsys cpuset Aug 22 15:08:47 rhel-7.example kernel: Initializing cgroup subsys cpu Aug 22 15:08:47 rhel-7.example kernel: Initializing cgroup subsys cpuacct
或者,我们也可以使用_TRANSPORT,其中所有与内核匹配的日志都将被过滤
提示:
传输参数设置为驱动程序,系统日志,日志,标准输出或者内核。
# journalctl _TRANSPORT=kernel -- Logs begin at Thu 2019-08-22 15:08:47 IST, end at Fri 2019-09-06 14:31:53 IST. - Aug 22 15:08:47 rhel-7.example kernel: Initializing cgroup subsys cpuset Aug 22 15:08:47 rhel-7.example kernel: Initializing cgroup subsys cpu Aug 22 15:08:47 rhel-7.example kernel: Initializing cgroup subsys cpuacct
11.检查日志日志使用的磁盘或者内存
如果使用的是持久性存储,则以下输出将显示已使用的磁盘量,如果使用的是非持久性存储,则此命令将显示用于systemd日志的内存量。
# journalctl --disk-usage Archived and active journals take up 384.0M on disk.
12.执行日志日志文件清除
我们可以使用--vaccum-size来删除已归档的日志文件,直到它们使用的磁盘空间小于指定的大小(以通常的" K"," M"," G"," T"后缀指定)为止,
# journalctl --disk-usage Archived and active journals take up 384.0M on disk.
我们将使用以下命令将日记文件的使用量减少到200MB:
# journalctl --vacuum-size=200M Deleted archived journal /run/log/journal/9bf0fc6f60ce41149a0a540f52db9773/system@822262d9c350464d9b67a3fd4fa4b8d8-00000000003bf4d9-000591cb0a1ae4c4.journal (40.0M). Deleted archived journal /run/log/journal/9bf0fc6f60ce41149a0a540f52db9773/system@822262d9c350464d9b67a3fd4fa4b8d8-00000000003c7a18-000591cb0bb46d64.journal (40.0M). Deleted archived journal /run/log/journal/9bf0fc6f60ce41149a0a540f52db9773/system@822262d9c350464d9b67a3fd4fa4b8d8-00000000003cff37-000591cb0d3dbe9b.journal (40.0M). Deleted archived journal /run/log/journal/9bf0fc6f60ce41149a0a540f52db9773/system@822262d9c350464d9b67a3fd4fa4b8d8-00000000003d847a-000591cb0ec7bb1a.journal (40.0M). Deleted archived journal /run/log/journal/9bf0fc6f60ce41149a0a540f52db9773/system@822262d9c350464d9b67a3fd4fa4b8d8-00000000003e0998-000591cb1060df61.journal (24.0M). Vacuuming done, freed 184.0M of archived journals on disk.
现在检查磁盘/内存使用情况日志日志
# journalctl --disk-usage Archived and active journals take up 200.0M on disk.
另外,我们也可以使用--vacuum-time,也可以同时使用--vacum-size和--vacuum-time来限制已归档日记文件的大小和时间限制
提示:
--vaccum-time
以通常的" s"," min"," h"," days"," months"," weeks"," years"后缀指定
13.根据用户过滤日志
要查看单个用户的systemd-journald日志,可以使用USERID
查看与映射用户有关的日志数据。
例如,我有一个用户ID为1008的用户" hynman"
# id oamsys uid=1008(hynman) gid=100(users) groups=100(users),10(wheel)
因此,要过滤此用户的systemd日志,我可以使用以下命令:
# journalctl _UID=1008 -- Logs begin at Thu 2019-09-05 15:07:24 IST, end at Fri 2019-09-06 14:43:49 IST. - Sep 06 11:26:21 rhel-7.example sshd[20525]: Starting session: shell on pts/3 for oamsys from 10.136.206.251 port 60337 id 0 Sep 06 11:26:23 rhel-7.example USERACT[20723]: pts/3, oamsys, su Sep 06 11:26:25 rhel-7.example su[20724]: (to root) oamsys on pts/3 Sep 06 11:26:25 rhel-7.example su[20724]: pam_unix(su:session): session opened for user root by oamsys(uid=1008) Sep 06 12:16:19 rhel-7.example sshd[20525]: Read error from remote host 10.136.206.251 port 60337: Connection timed out Sep 06 12:16:19 rhel-7.example su[20724]: pam_unix(su:session): session closed for user root
14.在详细模式下使用journalctl查看日志
要以详细模式查看systemd日志,请使用-o verbose。
# journalctl -o verbose -- Logs begin at Mon 2019-09-02 16:16:39 IST, end at Wed 2019-09-04 04:22:31 IST. - Mon 2019-09-02 16:16:39.603861 IST [s=ce2e96e21a954a1fa83a102c90207756;i=1;b=7b6f05a323bf4d8fb43149c8fc7c95c0;m=186bd5;t=5918f PRIORITY=6 _TRANSPORT=driver MESSAGE=Runtime journal is using 8.0M (max allowed 189.4M, trying to leave 284.2M free of 1.8G available → current limit 1 MESSAGE_ID=ec387f577b844b8fa948f33cad9a75e6 _PID=87 _UID=0 _GID=0 _COMM=systemd-journal _EXE=/usr/lib/systemd/systemd-journald _CMDLINE=/usr/lib/systemd/systemd-journald _CAP_EFFECTIVE=5402800cf _SYSTEMD_CGROUP=/system.slice/systemd-journald.service _SYSTEMD_UNIT=systemd-journald.service _SYSTEMD_SLICE=system.slice _BOOT_ID=7b6f05a323bf4d8fb43149c8fc7c95c0 _MACHINE_ID=3a0d751560f045428773cbf4c1769a5c _HOSTNAME=master.example.com Mon 2019-09-02 16:16:39.603953 IST [s=ce2e96e21a954a1fa83a102c90207756;i=2;b=7b6f05a323bf4d8fb43149c8fc7c95c0;m=186c32;t=5918f PRIORITY=6 _BOOT_ID=7b6f05a323bf4d8fb43149c8fc7c95c0 _MACHINE_ID=3a0d751560f045428773cbf4c1769a5c