Linux系统中的日志管理
实验环境
虚拟机:westos_node1 :172.25.254.110
本地主机 : 172.25.254.40
虚拟机关闭防火墙:systemctl stop firewalld
1.journald
-
服务器名称: systemd-journald.service
-
journalctl默认存放路径: /run/log
- journalctl 命令的用法
-
journalctl:命令显示全部日志
-
journalctl -n 3 显示日志最新三条
-
journalctl --since “3:00:00” 查看3点以后的日志
-
journalctl --until “3:00:00” 查看3点以前的日志
-
调整日志显示的模式
journalctl -o short -->经典模式显示日志
journalctl -o verbose -->显示日志的全部字节(显示日志本身相关的全部内容)
journalctl -o export -->适合传出和备份的二进制格式
journalctl -o json --> s格式显示输出
-
journalctl -p --显示指定级别的日志
journalctl -p 0 | emerg | 系统的严重问题的目录 |
journalctl -p 1 | alert | 系统中立即要根改的信息 |
journalctl -p 2 | crit | 严重级别会导致系统软件不能正常工作 |
journalctl -p 3 | err | 程序报错 |
journalctl -p 4 | warning | 程序警告 |
journalctl -p 5 | notice | 重要信息的普通日至 |
journalctl -p 6 | info | 普通信息 |
journalctl -p 7 | debug | 程序排错的信息 |
-
journalctl -F PRIORITY --> 查看可控日志级别
-
journalctl -u sshd(sshd可改) -->查看指定服务器的日志
-
journalctl --disk-usage 查看所有日志总共的大小
-
journalctl --vacuum-size=2G 设定日志存放大小
-
journalctl --vacuum-time=1w在系统中最长存放时间
9)journalctl -f 监控日志
10) journalctl _PID=889
journalctl -o verbose
journalctl _BOOT_ID=b52f50ddf03945618fe9fc7aa22e53f2
journalctl _MACHINE_ID=ea6e1822adaf414891a444e2a3fa22d4
2. 用 journalctl 服务永久存放日志
- 系统默认日志存放在: /run/log/journal/
- 系统在重启动时会默认清空日志
永久保存日志方法:
- journal文件如果属于root将不被识别
关闭防火墙:systemctl disable --now firewalld.service
建立新文件夹:mkdir /var/log/journal/
查看:ls -ld /var/log/journal/
设置文件所属所为systemd-journal: chgrp systemd-journal /var/log/journal/
查看:ls -ld /var/log/journal/
修改权限:chmod 2775 /var/log/journal
强制位:2-g+s:目录中新建的文件自动归属到目录所属组中
查看:ls -ld /var/log/journal/
3.永久删除日志文件
切换目录: cd /run/log/journal/
查看目录下内容:ls–>得到一串ea6e1822adaf414891a444e2a3fa
删除目录下的文件: rm -rf ea6e1822adaf414891a444e2a3fa
再次查看确保文件已删除: ls–>无文件
重启恢复服务:systemctl restart systemd-journald
实验:
实验环境:开启第二个虚拟机
westos-vmctl create westos_node2
westos-vmctl start westos_node2
westos-vmctl view westos_node2
后关闭westos_node2
virt-manager—设置网桥
开启westos_node2
nm-connection-editor --设置网卡
先删除原来的enpls0
新增:。。。。
检测连通性: ping 172.25.254.10
连接成功–>
关闭防火墙:systemctl disable --now firewalld.service
ping 连接其他电脑–其他电脑必须三打开的状态。
桥接不通
bridge-link
1 检查是否有ip
2 硬件损坏
采集日志到硬盘。不开也有日志生存
stop rsyslog 后/var/log
指定位置–>
rsyslog:
配置文件:/etc/rsyslog.conf --> 第46行编写类型.级别 /var/log/…
实验过程:
连接第一台虚拟机:ssh root@172.25.254.110
更改虚拟机名称为westos_node1: hostnamectl set-hostname westos_node1.westos.org
退出:logout
连接第一台虚拟机:ssh root@172.25.254.110
查看配置文件的状态:systemctl status rsyslog.service
cd /var/log
ls
systemctl restart sshd
tail /var/log/messages
实验:
编写临时配置文件: vim ~/.vimrc–> :set nu
(1)编写rsyslog.conf配置文件:vim /etc/rsyslog.conf–>
第47行 编写为 . /var/log/westos(所以类型文件的所有级别日志信息都存在/var/log/westos)
第一次查询无结果–>没有重启服务 (ls -l /var/log/westos)
重启服务:systemctl restart rsyslog.service
查看:s -l /var/log/westos
(2)编写rsyslog.conf配置文件:vim /etc/rsyslog.conf–>第47行 编写为 authpriv.* /var/log/westos(所以服务类型文件的所有级别日志信息都存在/var/log/westos)
重启服务:systemctl restart rsyslog.service
查看:s -l /var/log/westos
查看messages信息:cat /var/log/messages
清空文件内容: > /var/log/westos
查看内容:cat /var/log/westos
重启服务:systemctl restart sshd
再次查看文件内容变化:cat /var/log/westos
tail /var/log/messages
日志推送:(UDP)
man 5 rsyslog.conf
打开文件:vim /etc/rsyslog.conf
搜索:/remote
/remote machine 远程终端
tcp:最稳定@@
UDP:更快@
RELP:保存本机
虚拟机1:接受方1
1 关闭防火墙:
2 在日志接受方开启:vim /etc/rsyslog.conf
编写19-20行 都去掉“#”–>:wq保存
重启服务:systemctl restart rsyslog.service
查看端口信息(514):ss -alntupe | grep rsyslog
两边清空日志:> /var/log/messages
查看:car /var/log/messages
编写测试日志–>监控文件:tail -f /var/log/messages
–>接收logger内容
3 日志发送方node2: vim /etc/rsyslog.conf
编写任意一空行: . @172.25.254.100(此为接受方ip)
重启服务:systemctl restart rsyslog.service
编写测试日志–> 生成日志:logger + 任意文字
测试:
node1:tail监控
node2:looger生成日志
采集日志:
更改一个文件夹的接收方式:
1 日志接受方开启:vim /etc/rsyslog.conf
2 编写 1.任意行(39行):$template WESTOS, “%FROMHOST-IP% %timegenerated% %syslogtag% %msg% \n”–>2. RULES里46后一行(加上WESTOS)
例如:46 *.info;mail.none;authpriv.none;cron.none /var/log/messages;WESTOS
3 重启服务:systemctl restart rsyslog.service
4 > /var/log/messages
5 logger test
6 cat /var/log/messages
接受全部日志都更改为指定方式:
1 日志接受方开启:vim /etc/rsyslog.conf
2 编写:33 module(load=“builtin:omfile” Template=“WESTOS”)
(提前复制保存一行)33行–所有的日志都变成westos模式
3 重启服务:systemctl restart rsyslog.service
4 > /var/log/messages
5 logger test
6 cat /var/log/messages
四、时间同步服务
-
服务名称: chronyd.service
-
配置文件: /etc/chrony.conf
-
在服务端作为时间源,客户端同步服务端时间
-
timedatectl命令 显示本机系统时间
-
修改系统时间 timedatectl set-time “2021-02-01 11:29:59”
-
修改RTC时间 timedatectl set-local-rtc 1
默认伦敦时间 修改RTC时间后传输,握手可能出错
-
调整时区: timedatectl set-timezone “Africa/Banjul”
-
调整时间的计算方式: timedatectl set-local-rtc 0
-
查看所有的时区:timedatectl list-timezones
-
在time_server时间服务器中
服务端
关闭防火墙: systemctl stop firewalld
编辑服务配置文件:vim /etc/chrony.conf
修改文件内容:
23行 allow 0.0.0.0/0 ----- 允许所有网段主机同步时间
30行 local stratum 10 -----开启时间同步服务器功能并设定级别为10
重启服务:systemctl restart chronyd.service
-
在time_clinet中
客户端
编辑服务配置文件:vim /etc/chrony.conf
修改文件内容:
3行—改为pool 172.25.254.40 iburst(ip地址为服务端ip)
重启服务器:systemctl restart chronyd.service
把系统时间同步到硬件:clock -w
在time_client中查看时间: timedatectl
现实已经变成time_server中时间
用chronyc 命令检查时间同步状态:chronyc sources -v