• CentOS7日志文件及journalctl日志查看


    一、日 志 文 件 说 明

    tail /var/log/messages          //系统启动后的信息和错误日志,是Red Hat Linux中最常用的日志之一
    tail -f /var/log/secure         //与安全相关的日志信息
    tail /var/log/maillog           //与邮件相关的日志信息
    tail /var/log/cron              //与定时任务相关的日志信息
    tail /var/log/dmesg             //和系统启动相关
    tail /var/log/audit/audit.log   //系统审计日志
    tail /var/log/yum.log           //yum
    tail /var/log/xferlog           //和访问FTP服务器相关
    tail /var/log/wtmp             //当前登录的用户(命令:w)
    tail /var/log/btmp             //最近登录的用户(命令last)
    tail /var/log/lastlog          //所有用户的登录情况(命令lastlog )
    tail /var/log/boot.log         //守护进程启动和停止相关的日志消息
    tail /var/log/spooler          //与UUCP和news设备相关的日志信息
    #清理日志文件
    cat /dev/null > /var/log/boot.log
    cat /dev/null > /var/log/btmp
    cat /dev/null > /var/log/cron
    cat /dev/null > /var/log/dmesg
    cat /dev/null > /var/log/firewalld
    cat /dev/null > /var/log/grubby
    cat /dev/null > /var/log/lastlog
    cat /dev/null > /var/log/mail.info
    cat /dev/null > /var/log/maillog
    cat /dev/null > /var/log/messages
    cat /dev/null > /var/log/secure
    cat /dev/null > /var/log/spooler
    cat /dev/null > /var/log/syslog
    cat /dev/null > /var/log/tallylog
    cat /dev/null > /var/log/wpa_supplicant.log
    cat /dev/null > /var/log/wtmp
    cat /dev/null > /var/log/yum.log
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31

    二、journalctl
    日志管理工具journalctl是centos7上专有的日志管理工具,该工具是从message这个文件里读取信息。
    三、查看所有日志

    #默认情况下 ,只保存本次启动的日志
    [root@192 role]# journalctl
    
    • 1
    • 2

    四、查看内核日志

    [root@192 role]# journalctl -k
    
    • 1

    五、查看系统本次启动的日志

    [root@192 role]# journalctl -b
    
    • 1

    六、查看上一次启动的日志
    需要修改配置文件/etc/systemd/journald.conf

    journalctl -b -1
    
    • 1

    在这里插入图片描述需要将systemd-journald服务配置为在重新启动后永久保留系统日志,只需要将Storage参数设置为persistent。
    Storage参数设置值有:
    1、persistent:将日志存储在/var/log/journal目录中,该目录在重新启动后仍然存在。
    2、volatile:将日记存储在/run/log/journal目录中,这不会导致系统重启。
    3、auto:rsyslog将确定使用持久性存储(persistent)还是易失性存储(volatile),如果存在/var/log/journal目录,则rsyslog使用持久性存储,否则使用易失性存储。
    对于永久存储,请将其设置为:
    [Journal]
    Storage=persistent
    提交更改后,重新启动systemd-journald服务以使配置更改生效:

    mkdir /var/log/journal
    systemd-tmpfiles --create --prefix /var/log/journal
    systemctl restart systemd-journald
    systemctl status systemd-journald
    journalctl --list-boots 
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    七、查看指定时间的日志
    可以使用–since和–until选项过滤任意时间限制,这些限制分别显示给定时间之前或之后的条目。
    可以使用“yesterday”、“today”、“tomorrow”或者“now”等表达。
    另外,我们也可以使用“-”或者“+”设定相对值,或者使用“ago”之前的表达

    #"显示2023年10月1号,18点00分00秒到当前时间之间的所有日志信息"
    [root@192 role]# journalctl --since="2023-10-1 18:00:00"
    #获取昨天的日志如下:
    journalctl --since yesterday
    #获取某一个时间段到当前时间的前一个小时的日志
    journalctl --since 09:00 --until "1 hour ago" 
    #获取当前时间的前20分钟的日志
    journalctl --since "20 min ago"
    #获取某一天到某一个时间段的日志信息
    journalctl --since "2023-10-1" --until "2023-10-23 20:00" 
    #获取15:15到现在的日志
    journalctl --since"15:15" --until now
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    八、按服务过滤消息日志

    [root@yunwei ~]# journalctl -u nginx
    Oct 22 21:07:13 yunwei.harbor.com systemd[1]: Starting The nginx HTTP and reverse proxy server...
    Oct 22 21:07:13 yunwei.harbor.com nginx[5849]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    Oct 22 21:07:13 yunwei.harbor.com nginx[5849]: nginx: configuration file /etc/nginx/nginx.conf test is successful
    Oct 22 21:07:13 yunwei.harbor.com systemd[1]: Started The nginx HTTP and reverse proxy server.
    
    [root@yunwei ~]# journalctl -u nginx  --since today
    Oct 22 21:07:13 yunwei.harbor.com systemd[1]: Starting The nginx HTTP and reverse proxy server...
    Oct 22 21:07:13 yunwei.harbor.com nginx[5849]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
    Oct 22 21:07:13 yunwei.harbor.com nginx[5849]: nginx: configuration file /etc/nginx/nginx.conf test is successful
    Oct 22 21:07:13 yunwei.harbor.com systemd[1]: Started The nginx HTTP and reverse proxy server.
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    九、按进程、用户或者群组ID

    #通过进程ID实现查询,需要指定_PID字段。
    journalctl _PID=8088
    #查看指定用户的日志
    journalctl _UID=33  --since today
    #查看指定用户组的日志
    journalctl _GID=20  --since today
    #显示尾部的最新10行日志
    journalctl  -n
    #显示尾部指定行数的日志
    journalctl -n 20
    #实时滚动显示最新日志
    journalctl -f
    #查看指定服务的日志
    journalctl  /usr/lib/systemd/systemd
    #查看某个路径的脚本的日志
    journalctl    /usr/bin/bash
    #实时滚动显示某个Unit的最新日志
    journalctl  -u nginx.service  -f
    #合并显示多个Unit的日志
    journalctl  -u nginx.service  -u php-fpm.service  --since today
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    十、按照优先级
    使用journalctl配合-p选项显示特定优先级的信息,从而过滤掉优先级较低的信息。
    由最高到最低优先级:

    0: emerg
    1: alert
    2: crit
    3: err
    4: warning
    5: notice
    6: info
    7: debug
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    [root@yunwei ~]# journalctl -p err -b
    Oct 22 20:52:19 localhost kernel: Spectre V2 : WARNING: Unprivileged eBPF is enabled with eIBRS on, data leaks possible via >
    Oct 22 20:52:20 localhost kernel: integrity: Unable to open file: /etc/keys/x509_ima.der (-2)
    Oct 22 20:52:20 localhost kernel: integrity: Unable to open file: /etc/keys/x509_evm.der (-2)
    Oct 22 20:52:21 localhost kernel: sd 2:0:0:0: [sda] Assuming drive cache: write through
    Oct 22 20:52:23 yunwei.harbor.com kernel: SELinux:  Runtime disable is deprecated, use selinux=0 on the kernel cmdline.
    Oct 22 20:52:23 yunwei.harbor.com kernel: piix4_smbus 0000:00:07.3: SMBus Host Controller not enabled!
    Oct 22 21:02:24 yunwei.harbor.com ansible-dnf[2424]: ldapdb_canonuser_plug_init() failed in sasl_canonuser_add_plugin(): inv>
    Oct 22 21:06:34 yunwei.harbor.com ansible-dnf[3464]: ldapdb_canonuser_plug_init() failed in sasl_canonuser_add_plugin(): inv>
    Oct 22 21:07:08 yunwei.harbor.com ansible-dnf[4963]: ldapdb_canonuser_plug_init() failed in sasl_canonuser_add_plugin(): inv>
    Oct 22 21:07:15 yunwei.harbor.com ansible-dnf[6205]: ldapdb_canonuser_plug_init() failed in sasl_canonuser_add_plugin(): inv>
    Oct 22 21:15:11 yunwei.harbor.com python3[6276]: ldapdb_canonuser_plug_init() failed in sasl_canonuser_add_plugin(): invalid>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    十一、输出格式

    #journal能够以多种格式进行显示,只须添加-o选项加格式说明即可。
    #将journal条目输出为JSON格式:
    [root@yunwei ~]# journalctl -b -u nginx  -o json
    {"INVOCATION_ID":"c5811766517b4d8992bdc66084b174da","SYSLOG_IDENTIFIER":"systemd","JOB_TYPE":"start","_SYSTEMD_SLICE":"-.sli>
    {"_SYSTEMD_CGROUP":"/system.slice/nginx.service","__REALTIME_TIMESTAMP":"1697980033570720","__MONOTONIC_TIMESTAMP":"89729072>
    {"_COMM":"nginx","__MONOTONIC_TIMESTAMP":"897292522","_TRANSPORT":"stdout","PRIORITY":"6","SYSLOG_FACILITY":"3","_SYSTEMD_UN>
    {"_EXE":"/usr/lib/systemd/systemd","_UID":"0","SYSLOG_IDENTIFIER":"systemd","CODE_FILE":"src/core/job.c","_GID":"0","_CAP_EF>
    lines 1-4/4 (END)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    十二、journal维护

    #查看当前日志占用磁盘的空间的总大小
    [root@yunwei ~]# journalctl --disk-usage
    Archived and active journals take up 8.0M in the file system.
    #指定日志文件最大空间
    journalctl --vacuum-size=1G
    #指定日志文件保存多久
    journalctl --vacuum-time=1years #1年
    journalctl --vacuum-time=2d     #2天
    journalctl --vacuum-size=500M   #500M
    #不分页标准输出,日志默认分页输出--no-pager改为正常的标准输出
    journalctl  --no-pager
    #分页显示,其中插入省略号以代表被移除的信息,使用–no-full选
    journalctl --no-full
    #检查日志是否如常?日志文件是否完好且未损坏?
    journalctl --verify
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    十三、启用日志消息的持久存储
    要启用日志限制持久性配置,你可以修改journald的配置文件
    /etc/systemd/journald.conf
    将该Storage=选项设置为“persistent”以启用持久记录。
    Storge选项为:
    volatile,则日记日志数据将仅存储在内存中,即/run/log/journal中。
    persistent,则数据将最好存储在磁盘上,即在/var/log/journal的下方。
    auto,类似于“ persistent”,但是如果需要的话不会创建目录/var/log/journal,因此它的存在控制着日志数据的去向。

    vim  /etc/systemd/journald.conf
    [Journal]
    Storage=persistent
    
    • 1
    • 2
    • 3

    在CentOS 7上,您必须启用日志消息的持久存储:

    mkdir /var/log/journal
    systemd-tmpfiles --create --prefix /var/log/journal
    systemctl restart systemd-journald
    
    • 1
    • 2
    • 3
  • 相关阅读:
    1036 Boys vs Girls
    校园交友|基于SprinBoot+vue的校园交友网站(源码+数据库+文档)
    Pytorch_basics_main
    虹科分享 | 网络流量监控 | 使用 ntopng 收件人和端点进行灵活的警报处理
    tidb-系统内核调优及对比
    组里来了一个实习生,一行代码引发了一个惨案
    (刘二大人)PyTorch深度学习实践-卷积网络(基础篇作业)
    Docker镜像的导入导出
    eBPF Talk:变量声明的位置
    【七】http服务器开发--客户端请求格式和服务器响应格式
  • 原文地址:https://blog.csdn.net/weixin_45432833/article/details/133978571