系统日志管理
1.rsyslog ##此服务是用来采集系统日志的,它不产生日志只是起到采集作用
2.rsyslog的管理
/var/log/message ##服务信息日志
/var/log/secure ##系统认证日志
/var/log/maillog ##系统邮件服务日志
/var/log/spooler ##系统定时任务日志
/var/log/boot.log ##系统启动日志
3.日志的远程同步
在日志发送方:
vim /etc/rsyslog.conf
*.* @172.25.254.200 ##@表示udp协议发送,@@表示tcp协议发送
systemctl restart rsyslog
在日志接收方:
vim /etc/rsyslog.conf
15 $ModLoad imudp ##日志接收模块
16 $UDPServerRun 514 ##开启接收端口
systemctl restart rsyslog
systemctl stop firewalld ##关闭防火墙
systemctl disable firewalld ##设置开机不启动防火墙
在日志发送方:
logger test
cat /var/log/messages
在日志接收方:
cat /var/log/messages
4.日志采集格式
vim /etc/rsyslog.conf
$template$template LOGTMF, "%timegenerated% %FROMHOST-IP% %syslogtag% %msg%\n"
%timegenerated% ##显示日志时间
%FROMHOST-IP% ##显示主机ip
%syslogtag% ##日志记录目标
%msg% ##日志内容
\n ##换行
*.* /var/log/westos;LOGTMF
cat /var/log/westos
5.时间同步服务
服务名称
chronyd
在服务端:
vim /etc/chrony.conf
allow 172.25.254.0/24 ##允许那些客户端来同步本机时间
local stratum 10 ##本机不同步任何主机的时间,本机作为时间源
systemctl restart chronyd
在客户端:
vim /etc/chrony.conf
server 172.25.254.205 iburst ##本机立即同步205主机的时间
systemctl restart chronyd
测试:
[[email protected] Desktop]# chronyc sources -v
210 Number of sources = 1
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| / '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
|| .- xxxx [ yyyy ] +/- zzzz
|| / xxxx = adjusted offset,
|| Log2(Polling interval) -. | yyyy = measured offset,
|| \ | zzzz = estimated error.
|| | |
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* 172.25.254.105 10 6 377 51 -5380ns[ +13ms] +/- 176us
时区:
timedatectl ##显示当前时间信息
timedatectl set-timezone America/New_York ##设定当前时区
timedatectl set-time ##设定当前时间
timedatectl set-local-rtc 0|1 ##设定是否使用utc时间
timedatectl list-timezones ##查看支持的所有时区
6.journal
1.journalctl ##日志查看工具
journalctl -n 3 ##查看最近3条日志
journalctl -p err ##查看错误日志
journalctl -o verbose ##查看日志的详细参数
journalctl -o --since “xxxx-xx-xx xx:xx:xx” ##查看从什么时间开始的日志
journalctl -o --until “xxxx-xx-xx xx:xx:xx” ##查看到什么时间截至的日志
2.如何使用systemd-journald保存系统日志
默认systemd-journald是不保存系统日志到硬盘的
那么关机后再次开机只能看到本次开机之后的日志
上次关机之前的日志是无法查看的
通过以下命令可以保存系统日志
mkdir /var/log/journal
chgrp systemd-journal /var/log/journal
chmod g+s /var/log/journal
killall -1 systemd-journald
查看该文件夹,会有一个保存日志数据的文件。
ls -l /var/log/journal