学习Linux日志管理及优化方式

Linux日志管理及优化方式

实验环境

两台虚拟机

rhel8:192.168.1.32

rhel7:   192.168.1.33

1. journald

 服务名称  systemd-journald.service

 默认日志存放路径 /run/log (重启后会清空)

实验1

journalctl命令的用法

journalctl                            ##直接执行,浏览系统日志

journalctl            -n 3            ##显示最新3条

journalctl --since "2020-2-13 12:30" --until "2020-2-13 21:00"从什么时间到什么时间的日志

journalctl            -o  verbose        ##显示日志能够使用的详细进程参数


journalctl            -o  short        ##经典模式显示日志 


journalctl             -o  export        ##适合传出和备份的二进制格式 


journalctl            -o  json        ##js格式显示

journalctl  -p   显示级别的日志

journalctl -F PRIORITY
 

journalctl -u sshd

journalctl  --disk-usage

journalctl --vacuum-size(临时)

journalctl --vacuum-time(临时)

vim /etc/systemd/journald_conf 永久设定日志容量大小

journalctl -f 监控

查看指定pid的sshd日志

journalctl -o export 通过查看二进制日志获取信息

journalctl _PID=12861 _SYSTEMD_UNIT=sshd.service

实验2 用journald服务永久存放日志

系统默认从存放位置 /run/log/journal 中

默认方式在系统重启后日志会被清理,永久保存日志需要以下操作

测试

systemctl restart systemd-journal.service

reboot 重启虚拟机

2. rsyslog

服务名称:rsyslog.service

/var/log/messages    ##系统服务及日志,信息,报错
/var/log/secure         ##系统认证信息日志
/var/log/maillog         ##系统邮件服务信息
/var/log/cron             ##系统定时任务信息
/var/log/boot.log       ##系统启动信息

配置文件:etc/rsyslog.conf

实验1 自定义日志文件采集路径

####日志类型####
auth                ##pam产生的日志
authpriv            ##ssh,ftp等登录信息的验证信息
cron                ##时间任务相关
kern                ##内核
lpr                 ##打印
mail                ##邮件
mark(syslog)–rsyslog    ##服务内部的信息,时间标识
news                ##新闻组
user                ##用户程序产生的相关信息
uucp                ##unix to unix copy, unix主机之间相关的通讯
local 1~7           ##自定义的日志设备

####日志级别####

debug               ##有调式信息的,日志信息最多
info                ##般信息的日志,最常用
notice              ##最具有重要性的普通条件的信息
warning             ##警告级别
err                 ##错误级别,阻止某个功能或者模块不能正常工作的信息
crit                ##严重级别,阻止整个系统或者整个软件不能正常工作的信息
alert               ##需要立刻修改的信息
emerg               ##内核崩溃等严重信息

vim /etc/rsyslog.conf

systemctl restart rsyslog.service 重启服务

cat /var/log/westos | less查看文件

实验2 

1.更改日志采集格式

vim  /etc/rsyslog.conf

30行

$template WESTOS, "%timegenerated% %FROMHOST-IP% %syslogtag% %msg%\n"
 

2.设置采集格式应用

cat /var/log/westos

实验3 日志的远程同步

rhel7:192.168.1.33                 作为接受台,所有人日志都存放在这台主机

rhel8: 192.168.1.32                   发送日志到rhel7中

1.在rhel7中接收日志

vim  /etc/rsyslog.conf

netstat -antlupe | grep rsyslog 查询端口

2.在rhel8中设定将日志发送到 rhel7中

vim  /etc/rsyslog.conf

配置日志发送方
*.*             @172.25.0.11        ##通过udp协议把日志发送到11主机,@udp,@@tcp

测试:

在两个虚拟机中清空日志 > /var/log/messages

在rhel8中logger westos test messages生成日志

在rhel7中查看



3.timedatectl

timedatectl         status            ##显示当前时间信息
                          set-time        ##设定当前时间

                          list-timezones ##显示系统所有时区
                          set-timezone  "Asia/shanghai"      ##设定当前时区
                           set-local-rtc 0|1    ##设定是否使用utc时间

timedatectl set-time "2020-2-13 19:56"

imedatectl status

timedatectl list-timezones

timedatectl set-timezone "Asia/Shanghai"

4. 时间同步

服务名称chronyd.service

配置文件/etc/chrony.cof

rhel7作为时间源,rhel8同步rhel7的时间

在rhel7中

vim /etc/chrony.conf

systemctl restart chronyd.service 重启服务

systemctl stop firewalld  关闭防火墙

在rhel8中

vim /etc/chrony.conf

systemctl restart chronyd

chronyc sources -v


 

发布了14 篇原创文章 · 获赞 5 · 访问量 801

猜你喜欢

转载自blog.csdn.net/qq_46094902/article/details/104294519