#####################系统日志的管理与时间同步的设定#########################
1、系统日志管理
后台的程序(通常被称为守护进程或者服务进程)处理了你的linux系统的大部分任务,当这些守护进程工作的时候,他们会将任务的详细信息记录日志文件中,作为他们做过什么的历史信息(包括错误信息)
/var/log/messages ##记录系统中产生的日志
1、rsyslog服务:此服务用来采集系统日志,它不产生日志,只是起到采集的作用
2、rsyslog的管理:
vim /etc/rsyslog.conf
/var/log/messages 服务信息日志(记录linux操作系统常见的服务信息和常见信息)
/var/log/secure 系统的登陆日志(记录用户和工作组的变化情况,是系统安全日志,用户的日志,用户的认证的的登陆情况)
/var/log/mailing 邮件日志
/var/log/cron 定时任务
/var/log/boot.log 系统启动日志
什么类型的日志.什么级别的日志
(1)日志的类型:
auth 用户认证时产生的日志,login su 命令
authpriv ssh,ftp等登陆服务的验证信息
cron 与时间任务相关
kern 系统内核消息
lpr 与打印机活动有关
mail 邮件日志
mark 服务内部的信息,是时间标识(产生时间戳)
user 用于用户程序产生的相关信息
uucp unix to unix copy unix主机之间相关的通道
news.crit 新闻组(网络新闻传输协议(nnpt)产生的消息
(2)日志级别分为
debug 有调试信息(日志信息产生的最多)
info 一般信息的日志(最常用)
notice 最具有重要性的普通条件信息(不是错误,但是可能需要你去处理)
warning 警告
err 错误(一般错误)
crit 严重错误(情况危险)
alert 需要立即修改的信息(例如:数据库损坏)
emerg 内核崩溃
none 什么都不记录
2、日志的远程同步
实验环境:server19:172.25.254.219 (日志的发送方)
desktop19:172.25.254.119 (日志的接收方)
关闭防火墙:systemctl stop firewalld
systemctl disable firewalld
在日志的发送方:
vim /etc/rsyslog.conf
*.* @172.25.254.119
######@:表示udp协议发送 @@:表示tcp协议发送 ip:接收方的ip
systemctl restart rsyslog.server
在日志的接受方:
vim /etc/rsyslog.conf
15 $ModLoad imudp 日志的接收模块
16 $UDPServerRun 514 开启接收端口
测试:
在发送方和接受方都清空日志文件
> /var/log/messages
在日志的发送方
logger test
cat /var/log/messages 查看日志是否生成
在日志的接受方:
tail -f /var/log/messages 动态监控文件的末尾信息
3、定义日志采集格式
vim /etc/rsyslog.conf
$template 格式名称,"日志采集格式"
$template westos,"%timegenerated% %FOMHOST-IP% %syslogtag% %msg%\n"
%timegenerated% 显示日志时间
%FOMHOST-IP% 显示主机ip
%syslogtag% 日志记录目标
%msg% 日志内容
[root@desktop19 mnt]# vim /etc/rsyslog.conf
51 $template LOGFMT,"%timegenerated% %FROMHOST-IP% %syslogtag% %msg%\n"
54 *.info;mail.none;authpriv.none;cron.none /var/log/messages;LOGFM
[root@desktop19 mnt]# cat /var/log/messages
Jan 13 00:54:32 172.25.254.219 root: test
Jan 13 00:54:32 172.25.254.219 root: test
Jan 13 00:54:34 172.25.254.219 rc.local: /etc/rc.d/rc.local: connect: Connection refused
Jan 13 00:54:34 172.25.254.219 rc.local: /etc/rc.d/rc.local: line 18: /dev/tcp/content.example.com/80: Connection refused
4、时间的同步
服务的名称:chronyd
![](https://i-blog.csdnimg.cn/blog_migrate/83b8c91193ff141a7b895d2738b2560d.png)
在服务端:
[root@server19 Desktop]# vim /etc/chrony.conf
22 allow 172.25.254.0/24 允许172.25.254网段的机子进行连接
29 local stratum 10 本机不同步任何主机的时间,本机作为时间源
[root@server19 Desktop]# systemctl restart chronyd.service 重启服务
[root@desktop19 mnt]# timedatectl set-timezone Asia/Shanghai 更改当前时区为东八区
在客户端:
[root@desktop19 mnt]# vim /etc/chrony.conf
3 server 172.25.254.219 iburst 本机同步172.25.254.219的时间
[root@desktop19 mnt]# systemctl restart chronyd.service
[root@desktop19 mnt]# timedatectl set-timezone Asia/Shanghai
[root@desktop19 mnt]# chronyc sources -v (查看已同步172.25.254.219的时间)
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.254.219 10 6 37 2 +23us[+1385ms] +/- 929us
5、日志
journalctl 查看日志的工具
journalctl -n 3 查看最近的3条日志
journalctl -p err 查看错误日志
--since: 查看从什么时间开始日志
--until: 查看从什么时间截至的日志
journalctl --since'2019-01-13 11:00' --until'2019-01-13 12:00' 查看从2019年1月13日11:00到2019年1月13日12:00的日志
如何使用systemd-journald保存系统日志:默认systemd-journald是不保存日志到硬盘的,那么关机以后再次开机只能看到开机之后的日志,为了要使我们电脑关机后重新开开机后还可以看到关机前的日志(只能看到此设置设定好了之后的时刻开始记录日志)
[root@desktop19 Desktop]# mkdir /var/log/journal
[root@desktop19 Desktop]# chgrp systemd-journal /var/log/journal/
[root@desktop19 Desktop]# chmod g+s /var/log/journal/
[root@desktop19 Desktop]# ps aux | grep systemd-journal
root 1250 0.0 0.1 40864 1520 ? Ss 16:22 0:00 /usr/lib/systemdsystemd-journald
root 1389 0.0 0.0 112640 940 pts/0 R+ 16:29 0:00 grep --color=auto systemd-journal
[root@desktop19 Desktop]# killall -1 systemd-journald
[root@desktop19 Desktop]# ps aux | grep systemd-journal
root 1402 0.1 0.5 42992 4992 ? Ss 16:29 0:00 /usr/lib/systemdsystemd-journald
root 1414 0.0 0.0 112640 936 pts/0 R+ 16:30 0:00 grep --color=auto systemd-journal
[root@desktop19 Desktop]# ls /var/log/journal/
946cb0e817ea4adb916183df8c4fc817
[root@desktop19 Desktop]# cd /var/log/journal/
[root@desktop19 journal]# ll
total 0
drwxr-sr-x 2 root systemd-journal 27 Jan 13 16:29 946cb0e817ea4adb916183df8c4fc817
[root@desktop19 journal]# reboot
6、系统时间的设定
date 时间的设定
查看当前日期的x天前或者x天后的时间
[root@desktop19 mnt]# date -d +3day
Wed Jan 16 15:26:52 CST 2019
[root@desktop19 mnt]# date
Sun Jan 13 15:26:55 CST 2019
[root@desktop19 mnt]# date -d -3day
Thu Jan 10 15:27:03 CST 2019
修改时间显示的格式:
[root@desktop19 mnt]# date +%Y-%m-%d
2019-01-13
[root@desktop19 mnt]# date +"%Y-%m-%d %H:%M:%S"
2019-01-13 15:31:41
[root@desktop19 mnt]# date +"%Y-%m-%d %H:%M:%S %A"
2019-01-13 15:33:37 Sunday
练习:
1、从1970年1月1日00:00:00到目前为止经历的秒数
[root@desktop19 mnt]# date +%s
1547363951
2、显示时间格式为PM/AM
[root@desktop19 mnt]# date +%r
03:22:53 PM
3、显示时间一年中的第几个星期
[root@desktop19 mnt]# date +%W
02
7、系统管理的时间
[root@desktop19 mnt]# timedatectl status 查看时间
Local time: Sun 2019-01-13 15:42:14 CST
Universal time: Sun 2019-01-13 07:42:14 UTC
RTC time: Sun 2019-01-13 07:42:15
Timezone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
[root@desktop19 mnt]# timedatectl list-timezones 列出时区
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Bamako
[root@desktop19 mnt]# timedatectl set-timezone Asia/Shanghai 设置时区为亚洲上海
[root@desktop19 mnt]# timedatectl status 查看时间
Local time: Sun 2019-01-13 15:45:28 CST
Universal time: Sun 2019-01-13 07:45:28 UTC
RTC time: Sun 2019-01-13 07:45:29
Timezone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
[root@desktop19 mnt]# timedatectl set-local-rtc 0 设定是否使用utc时间(使用utc时间进行时间校准)
[root@desktop19 mnt]# timedatectl status
Local time: Sun 2019-01-13 15:45:45 CST
Universal time: Sun 2019-01-13 07:45:45 UTC
RTC time: Sun 2019-01-13 07:45:46
Timezone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
[root@desktop19 mnt]# timedatectl set-local-rtc 1 使用本地时间进行校准
[root@desktop19 mnt]# timedatectl status
Local time: Sun 2019-01-13 15:45:57 CST
Universal time: Sun 2019-01-13 07:45:57 UTC
RTC time: Sun 2019-01-13 15:45:57
Timezone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: yes
DST active: n/a
Warning: The RTC is configured to maintain time in the local timezone. This
mode is not fully supported and will create various problems with time
zone changes and daylight saving adjustments. If at all possible use
RTC in UTC, by calling 'timedatectl set-local-rtc 0'.