日志系统负责记录系统运行过程中内核产生的各种信息,并分别将它们存放到不同的欸之文件中,以便系统管理源进行故障排查、异常跟踪。
REHL8使用rsylog作为日志服务程序。

介绍

syslog是一个综合的日志记录系统,主要功能是为了方便管理日志和分类存放系统日志。
使用grep命令查看日志很费劲,rsylog是syslog的替代产品。Redhat和Fedora使用rsylog替换了syslog

rsylog配置文件及语法

rsylog默认配置文件为/etc/rsyslog.conf,定义了系统中需要监听的事件和对应的日志文件的保存位置。

例子

  1. *.info;mail.none;authpriv.none;cron.none /var/log/messages
  2. # The authpriv file has restricted access.
  3. authpriv.* /var/log/secure
  4. # Log all the mail messages in one place.
  5. mail.* -/var/log/maillog
  6. # Log cron stuff
  7. cron.* /var/log/cron
  8. # Everybody gets emergency messages
  9. *.emerg :omusrmsg:*
  10. # Save news errors of level crit and higher in a special file.
  11. uucp,news.crit /var/log/spooler
  12. # Save boot messages also to boot.log
  13. local7.*

每一行由两部分组成:
第一部分是一个或多个“设备”,设备后面跟一些空格字符
第二部分是一个“操作动作”

设备

设备本身分为两个字段,之间用一个小数点“.”分割。
前一个字段表示一项服务
后一个字段是一个优先级
通过设备将不同类型的消息发送到不同的地方。
在同一个rsyslog配置行上允许出现一个以上设备,但是必须使用“;”把他们分隔开
Linux操作系统可以识别的设备。

日志 功能
auth 由pam_pwdb报告的认证活动
authpriv 包括特权信息,eg:用户名在内的认证活动
cron 与cron和at有关的计划任务信息
daemon 与inectd守护进程有关的后台进程信息
kern 内核信息,首先通过klogd传递
lpr 与打印服务有关的服务
mail 与电子邮件有关的信息
mark syslog内部功能,用于生成时间戳
news 来自新闻服务器的信息
syslog 由syslog生成的信息
user 由用户程序生成的信息
uucp 由uucp生成的信息
local0-local7 与自定义程序使用

优先级

优先级代表消息的紧急程度。
优先级定义消息的紧急程序,按严重程度按高到底排列如表

日志 功能
emerg 该系统不可用,等同于panic
alert 需要立即呗修改的条件
crit 阻止某些子系统部分功能实现的错误条件,
等同于error
warning 预警信息,等同于warn
notice 具有重要性的普通条件
info 提供信息的消息
debug 不包含函数条件或问题的其他信息
none 没有重要级别,通常用于调试
* 所有级别,除了none

优先级限定符

使用三种限定符对优先级进行修饰:

  • 星号(*) 表示对应服务生成的所有日志消息都发送到操作动作到指定的地点
  • 等号(=) 表示只把对应服务生成的本优先级的日志消息都发送到操作动作指定的地点
  • 叹号(!) 表示把对应服务生成的所有日志消息都发生到操作动作指定的地点,但是本优先级的消息不包括在内,类似于编程语言中“非”运算符的用法

    操作动作

    日志信息可以分别记录到多个文件中,还可以发送到命名管道、其他程序,甚至远程主机
    常见的动作
    File:指定日志文件的绝对路径
    terminal或print:发送到串行或并行设备标志符。eg:/dev/ttyS2
    @host:远程的日志服务器
    Username:发送信息到本机的指定用户终端中,前提是该用户必须已经登录到系统中
    named pipe:发送到预先使用mkfifo命令来创建FIFO文件的绝对路径

    logrotate

    logrotate可以自动完成日志的压缩、备份、删除等工作,并可以设置为定时任务,eg:每日、每周或每月处理。

    参数

    | 参数 | 说明 | | —- | —- | | -d | 详细显示指令执行过程,便于调试或了解程序执行的情况 | | -f | 强行启动记录文件维护操作 | | -s | 使用指定的状态文件 | | -v | 在执行日志滚动时显示详细信息 | | -? | 显示帮助信息 |

配置文件

主配置文件
/etc/logrotate.conf 和 /etc/logrotate.d

[root@lhuan ~]# cat /etc/logrotate.conf 
# see "man logrotate" for details
#使用man logrotate查看更多帮助消息

# rotate log files weekly
#每周轮转
weekly

#保存过去4周的文件
# keep 4 weeks worth of backlogs
rotate 4

#轮转后创建新的空日志文件
# create new (empty) log files after rotating old ones
create

