系统日志
1.日志采集规则:
修改日志采集规则编辑配置文件:vim /etc/rstslog.conf
内容如下:

systemctl stop rsyslog.service 关闭日志采集服务,日志依然会采集
systemctl restart rsyslog.service 开启日志采集服务
*.* 文件名称
表示:日志类型.日志级别 日志存放文件
日志类型:
auth 用户登陆日至(pam产生日志)
authpriv 服务认证日志(sshd日志)
kern 内和日志
cron 定时任务日志
mail 邮件日志
news 新闻日志
user 用户相关程序日志
local 1-7 用户自定义日志(1-7个级别)
日志级别:
debug 系统调试信息
info 常规信息
warning 警告信息
err 报错(级别底,阻止了某个功能不能正常工作)
crit 报错(级别高,阻止了某个软件或整个系统不能正常工作)
alert 需要立即修改的信息
emerg 内核崩溃
none 不采集任何日志信息
注意:从上到下,级别从低到高,记录的信息越来越少,详细的可以查看手册: man 3 syslog。
例如:
auth.debug /var/log/westos 用户登陆的系统调式日志采集到/var/log/westos
auth.* /var/log/hello 用户登陆的所有日志采集到/var/log/hello
*.* /var/log/all 所有类型的所有级别日志采集到/var/log/all
系统常用日志:
*.info;mail.none;authpriv.none;cron.none /var/log/messages 所有日志级别的常规信息(不包含邮件日志,服务认证服务,定时任务)
mail.* /var/log/maillog 邮件日志
authpriv.* /var/log/secure 服务认证日志
cron.* /var/log/cron 定时任务日志
2.日志的远程同步
为什么同步日志:运维人员管理很多台主机,每个查看有难度,所以把日志同到一台主机上查看。
可以通过三种方式发送:UPD(速度快,不需要握手),TCP(需要握手),RELP。
日志同步:
在日志发送方:
编辑配置文件:vim /etc/rsyslog.conf
修改内容:
*.* @172.25.254.114 日志接收方的地址

在日志接收方:
编辑配置文件:vim /etc/rsyslog.conf
修改内容:
14 # Provides UDP syslog reception 提供UPD的接收通道
15 $ModLoad imudp 接收插件
16 $UDPServerRun 514 接收的接口

systemctl restart rsyslog 重启服务
systemctl stop firewalld 关闭防火墙
systemctl disable firewalld 设置开机关闭防火墙
cat /var/log/messages 查看日志



3.自定义日志采集格式:
在日志接收方编辑配置文件:vim /etc/rsyslog.conf
修改内容:
$template 格式名称,“日志采集格式”


$template westos,"%timegenerated% %FROMHOST-IP% %syslogtag% %msg%\n"
%timegenerated%
日志来源主机ip
%FROMHOST-IP% 日志来源主机ip
%stslogtag% 日志生成程序
%msg% 日志内容
\n 换行
*.info;mail.none;authpriv.none;cron.none /var/log/messages;westos 日志采集格式为westos
cat /var/log/messages 查看日志
例如:$template westos,"%FROMHOST-IP% %timegenerated% %syslogtag% %msg%\n"
格式为:日志来源主机ip,日志来源主机ip,日志生成程序,日志内容,换行

4.系统日志查看工具
journalctl 参数
-n 3 最新3 行的日志
-p err 查看报错日志
-f 监控日志,ctrl+c结束监控
--since --until 查看从什么时间到什么时间的日志
-o verbose 查看日志详细参数 _pid=651 journalctl _pid=65
例如:journalctl -n 3 显示最近三行的日志

5.对systemd-journald管理
默认此程序只负责对日志进行查看而不对日志进行保存和采集。
那么关机后在开机,对日志进行查看,只能看到开机后的日志,系统之前的日志因为是保存在内存中的,所以关机后就被清空了,那么在开机时用journalctl看不到的。
如何让systemd-journald保存日志在硬盘中
mkdir /var/log/journal 建立目录
chgrp systemd-journal /var/log/journal 改变所属组
chmod g+s /var/log/journal/ 改变权限
ls -ld /var/log/journal 查看权限
killall -1 systemd-journald 重新加载配置

测试:
journalctl -n 3 查看最新3行日志
date 查看时间
reboot 重启
journalctl 查看日志(如果有reboot之前时间的日志说明设置成功)


6.连接符号
.xxx: 表示大于等于xxx级别的信息
.=xxx:表示等于xxx级别的信息
.!xxx:表示在xxx之外的等级的信息
7.时间同步
1.服务端共享时间
编辑配置文件:vim /etc/chrony.conf 主配置文件
21 # Allow NTP client access from local network.
22 allow 172.25.0.0/24 允许谁去同步我的时间
27 # Serve time even if not synchronized to any NTP server.
28 local stratum 10 不去同步任何人的时间,时间同步服务器级别
systemctl restart chronyd 重启服务
systemctl stop firewalld 关闭防火墙


2.客户端
编辑配置文件:vim /etc/chrony.conf
3 server 0.rhel.pool.ntp.org iburst
4 server 1.rhel.pool.ntp.org iburst====> server ntpserverip iburst
5 server 2.rhel.pool.ntp.org iburst====>
6 server 3.rhel.pool.ntp.org iburst
内容为:
server 172.25.254.107 iburst 同步172.25.254.107这台主机的时间
syatemctl restsrt chronyd 重启服务
chronyc sources -v 查看同步的信息

重启服务:systemctl restart chronyd
关闭防火墙:systemctl stop firewalld
测试:
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.0.11 10 6 377 41 +170us[ +201us] +/- 191us
注意:"*"表示同步成功。

8.timedatectl命令
timedatectl status 显示当前时间信息
set-time 设定当前时间
set-timezone 设定当前时区
set-local-rtc 0|1 设定是否使用utc时间
例如:timedatectl status 显示当前时间信息

例如:timedatectl set-time "2018-12-12" 设定当前时间为2018-12-12

例如:timedatectl set-timezone Asia/Shanghai 设置当前时区为上海

例如:timedatectl set-local-rtc 0 不使用UTC时间

相关文章: