NTP服务解析:
chronyd 开源时间同步服务器:
安装包:
chrony
1、定义:
chrony 是网络时间协议(NTP)的通用软件,使用 UDP 323 端口通信,根据 chrony.conf 文件的不同配置分成服务端和客户端。
2、配置:
vi /etc/chrony.conf
server 1.1.1.1 iburst
allow 192.168.0.0/16
local stratum 10
重启服务:
systemctl restart chronyd
查看时间同步:
chronyc sources -v
客户端设置:
vi /etc/chrony.conf
server 192.168.200.3 iburst
其他查看命令:
chronyc sources ;
chronyc tracking ;
chronyc sources -v ;
查看时间源状态:
chronyc sourcestats -v
查看时间源是否在线:
chronyc activity -v ;
3、网络策略:
chrony 使用323 udp 端口 , 需要防火墙提前开通;
chronyc -a makestep 手动同步时钟
时间命令:
timedatectl 命令;
ntp时间服务同步:
yum install -y ntp 服务端,客户端;
使用端口 udp 123
ntp 平滑同步;
ntpdate 立即同步;
服务端配置, /etc/ntp.conf
记录和上级时间服务器的时间差异
driftfile /var/lib/ntp/drift
ntp 日志
logfile /var/log/ntp.log
日志级别 all event info
logconfig all
设置默认策略,允许同步时间,不允许修改
restrict default nomodify notrap nopeer noquery
允许本机地址的一切操作,-6 为 IPV6
restrict 127.0.0.1
restrict -6 ::1
允许网段内客户端连接此服务器同步时间,但是拒绝让他们修改服务器上的时间
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
https://www.ntppool.org/zone/cn
利用 server 设定上层 NTP 服务器,可设置多个。prefer 表示优先
server s1b.time.edu.cn prefer
在 /ntp.conf 中定义的 server 都不可用时,将使用 local 时间作为 ntp 服务提供给 ntp 客户端。建议配置,否则 ntp 服务器无法与公网 ntp 服务器同步时,其客户端也会无法同步
server 127.127.1.0
fudge 127.127.1.0 stratum 10
主要配置:
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
server 1.1.1.1
3、重启ntp服务:
systemctl restart ntp
4、常用命令:
上层 ntp 的状态
ntpq -p
ntp 同步状态
ntpstat
向 NTP 服务器同步时间,需关闭 ntpd 服务,-u 指定使用无特权的端口发送数据包 -d 调试
ntpdate -u s1a.time.edu.cn
ntpdate -d 1.1.1.1
系统时间与硬件时间:
hwclock -w 设置硬件时间,系统时间写入硬件时间;
hwclock -s 设置系统时间,硬件时间写入系统时间。
增加定时任务,单次同步时间:
0 12 * * * ntpdate 1.1.1.1 && hwclock -w
时钟的跃变,可能对程序造成严重问题。