一、时间服务器
简介
1.重要性:订单,日志,
2.RTC:硬件时间、系统时间:操作系统时间
3.设置时间的两个命令 date timedatectl:
date -s "%Y-%m-%d %H:%M:%S"
date -s "%Y:%m:%d"
date -s "%H:%M"
date 用来设置命令的时候 还能影响系统时间 无法修改 硬件时间
root@server \~\]# date -s "2011-11-11 11:11:11" 2011年 11月 11日 星期五 11:11:11 CST \[root@server \~\]# timedatectl Local time: 五 2011-11-11 11:11:18 CST Universal time: 五 2011-11-11 03:11:18 UTC RTC time: 六 2025-11-01 02:13:59 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: no NTP service: active RTC in local TZ: no # timedatectl 设置时间 # 如果直接set-time 会失败 原因是系统中的时间同步开启中 \[root@server \~\]# timedatectl set-time "2022-2-22 2:2:2" Failed to set time: Automatic time synchronization is enabled \[root@server \~\]# timedatectl Local time: 五 2011-11-11 11:15:59 CST Universal time: 五 2011-11-11 03:15:59 UTC RTC time: 六 2025-11-01 02:18:29 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: no NTP service: active RTC in local TZ: no # 所以首先关闭系统同步 \[root@server \~\]# timedatectl set-ntp 0/timedatectl set-ntp false/timedatectl set-ntp no \[root@server \~\]# timedatectl Local time: 五 2011-11-11 11:17:03 CST Universal time: 五 2011-11-11 03:17:03 UTC RTC time: 六 2025-11-01 02:19:29 Time zone: Asia/Shanghai (CST, +0800) System clock synchronized: no NTP service: inactive #系统同步服务已关闭 RTC in local TZ: no # 关闭后再去设置系统时间,他会修改系统时间及硬件时间 \[root@server \~\]# timedatectl set-time "2022-2-22 2:2:2" \[root@server \~\]# timedatectl Local time: 二 2022-02-22 02:02:08 CST Universal time: 一 2022-02-21 18:02:08 UTC RTC time: 一 2022-02-21 18:02:08 Time zone: Asia/Shanghai (CST, +0800) # 时间同步 当系统时间date命令或其他命令修改了,系统认为你要使用自己设定时间 # 所以他不会再去同步授时中心下发的时间,即使ntp service : active # 如果你不想使用自定义时间而是要授时中心下发的时间那么你需要先关闭同步服务在开启同步服务 \[root@server \~\]# timedatectl set-ntp 0 \[root@server \~\]# timedatectl set-ntp 1 **4.hwclock :可以用系统时间设置硬件时间,用硬件时间设置系统时间** hwclcok -s :使用硬件时间设置系统时间 hwclock -w :使用系统时间设置硬件时间 ### **二、NTP及chrony** NTP:首先是一个理论,它是关于时间同步的通信协议。 chrony:就是将该ntp通信协议具体实现出来的一套软件系统。 **安装** \[root@server \~\]# dnf install chrony -y **1.配置文件分析** /etc/chrony.conf 1 # Use public servers from the pool.ntp.org project. 2 # Please consider joining the pool (https://www.pool.ntp.org/jo in.html). 3 pool 2.centos.pool.ntp.org iburst # 这里就是配置授时服务器的地址 iburst 表示chronyd启动时会立即进行一次同步 4 5 # Use NTP servers from DHCP. # 通过dhcp服务获取时间同步服务(前提是支持该功能的dhcp) 6 sourcedir /run/chrony-dhcp 7 8 # Record the rate at which the system clock gains/losses time. 9 driftfile /var/lib/chrony/drift # 该文件记录系统时钟的误差 10 11 # Allow the system clock to be stepped in the first three updat es 12 # if its offset is larger than 1 second. 13 makestep 1.0 3 # 当跟授时服务器的误差超过1秒时,会进行3次快速同步 14 15 # Enable kernel synchronization of the real-time clock (RTC). 16 rtcsync 17 18 # Enable hardware timestamping on all interfaces that support i t. 19 #hwtimestamp \* 20 21 # Increase the minimum number of selectable sources required to adjust 22 # the system clock. 23 #minsources 2 24 # 当本机作为授时服务器时 以下字段生效 25 # Allow NTP client access from local network. 26 allow 0.0.0.0/0 27 28 # Serve time even if not synchronized to a time source. 29 local stratum 10 **2.实验** 修改时间,换源,同步 1. 修改时间 date -s "2011-11-11 1:1:1" # 2. 修改配置文件中授时服务器地址 vim /etc/chrony.conf ########### 3 pool ntp.aliyun.com iburst # 修改授时源 ########### # 3. 重启chronyd systemctl restart chronyd.service # 4.稍等片刻去观察时间是否进行了同步,如果同步成功说明 阿里云的收拾服务器设置成功 **3.一台server 一台client 用client去同步错误时间的server** # 服务端配置 # 1.创建时间服务器修改/etc/chrony.conf的26和29行 ######### 26 allow xxx.xxx.xxx.0/24 # 允许指定网段中主机访问 29 stratum 10 #表示当前时时钟源的第十层 ######### # 2. 重启服务 systemctl restart chronyd # 3.将自己的时间修改为一个错误的时间 date -s "xxxx-xx-xx xx:xx:xx" ------------------------------------------- # 客户端 # 1.修改配置文件将授时服务器指定为xxx.xxx.xxx.xxx 3 pool xxx.xxx.xxx.xxx iburst # 2.重启服务 systemctl restart chronyd **4.chronyc sources -v 查看授时服务器的状态** \[root@client \~\]# chronyc sources -v .-- Source mode '\^' = server, '=' = peer, '#' = local clock. / .- Source state '\*' = current best, '+' = combined, '-' = not combined, \| / 'x' = may be in error, '\~' = too variable, '?' = unusable. \|\| .- 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.100 2 6 377 7 -4859s\[ -4859s\] +/- 35ms # \^\~ :表示波动大的授时服务器 # \^\* :当前最好的源,主授时源 # \^= : 同级别的授时源 # \^+ : 备用授时源 # \^- : 不适合授时源 # \^x : 有错误的授时源 # \^? : 不可用的授时源 # \^# : 本地授时源 # poll 6 : 同步周期为2\^6=64 # 在2进制转为16进制中 4为二进制对应1为十六进制 # 在2进制转为8进制中 3为二进制对应1为八进制 # Reach 377: 377==\>011111111 表示最近8次的同步结果如果同步结果为1,如果同步失败结果0 # LastRx :上一次同步实在多少秒前