#轮转的文件以日志结尾,eg:messages-20201012
# use date as a suffix of the rotated file
dateext

#如果需要将轮转后日志压缩,可以去掉此行的注释
# uncomment this if you want your log files compressed
#compress

#其他配置可以放到此文件夹中
# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

#一些系统日志的轮转规则
# no packages own wtmp and btmp -- we'll rotate them here
/var/log/wtmp {
    monthly
    create 0664 root utmp
    minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.

配置文件参数

参数 说明
compress 通过gzip 压缩转储以后的日志
nocompress 不做gzip压缩处理
copytruncate 用于还在打开中的日志文件,把当前日志备份并截断;是先拷贝再清空的方式,拷贝和清空之间有一个时间差,可能会丢失部分日志数据。
nocopytruncate 备份日志文件不过不截断
create mode owner group 轮转时指定创建新文件的属性,如create 0777 nobody nobody
nocreate 不建立新的日志文件
delaycompress 和compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。
missingok 如果日志丢失,不报错继续滚动下一个日志
errors address 专储时的错误信息发送到指定的Email 地址
ifempty 即使日志文件为空文件也做轮转,这个是logrotate的缺省选项,也就默认选项
notifempty 当日志文件为空时,不进行轮转
mail address 把转储的日志文件发送到指定的E-mail 地址
nomail 转储时不发送日志文件
olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir 转储后的日志文件和当前日志文件放在同一个目录下
sharedscripts 运行postrotate脚本,作用是在所有日志都轮转后统一执行一次脚本。如果没有配置这个,那么每个日志轮转后都会执行一次脚本
prerotate 在logrotate转储之前需要执行的指令,例如修改文件的属性等动作;必须独立成行
postrotate 在logrotate转储之后需要执行的指令,例如重新启动 (kill -HUP) 某个服务!必须独立成行
daily 指定转储周期为每天
weekly 指定转储周期为每周
monthly 指定转储周期为每月
rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份
dateext 使用当期日期作为命名格式
dateformat .%s 配合dateext使用,紧跟在下一行出现,定义文件切割后的文件名,必须配合dateext使用,只支持 %Y %m %d %s 这四个参数
size(或minsize) log-size 当日志文件到达指定的大小时才转储,log-size能指定bytes(缺省)及KB (sizek)或MB(sizem).
当日志文件 >= log-size 的时候就转储。 以下为合法格式:(其他格式的单位大小写没有试过)
size = 5 或 size 5 (>= 5 个字节就转储)
size = 100k 或 size 100k
size = 100M 或 size 100M

systemd日志

systemd主要是服务管理所产生的日志和系统日志

配置文件

/etc/systemd/journald.conf

[root@lhuan ~]# cat /etc/systemd/journald.conf 
#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See journald.conf(5) for details.

[Journal]
#Storage=auto
#Compress=yes
#Seal=yes
#SplitMode=uid
#SyncIntervalSec=5m
#RateLimitInterval=30s
#RateLimitBurst=1000
#SystemMaxUse=
#SystemKeepFree=
#SystemMaxFileSize=
#RuntimeMaxUse=
#RuntimeKeepFree=
#RuntimeMaxFileSize=
#MaxRetentionSec=
#MaxFileSec=1month
#ForwardToSyslog=yes
#ForwardToKMsg=no
#ForwardToConsole=no
#ForwardToWall=yes
#TTYPath=/dev/console
#MaxLevelStore=debug
#MaxLevelSyslog=debug
#MaxLevelKMsg=notice
#MaxLevelConsole=info
#MaxLevelWall=emerg
#LineMax=48K

journalctl

1.journalctl 展示的是一个交互式界面,用空格翻页,q键退出

[root@lhuan ~]# journalctl 
-- Logs begin at Sat 2020-02-22 18:16:12 CST, end at Wed 2020-10-14 00:08:26 CST. --
Feb 22 18:16:12 VM_0_3_centos sshd[12331]: Received disconnect from 193.112.97.106 port 33710:11: Bye Bye
Feb 22 18:16:12 VM_0_3_centos sshd[12331]: Disconnected from 193.112.97.106 port 33710
Feb 22 18:24:57 VM_0_3_centos passwd[13514]: pam_unix(passwd:chauthtok): password changed for ftpuser
Feb 22 18:25:08 VM_0_3_centos passwd[13553]: pam_unix(passwd:chauthtok): authentication failure; logname=ftpuser uid=1000 euid=0 tty=pts/1 ruser= rhos
Feb 22 18:25:15 VM_0_3_centos passwd[13565]: pam_unix(passwd:chauthtok): authentication failure; logname=ftpuser uid=1000 euid=0 tty=pts/1 ruser= rhos
Feb 22 18:25:20 VM_0_3_centos passwd[13577]: pam_unix(passwd:chauthtok): authentication failure; logname=ftpuser uid=1000 euid=0 tty=pts/1 ruser= rhos
-- Reboot --

查看内核日志 -k

[root@lhuan ~]# journalctl  -k
-- Logs begin at Sat 2020-02-22 18:16:12 CST, end at Wed 2020-10-14 00:09:25 CST. --
Sep 03 15:21:11 VM_69_195_centos kernel: Initializing cgroup subsys cpuset
Sep 03 15:21:11 VM_69_195_centos kernel: Initializing cgroup subsys cpu
Sep 03 15:21:11 VM_69_195_centos kernel: Initializing cgroup subsys cpuacct
Sep 03 15:21:11 VM_69_195_centos kernel: Linux version 3.10.0-1062.9.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Re
Sep 03 15:21:11 VM_69_195_centos kernel: Command line: BOOT_IMAGE=/boot/vmlinuz-3.10.0-1062.9.1.el7.x86_64 root=UUID=4b499d76-769a-40a0-93dc-4a31a59ad
Sep 03 15:21:11 VM_69_195_centos kernel: e820: BIOS-provided physical RAM map:
Sep 03 15:21:11 VM_69_195_centos kernel: BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable
Sep 03 15:21:11 VM_69_195_centos kernel: BIOS-e820: [mem 0x000000000009fc00-0x000000000009ffff] reserved
Sep 03 15:21:11 VM_69_195_centos kernel: BIOS-e820: [mem 0x00000000000f0000-0x00000000000fffff] reserved

查看系统本次启动的日志 -b

[root@lhuan ~]# journalctl  -b
-- Logs begin at Sat 2020-02-22 18:16:12 CST, end at Wed 2020-10-14 00:09:51 CST. --
Sep 03 15:21:11 VM_69_195_centos systemd-journal[89]: Runtime journal is using 8.0M (max allowed 91.8M, trying to leave 137.8M free of 910.9M availabl
Sep 03 15:21:11 VM_69_195_centos kernel: Initializing cgroup subsys cpuset
Sep 03 15:21:11 VM_69_195_centos kernel: Initializing cgroup subsys cpu
Sep 03 15:21:11 VM_69_195_centos kernel: Initializing cgroup subsys cpuacct
Sep 03 15:21:11 VM_69_195_centos kernel: Linux version 3.10.0-1062.9.1.el7.x86_64 (mockbuild@kbuilder.bsys.centos.org) (gcc version 4.8.5 20150623 (Re
Sep 03 15:21:11 VM_69_195_centos kernel: Command line: BOOT_IMAGE=/boot/vmlinuz-3.10.0-1062.9.1.el7.x86_64 root=UUID=4b499d76-769a-40a0-93dc-4a31a59ad
Sep 03 15:21:11 VM_69_195_centos kernel: e820: BIOS-provided physical RAM map:
Sep 03 15:21:11 VM_69_195_centos kernel: BIOS-e820: [mem 0x0000000000000000-0x000000000009fbff] usable

查看指定时间的日志

root@lhuan ~]# journalctl  --since yesterday
-- Logs begin at Sat 2020-02-22 18:16:12 CST, end at Wed 2020-10-14 00:13:31 CST. --
Oct 13 00:00:01 lhuan systemd[1]: Started Session 57686 of user root.
Oct 13 00:00:01 lhuan systemd[1]: Started Session 57687 of user root.
Oct 13 00:00:01 lhuan CROND[15527]: (root) CMD (/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &)
Oct 13 00:00:01 lhuan CROND[15528]: (root) CMD (/usr/local/qcloud/YunJing/YDCrontab.sh > /dev/null 2>&1 &)


[root@lhuan ~]# journalctl  --since "2020-09-09" --until "2 day ago"
-- Logs begin at Sat 2020-02-22 18:16:12 CST, end at Wed 2020-10-14 00:14:45 CST. --
Sep 09 00:00:01 lhuan systemd[1]: Started Session 7856 of user root.
Sep 09 00:00:01 lhuan CROND[25107]: (root) CMD (/usr/local/qcloud/stargate/admin/start.sh > /dev/null 2>&1 &)
Sep 09 00:00:01 lhuan systemd[1]: Started Session 7857 of user root.
Sep 09 00:00:01 lhuan CROND[25108]: (root) CMD (/usr/local/qcloud/YunJing/YDCrontab.sh > /dev/null 2>&1 &)
Sep 09 00:00:21 lhuan sshd[25160]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=111.85.53.58  user=root
Sep 09 00:00:21 lhuan sshd[25160]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Sep 09 00:00:23 lhuan sshd[25160]: Failed password for root from 111.85.53.58 port 39765 ssh2


[root@lhuan ~]# journalctl  --since  "30 min ago"
-- Logs begin at Sat 2020-02-22 18:16:12 CST, end at Wed 2020-10-14 00:15:19 CST. --
Oct 13 23:45:19 lhuan sshd[4811]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=111.230.157.219  user=root
Oct 13 23:45:19 lhuan sshd[4811]: pam_succeed_if(sshd:auth): requirement "uid >= 1000" not met by user "root"
Oct 13 23:45:21 lhuan sshd[4811]: Failed password for root from 111.230.157.219 port 50858 ssh2
Oct 13 23:45:21 lhuan sshd[4811]: Received disconnect from 111.230.157.219 port 50858:11: Bye Bye [preauth]
Oct 13 23:45:21 lhuan sshd[4811]: Disconnected from 111.230.157.219 port 50858 [preauth]
Oct 13 23:45:42 lhuan sshd[4889]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=118.25.185.106  user=root

查看某个服务产生的日志

[root@lhuan ~]# journalctl  -u nginx.service
-- Logs begin at Sat 2020-02-22 18:16:12 CST, end at Wed 2020-10-14 00:16:06 CST. --
Sep 03 15:20:43 lhuan systemd[1]: Stopping nginx - high performance web server...
Sep 03 15:20:43 lhuan systemd[1]: Stopped nginx - high performance web server.
-- Reboot --
Sep 03 15:21:24 lhuan systemd[1]: Starting nginx - high performance web server...
Sep 03 15:21:25 lhuan nginx[1342]: nginx: [warn] conflicting server name "localhost" on 0.0.0.0:80, ignored
Sep 03 15:21:25 lhuan systemd[1]: Can't open PID file /var/run/nginx.pid (yet?) after start: No such file or directory
Sep 03 15:21:25 lhuan systemd[1]: Started nginx - high performance web server.

滚动显示某个服务产生的日志

[root@lhuan ~]# journalctl  -u nginx.service -f

利用日志定位问题

查看系统登录日志

[root@lhuan ~]# lastlog
Username         Port     From             Latest
root             pts/1    223.104.145.42   Tue Oct 13 23:40:11 +0800 2020
bin                                        **Never logged in**
daemon                                     **Never logged in**
adm                                        **Never logged in**
lp                                         **Never logged in**
sync                                       **Never logged in**
shutdown                                   **Never logged in**
halt                                       **Never logged in**
mail

查看历史命令

[root@lhuan ~]# history | grep nginx
    2  2020-02-22 00:09:31 vi /etc/yum.repos.d/nginx.repo
    4  2020-02-22 00:12:04 vim /etc/yum.repos.d/nginx.repo
    5  2020-02-22 00:14:12 yum install -y nginx
    6  2020-02-22 00:23:04 vim /etc/nginx/nginx.conf
    7  2020-02-22 00:28:07 vim /etc/nginx/nginx.conf
    8  2020-02-22 00:31:07 vim /etc/nginx/nginx.conf
    9  2020-02-22 00:33:34 systemctl start nginx
   10  2020-02-22 00:33:47 systemctl enable  nginx
   24  2020-02-22 01:53:23 echo "<?php phpinfo(); ?>" >> /usr/share/nginx/html/index.php
   25  2020-02-22 01:53:33 systemctl restart nginx
   32  2020-02-22 14:27:20 rm -rf /usr/share/nginx/html/index.php

查看系统日志

[root@lhuan ~]# head -n  10 /var/log/messages  

Dec 25 21:03:33 VM_0_3_centos rsyslogd: imjournal: journal reloaded... [v8.24.0-41.el7_7.2 try http://www.rsyslog.com/e/0 ]
Dec 25 21:03:33 VM_0_3_centos systemd: Stopped target Cloud-init target.
Dec 25 21:03:33 VM_0_3_centos ntpd[551]: ntpd exiting on signal 15
Dec 25 21:03:33 VM_0_3_centos systemd: Stopping RPC bind service...
Dec 25 21:03:33 VM_0_3_centos systemd: Stopped Dump dmesg to /var/log/dmesg.
Dec 25 21:03:33 VM_0_3_centos systemd: Stopping Authorization Manager...
Dec 25 21:03:33 VM_0_3_centos acpid: exiting
Dec 25 21:03:33 VM_0_3_centos systemd: Stopped target Multi-User System.
Dec 25 21:03:33 VM_0_3_centos systemd: Stopping Network Time Service...