<XI>日志管理

**


一、日志

1.journalctl

默认日志存放路径: /run/log

[root@localhost ~]# journalctl -n 3  ##日志的最新 3 条
[root@localhost ~]# journalctl --since "2020-11-05 20:10:00" ##显示 20:10:00 后的日志
[root@localhost ~]# journalctl --until "2020-11-05 21:10:00"##显示日志到 21:10:00
[root@localhost ~]# journalctl --since "2020-11-05 20:10:00" --until "2020-11-05 20:20:00"
[root@localhost ~]# journalctl -o short     #经典模式显示日志
[root@localhost ~]# journalctl -o verbose #显示日志的全部字节
[root@localhost ~]# journalctl -o export #适合传出和备份的二进制格式
[root@localhost ~]# journalctl -o json #js 格式显示输出

[root@localhost ~]# journalctl -p 0   ## emerg 系统的严重问题日志
[root@localhost ~]# journalctl -p 1 #alert 系统中立即要更改的信息
[root@localhost ~]# journalctl -p 2 #crit 严重级别会导致系统软件不能正常工作
[root@localhost ~]# journalctl -p 3 #error 程序报错
[root@localhost ~]# journalctl -p 4 #warning 程序警告
[root@localhost ~]# journalctl -p 5 #notice 重要信息的普通日志
[root@localhost ~]# journalctl -p 6 #info 普通信息
[root@localhost ~]# journalctl -p 7 #debug 程序拍错信息
[root@localhost ~]# journalctl -F PRIORITY ##查看可控日志级别
[root@localhost ~]# journalctl -u sshd ##指定查看服务
[root@localhost ~]# journalctl --disk-usage ##查看日志大小占用多大的文件系统journ
[root@localhost ~]# journalctl --vacuum-size=1G ##设定日志存放最大为1G 
[root@localhost ~]# journalctl --vacuum-time=1W ##日志在系统中最长存放时间为一周,可以使用设置以上两个参数来设置回滚
[root@localhost ~]# journalctl -f ##监控日志  journalctl -f
	journalctl _PID=10924 _SYSTEMD_UNIT=sshd.service

2.用 journald 服务永久存放日志

服务名称:systemd-journald.service
系统中默认日志在:/run/log/journal 中,默认方式在系统重启后日志会被清理,要永久保存日志请完成以下操作:

[root@localhost ~]# mkdir /var/log/journal
[root@localhost ~]# ls /var/log/journal
[root@localhost ~]# chgrp systemd-journal /var/log/journal
[root@localhost ~]# chmod 2755 /var/log/journal   #g+s,继承所属组
[root@localhost ~]# ls /var/log/journal
[root@localhost ~]# systemctl restart systemd-journald
[root@localhost ~]# ls /var/log/journal

设置完成后重启系统,会发现重启之前的日志是被保存下来的

在这里插入图片描述

3.自定义日志采集路径


[root@localhost ~]# cat /var/log/messages
[root@localhost ~]# vim /etc/rsyslog.conf
*.*    /var/log/westosall #把系统中所有级别的日志存放到westos 中

[root@localhost ~]# systemctl restart rsyslog.service #重启服务
[root@localhost ~]# vim /etc/rsyslog.conf  
*.*;authprive.none     /var/log/westosall #把系统中所有级别的日志存放到westos 中,##但是 authpriv 不存放到 westos 中
[root@localhost ~]# systemctl restart rsyslog.service #重启服务
[root@localhost ~]# > /var/log/westosall  #晴空

在这里插入图片描述在这里插入图片描述

4.监控其他主机的日志

1,其他主机的操作:
vim /etc/rsyslog.conf 
编辑内容
*.*                                                     @10.4.17.242
19和20行去掉注释
编辑完成
  
systemctl disable --now firewalld   #关掉火墙
systemctl restart rsyslog  #重启服务
2,本主机内的操作:
vim /etc/rsyslog.conf 
编辑内容
19和20行去掉注释
编辑完成
systemctl disable --now firewalld   #关掉火墙
systemctl restart rsyslog  #重启服务
> /var/log/messages
tail -f /var/log/messages   #此时,再10.4.17.242中操作时,10.4.17.241就能时事监控到10.4.17.242的日志

在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

二、如何更改日志采集格式

1. 定义日志采集格式

指令 功能
WESTOS_FORMAT 格式名称
FROMHOST-IP% 日志来源主机 IP
timegenerated% 日志生成时间
syslogtag 日志生成服务
msg 日志内容
\n 换行

2. 设定日志采集格式应用

[root@localhost ~]# vim /etc/rsyslog.conf 
编辑内容
 34 $Template WESTOSFORMAT,"%FROMHOST-IP% %timegenerated% %syslogtag% %msg%\n"
 47 *.*                /var/log/messages;WESTOSFORMAT
[root@localhost ~]# systemctl restart rsyslog.service   
[root@localhost ~]# systemctl status systemd-journald  #查看日志开启状态
[root@localhost ~]# > /var/log/messages 
[root@localhost ~]# cat /var/log/messages 

在这里插入图片描述在这里插入图片描述

3.timedatectl

[root@localhost ~]# timedatectl set-time "2020-02-13 10:41:55" ##设定系统时间
[root@localhost ~]# timedatectl
[root@localhost ~]# timedatectl list-timezones  ##显示系统的所有时区
[root@localhost ~]# timedatectl set-timezone "Atlantic/Azores"##设定系统时区
[root@localhost ~]# timedatectl
[root@localhost ~]# timedatectl set-timezone "Asia/Shanghai" ##设定系统时区
[root@localhost ~]# timedatectl
[root@localhost ~]# timedatectl set-local-rtc 0|1  ##设定系统时间计算方式##0 表示使用 utc 时间计算方式

在这里插入图片描述

4.时间同步服务

需要两个快照node1,node2
在服务端node2操作:
[root@localhost ~]# vim /etc/chrony.conf
allow 0.0.0.0 ##允许所有网段主机同步时间
29 local stratum 10##开启时间同步服务器功能并设定级别为 10
[root@localhost ~]# systemctl restart chronyd.service 
[root@localhost ~]# systemctl disable --now firewalld
[root@localhost ~]# timedatectl set-local-rtc 0

在客户端node1操作
[root@localhost ~]# vim /etc/chrony.conf
pool 172.25.254.203 iburst
[root@localhost ~]# systemctl restart chronyd
[root@localhost ~]# chronyc sources -v   #^*表示已经同步 

在服务端node2操作:vim /etc/chrony.conf
在这里插入图片描述在客户端node1操作: vim /etc/chrony.conf

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qiao_qing/article/details/109592796