【Linux】Linux中的日志查询方法

发布时间:2024-01-24 10:53:51

linux日志与日志的查询方法

在Linux系统中,日志文件用于记录系统的各种运行信息和错误消息。常见的日志文件包括但不限于/var/log/下的各种日志,如message、secure、maillog、cron等。

对于采用Systemd作为初始化系统的Linux,除了传统的文本日志文件外,还提供了Journal存储机制,可以通过journalctl命令进行复杂且高效的查询操作:

journalctl [OPTIONS...] [MATCHES...] 
命令说明
journalctl -x查看日志
journalctl -xe跳到尾部查看日志

更多journalctl用法

Options:

optionsdescription
–systemShow the system journal
–userShow the user journal for the current user
-M --machine=CONTAINEROperate on local container
-S --since=DATEShow entries not older than the specified date
-U --until=DATEShow entries not newer than the specified date
-c --cursor=CURSORShow entries starting at the specified cursor
–after-cursor=CURSORShow entries after the specified cursor
–show-cursorPrint the cursor after all the entries
–cursor-file=FILEShow entries after cursor in FILE and update FILE
-b --boot[=ID]Show current boot or the specified boot
–list-bootsShow terse information about recorded boots
-k --dmesgShow kernel message log from the current boot
-u --unit=UNITShow logs from the specified unit
–user-unit=UNITShow logs from the specified user unit
-t --identifier=STRINGShow entries with the specified syslog identifier
-p --priority=RANGEShow entries with the specified priority
–facility=FACILITY…Show entries with the specified facilities
-g --grep=PATTERNShow entries with MESSAGE matching PATTERN
–case-sensitive[=BOOL]Force case sensitive or insenstive matching
-e --pager-endImmediately jump to the end in the pager
-f --followFollow the journal
-n --lines[=INTEGER]Number of journal entries to show
–no-tailShow all lines, even in follow mode
-r --reverseShow the newest entries first
-o --output=STRINGChange journal output mode (short, short-precise,
-x --catalogAdd message explanations where available
-a --allShow all fields, including long and unprintable
-q --quietDo not show info messages and privilege warning
-m --mergeShow entries from all available journals
-D --directory=PATHShow journal files from directory

Commands:

commandsdescription
–helpShow this help text
–versionShow package version
–fieldsList all field names currently used
–field=FIELDList all values that a specified field takes
–disk-usageShow total disk usage of all journal files
–vacuum-size=BYTESReduce disk usage below specified size
–vacuum-files=INTLeave only the specified number of journal files
–vacuum-time=TIMERemove journal files older than specified time
–verifyVerify journal file consistency
–syncSynchronize unwritten journal messages to disk
–relinquish-varStop logging to disk, log to temporary file system
–smart-relinquish-varSimilar, but NOP if log directory is on root mount
–flushFlush all journal data from /run into /var
–rotateRequest immediate rotation of the journal files
–headerShow journal header information
–list-catalogShow all message IDs in the catalog
–dump-catalogShow entries in the message catalog
–update-catalogUpdate the message catalog database
–setup-keysGenerate a new FSS key pair

journalctl用法案例

journalctl -a --header

journalctl -a --header会展示系统日志中的所有记录,并且每条记录前面都会带有详细的头部信息。
在这里插入图片描述

journalctl -a

执行此命令会查看存储在系统 journal 中的完整日志历史。包括过去和现在的、与当前控制台关联的以及不关联的所有日志条目,不论它们的日志级别(优先级)如何。
在这里插入图片描述

journalctl -xe

journalctl -xe命令主要用于查看系统最新的、详尽的日志信息,且这些信息会直接输出到终端屏幕,方便用户实时监控系统的最新状态和事件。
在这里插入图片描述

id mirror
journalctl _UID=1000 -n 20

从系统日志中查找并显示由用户ID为1000的用户产生的最近20条日志条目。
在这里插入图片描述

journalctl --since "2024-01-23 12:00:00" --until "2024-01-23 14:00:00"
  • --since "2024-01-23 12:00:00":这个选项用于从特定时间点开始显示日志记录,即从2024年1月23日的中午12点开始。

  • --until "2024-01-23 14:00:00":这个选项用于限制输出的日志条目范围,仅显示直到特定时间点的日志记录,此处指到同一天下午2点为止。
    在这里插入图片描述

journalctl -u ssh.service 

或者

journalctl --unit ssh.service

查看某个服务的日志,这里举例的是ssh服务。
在这里插入图片描述

部分日志路径说明

日志路径说明
/var/log/message全局系统日志,包括登录,对服务启停认证等
/var/log/lastlog不是一个文本文件,需要用lastlog命令读,保存了用户最近的登录信息
/var/log/yun.log最近通过yum安装的程序的日志
/var/log/cron定时任务的日志
/var/log/boot.log启动日志
/var/log/kern内核日志,也可以通过dmesg查看

推荐阅读

文章来源:https://blog.csdn.net/weixin_37813152/article/details/135770869
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。