linux系统的日志管理和时间同步服务

Linux系统中的日志管理

实验环境

虚拟机:westos_node1 :172.25.254.110
本地主机 : 172.25.254.40
虚拟机关闭防火墙:systemctl stop firewalld

1.journald

  • 服务器名称: systemd-journald.service
    在这里插入图片描述

  • journalctl默认存放路径: /run/log
    在这里插入图片描述

  1. journalctl 命令的用法
  • journalctl:命令显示全部日志
    在这里插入图片描述

  • journalctl -n 3 显示日志最新三条
    在这里插入图片描述

  • journalctl --since “3:00:00” 查看3点以后的日志
    在这里插入图片描述

  • journalctl --until “3:00:00” 查看3点以前的日志
    在这里插入图片描述

  1. 调整日志显示的模式
    journalctl -o short -->经典模式显示日志
    在这里插入图片描述

    journalctl -o verbose -->显示日志的全部字节(显示日志本身相关的全部内容)
    在这里插入图片描述

    journalctl -o export -->适合传出和备份的二进制格式
    在这里插入图片描述

    journalctl -o json --> s格式显示输出
    在这里插入图片描述

  2. journalctl -p --显示指定级别的日志
    在这里插入图片描述

journalctl -p 0emerg系统的严重问题的目录
journalctl -p 1alert系统中立即要根改的信息
journalctl -p 2crit严重级别会导致系统软件不能正常工作
journalctl -p 3err程序报错
journalctl -p 4warning程序警告
journalctl -p 5notice重要信息的普通日至
journalctl -p 6info普通信息
journalctl -p 7debug程序排错的信息
  1. journalctl -F PRIORITY --> 查看可控日志级别
    在这里插入图片描述

  2. journalctl -u sshd(sshd可改) -->查看指定服务器的日志
    在这里插入图片描述

  3. journalctl --disk-usage 查看所有日志总共的大小
    在这里插入图片描述

  4. journalctl --vacuum-size=2G 设定日志存放大小
    在这里插入图片描述

  5. 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命令 显示本机系统时间
    在这里插入图片描述

  1. 修改系统时间 timedatectl set-time “2021-02-01 11:29:59”
    在这里插入图片描述

  2. 修改RTC时间 timedatectl set-local-rtc 1
    默认伦敦时间 修改RTC时间后传输,握手可能出错
    在这里插入图片描述

  3. 调整时区: timedatectl set-timezone “Africa/Banjul”
    在这里插入图片描述

  4. 调整时间的计算方式: timedatectl set-local-rtc 0
    在这里插入图片描述

  5. 查看所有的时区: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
    在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值