- 前言
- 一、日志管理
- 1.journald
- 2.实验1 journalctl命令的用法
- 3.实验2 用journald服务永久存放日志系统中默认日志在:/run/log/journal中
- 4.实验3.自定义日志采集路径
- 5.如何更改日志采集格式
- 6.日志的远程同步
- 7.timedatectl
- 8.时间同步服务
- 总结
这一节主要学习日志管理,如何永久查看日志;如何将日志文件保存在自己想要指定的目录中;如何远程同步日志;如何修改日志显示的格式;也学习了时间的查看和更改;以及时间发生错误时我们该如何使用同步其他主机的时间来做到时间的精确修改等等
一、日志管理 1.journald服务名称:systemd-journald.service
journalctl
默认日志存放路径: /run/log
journalctl -n 3 ##日志的最新3条 --since "2020-05-01 11:00:00" ##显示11:00后的日志 --until "2020-05-01 11:05:00" ##显示日志到11:05 -o ##设定日志的显示方式 # short 经典模式显示日志 # verbose 显示日志的全部字节 # export 适合传出和备份的二进制格式 # json js格式显示输出 -p ##显示制定级别的日志 #0 emerg 系统的严重问题日志 #1 alert 系统中立即要更改的信息 #2 crit 严重级别会导致系统软件不能正常工作 #3 err 程序报错 #4 warning 程序警告 #5 notice 重要信息的普通日志 #6 info 普通信息 #7 debug 程序拍错信息 -F PRIORITY ##查看可控日志级别 -u sshd ##指定查看服务 --disk-usage ##查看日志大小 --vacuum-size=1G ##设定日志存放大小 --vacuum-time=1W ##日志在系统中最长存放时间 -f ##监控日志 journalctl _PID=10924 _SYSTEMD_UNIT=sshd.service
37 journalctl 38 journalctl -n 3 查看最近的三条日志 39 journalctl --since "2022-08-06 00:30:00" 40 journalctl --until "2022-08-06 00:30:00" 41 journalctl -o 设定日志显示方式 42 journalctl -o short 43 journalctl -o verbose 44 journalctl -o export 45 journalctl -o json 46 journalctl -p 1 47 journalctl -p 2 48 journalctl -p 3 49 journalctl -p 3 50 q 51 journalctl -p 4 52 journalctl -u 指定查看服务 53 journalctl -u sshd 54 journalctl --disk-usage 查看日志大小 55 journalctl --vacuum-size=1G 56 journalctl --vacuum-time=1w 设定存放时间 57 history3.实验2 用journald服务永久存放日志系统中默认日志在:/run/log/journal中
默认方式在系统重启后日志会被清理要永久保存日志请完成以下操作:
mkdir /var/log/journal
chgrp systemd-journal /var/log/journal
chmod 2775 /var/log/journal
systemctl restart systemd-journald.service
当服务重启日志存放路径会被制定到:/var/log/journal
测试:
1.在操作以上步骤之前查看日志
2.重启系统
3.再次查看日志
4.可以看到日志是不会被保存下来的只能看到重启之后的日志
5.完成以上操作后再次重启系统可以看到日志是被保存下来的
查看最近的五个日志
[root@westos_student50 ~]# journalctl -n5 -- Logs begin at Sat 2022-08-06 10:19:23 CST, end at Sat 2022-08-06 18:19:19 CST. -- Aug 06 11:46:29 westos_student50.westos.org sshd[4051]: Accepted password for root from 172.25.254.5 port 65511 ssh2 Aug 06 11:46:30 westos_student50.westos.org systemd-logind[1011]: New session 4 of user root. Aug 06 11:46:30 westos_student50.westos.org systemd[1]: Started Session 4 of user root. Aug 06 11:46:30 westos_student50.westos.org sshd[4051]: pam_unix(sshd:session): session opened for user root by (uid=0) Aug 06 11:47:23 westos_student50.westos.org chronyd[929]: Selected source 162.159.200.1 [root@westos_student50 ~]# cat /run/log/journal/
重启主机之后再次查看最近的五个日志,发现之前的日志已经被自动清理
[root@westos_student50 ~]# journalctl -n5 -- Logs begin at Sat 2022-08-06 11:48:54 CST, end at Sat 2022-08-06 19:48:50 CST. -- Aug 06 11:49:24 westos_student50.westos.org systemd[1]: Reached target Multi-User System. Aug 06 11:49:24 westos_student50.westos.org systemd[1]: Reached target Graphical Interface. Aug 06 11:49:24 westos_student50.westos.org systemd[1]: Starting Update UTMP about System Runlevel Changes... Aug 06 11:49:24 westos_student50.westos.org systemd[1]: Started Update UTMP about System Runlevel Changes. Aug 06 11:49:24 westos_student50.westos.org systemd[1]: Startup finished in 1.067s (kernel) + 2.804s (initrd) + 32.977s (userspace) = 36.849s.
[root@westos_student50 ~]# chgrp systemd-journal /var/log/journal/ [root@westos_student50 ~]# chmod 2775 /var/log/journal/ 更改权限 [root@westos_student50 ~]# systemctl restart systemd-journald 重启服务 [root@westos_student50 ~]# hostname hostname hostnamectl [root@westos_student50 ~]# hostnamectl 查看主机名 Static hostname: westos_student50.westos.org Icon name: computer-vm Chassis: vm Machine ID: ec7e72e1b0b840088468f75ab76af82d Boot ID: 9283d35689ca4d1d86c2bae5cb2efe39 Virtualization: vmware Operating System: Red Hat Enterprise Linux 8.2 (Ootpa) CPE OS Name: cpe:/o:redhat:enterprise_linux:8.2:GA Kernel: Linux 4.18.0-193.el8.x86_64 Architecture: x86-64
这样更改之后,日志文件就会被放在/var/log/journal/中,再次重启系统,之前的日志文件就不会被删除
4.实验3.自定义日志采集路径vim /etc/rsyslog.conf
日志类型.日志级别 日志存放路径
. /var/log/westos ##把系统中所有级别的日志存放到westos中
.;authpriv.none /var/log/westos ##把系统中所有级别的日志存放到westos中
##但是authpriv不存放到westos中
日志类型
auth #用户认证
authpriv #服务认证
cron #时间任务
kern #内核类型
mail #邮件
news #系统更新信息
user #用户
日志级别
debug #程序排错信息
info #程序常规运行信息
notice #重要信息的普通日志
waring #程序警告
err #程序报错
crit #严重级别会导致系统软件不能正常工作
alert #系统中立即要更改的信息
emerg #系统的严重问题日志
none #不采集
[root@westos_student50 ~]# > /var/log/messages 清空目录下的日志 [root@westos_student50 ~]# cat /var/log/messages 查看日志 [root@westos_student50 ~]# logger hello mou 执行一步操作 [root@westos_student50 ~]# cat /var/log/messages 查看,可以看到刚刚的操作 Aug 7 21:30:57 westos_student50 root[2183]: hello mou [root@westos_student50 ~]# vim /etc/rsyslog.conf 进入文件编辑,将所有类型的文件放在自定义路径下 [root@westos_student50 ~]# > /var/log/w westos wtmp [root@westos_student50 ~]# > /var/log/westos 清空刚刚定义的目录 [root@westos_student50 ~]# logger hello mou [root@westos_student50 ~]# cat /etc/l ld.so.cache libaudit.conf libnl/ libssh/ locale.conf logrotate.conf lvm/ ld.so.conf libblockdev/ libpaper.d/ libuser.conf localtime logrotate.d/ ld.so.conf.d/ libibverbs.d/ libreport/ libvirt/ login.defs lsm/ [root@westos_student50 ~]# cat /etc/log login.defs logrotate.conf logrotate.d/ [root@westos_student50 ~]# cat /etc/log login.defs logrotate.conf logrotate.d/ [root@westos_student50 ~]# cat /var/log/westos Aug 7 21:37:04 westos_student50 root[2277]: hello mou 可以看到现在的日志已经显示在了我们自定义的目录中
下边是vim中的内容,我们将服务认证的日志不显示在我们自定义的目录中
#### RULES #### # Log all kernel messages to the console. # Logging much else clutters up the screen. #kern.* /dev/console # Log anything (except mail) of level info or higher. # Don't log private authentication messages! *.info;mail.none;authpriv.none;cron.none /var/log/messages *.*;authpriv.none /var/log/westos # The authpriv file has restricted access. authpriv.*5.如何更改日志采集格式
1.定义日志采集格式
$template WESTOS_FORMAT, “%FROMHOST-IP% %timegenerated% %FROMHOST-IP% %syslogtag% %msg%n”
#WESTOS_FORMAT: 格式名称
#%FROMHOST-IP%: 日志来源主机IP
#%timegenerated%: 日志生成时间
#%syslogtag%: 日志生成服务
#%msg%: 日志内容
#n: 换行
2.设定日志采集格式应用
在vim中进行以下修改
# Use default timestamp format #module(load="builtin:omfile" Template="RSYSLOG_TraditionalFileFormat") module(load="builtin:omfile" Template="MOUY") # Include all config files in /etc/rsyslog.d/ include(file="/etc/rsyslog.d/*.conf" mode="optional") $template MOUY, "%FROMHOST-IP% %timegenerated%n"
重启服务之后,就可以看到日志以下面格式显示
127.0.0.1 Aug 8 12:54:33 127.0.0.1 Aug 8 12:54:33 127.0.0.1 Aug 8 12:54:33 127.0.0.1 Aug 8 12:54:33 127.0.0.1 Aug 8 12:54:33 127.0.0.1 Aug 8 12:54:336.日志的远程同步
westos_node1:172.25.254.20 存放日志作为日志接受端,所有人日志都存放到此台主机
westos_linux:172.25.254.10 发送日志到westos_node1主机中
1.在westos_node1中设定接受所有人的日志
systemctl stop firewalld
vim /etc/rsyslog.conf
19 module(load=“imudp”) ##打开日志接受插件
20 input(type=“imudp” port=“514”) ##指定插件使用接口
systemctl restart rsyslog
查询端口:
root@rhel7_node1 ~]# netstat -antlupe | grep rsyslog
udp 0 0 0.0.0.0:514 0.0.0.0:* 0
67600 11115/rsyslogd
udp6 0 0 :::514 :: 0
67601 11115/rsyslogd
2.westos_linux中设定发送日志到westos_node1中
vim /etc/rsyslog.conf
. @172.25.254.20
systemctl restart rsyslog
@ 表示使用udp传输日志
@@ 表示使用tcp传输日志
@172.25.254.20 把本机日志用udp的传输方式发送到172.25.254.20主机
测试: 在westos_linux和westos_node1中 > /var/log/messages
在westos_linux中
logger westos test message
在westos_node1中可以看到westos_linux中生成的日志!!
.;authpriv.none /var/log/westos;WESTOS
module(load=“builtin:omfile” Template=“WESTOS_FORMAT”) ##默认采用WESTOS_FORMAT格式
将514端口打开
16 17 # Provides UDP syslog reception 18 # for parameters see http://www.rsyslog.com/doc/imudp.html 19 module(load="imudp") # needs to be done just once 20 input(type="imudp" port="514") 21 22 # Provides TCP syslog reception 23 # for parameters see http://www.rsyslog.com/doc/imtcp.html 24 #module(load="imtcp") # needs to be done just once 25 #input(type="imtcp" port="514") 26
重启服务,并且查看514端口是否打开
[root@westos_student50 ~]# systemctl restart rsyslog.service [root@westos_student50 ~]# netstat -antlupe |grep rsyslog udp 0 0 0.0.0.0:514 0.0.0.0:* 0 49080 2713/rsyslogd udp6 0 0 :::514 :::* 0 49081 2713/rsyslogd7.timedatectl
timedatectl set-time “2020-02-13 10:41:55” ##设定系统时间
timedatectl list-timezones ##显示系统的所有时区
timedatectl set-timezone “Asia/Shanghai” ##设定系统时区
timedatectl set-local-rtc 0|1 ##设定系统时间计算方式
##0表示使用utc时间计算方式
[root@westos_student50 ~]# vim /etc/rsyslog.conf [root@westos_student50 ~]# timedatectl Local time: Mon 2022-08-08 13:08:14 CST Universal time: Mon 2022-08-08 05:08:14 UTC RTC time: Mon 2022-08-08 05:08:14 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes NTP service: active RTC in local TZ: no8.时间同步服务
有时候,我们的主机时间可能会出现问题,我们需要将其他时间正确的主机上的时间同步在我们要更改时间的主机上,所以就有了时间同步服务
############################### **#服务名称: chronyd.service #配置文件: /etc/chrony.conf 在rhel7作为时间源rhel8同步rhel7时间** 在time_server中 vim /etc/chrony.conf 26 allow 172.25.254.0/24 ##允许172.25.254.0网段主机同步时间 29 local stratum 10 ##开启时间同步服务器功能并设定级别为10 systemctl restart chronyd.service systemctl stop firewalld 在time_clinet中 vim /etc/chrony.conf pool 172.25.254.100 iburst systemctl restart chronyd 查看: 在time_client中查看时间: 现实已经变成time_server中时间 使用chronyc 命令查看时间效果: [root@rhel8_node1 ~]# 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 || Reachability register (octal) -. | 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.11 10 6 17 15 -6970ns[+8437ns] +/- 180us
首先关闭时间同步服务,再修改时间,再次查看就可以看到时间已经被修改
[root@westos_student50 ~]# systemctl disable --now chronyd.service Removed /etc/systemd/system/multi-user.target.wants/chronyd.service. [root@westos_student50 ~]# timedatectl set-time "2022-08-08 15:00:00" [root@westos_student50 ~]# timedatectl Local time: Mon 2022-08-08 15:00:03 CST Universal time: Mon 2022-08-08 07:00:03 UTC RTC time: Mon 2022-08-08 07:00:03 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: no NTP service: inactive RTC in local TZ: no
下边这条命令可以更改时区,
timedatectl set-timezone ""
以下是操作步骤
这是在服务端开启时间同步服务器功能
[root@westos_student50 ~]# systemctl restart chronyd.service [root@westos_student50 ~]# systemctl stop firewalld
允许要修改时间的主机网段的同步,并且关闭火墙
# Allow NTP client access from local network. allow 192.168.0.0/16 # Serve time even if not synchronized to a time source. local stratum 10
在要修改的主机中对文件进行修改,重启服务后就可以看到时间已经恢复
[root@westos_student50 ~]# [root@westos_student50 ~]# timedatectl Local time: Tue 2022-08-09 01:39:30 CST Universal time: Mon 2022-08-08 17:39:30 UTC RTC time: Mon 2022-08-08 17:04:29 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: no NTP service: inactive RTC in local TZ: no [root@westos_student50 ~]# timedatectl set-time "2022-08-09 01:00:00" [root@westos_student50 ~]# timedatectl Local time: Tue 2022-08-09 01:00:02 CST Universal time: Mon 2022-08-08 17:00:02 UTC RTC time: Mon 2022-08-08 17:00:02 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: no NTP service: inactive RTC in local TZ: no [root@westos_student50 ~]# vim /etc/chrony.conf [root@westos_student50 ~]# systemctl restart chronyd.service [root@westos_student50 ~]# timedatectl Local time: Tue 2022-08-09 01:50:34 CST Universal time: Mon 2022-08-08 17:50:34 UTC RTC time: Mon 2022-08-08 17:50:34 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: yes NTP service: active RTC in local TZ: no
通过chronyc sources -v 命令就可以看到自己的主机同步的是哪一个主机的时间
[root@westos_student50 ~]# 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 || Reachability register (octal) -. | xxxx = adjusted offset, || Log2(Polling interval) --. | | yyyy = measured offset, || | | zzzz = estimated error. || | | MS Name/IP address Stratum Poll Reach LastRx Last sample =============================================================================== ^* 192.168.0.29 3 6 377 60 +423us[+1244us] +/- 111ms [root@westos_student50 ~]#总结
这一节主要学习日志管理,如何永久查看日志;如何将日志文件保存在自己想要指定的目录中;如何远程同步日志;如何修改日志显示的格式;也学习了时间的查看和更改;以及时间发生错误时我们该如何使用同步其他主机的时间来做到时间的精确修改等等