linux系统ntp时间同步
概要
linux系统时间同步有ntp和chrony两种不同实现方式。
两者相比chrony性能更优,如果系统支持,那么能够使用chrony尽量使用它。
- chrony 具有更精确的时间准确性,精度更高,同步时间的速度更快,从而最大程度的减少时间和频率误差
- chrony 可以随机间歇性校准时间,而 NTP 需要定期轮询才能正常工作,因此 chrony 具备间歇性网络连接的系统仍然可以快速同步时钟
- chrony 对时间的校准是连续的,通过从而最大程度的减少时间和频率误差;而 ntp 需要搭配 crontab 来定时调整时间,会出现间断
- chrony 能够快速适应时钟速率的突然变化(例如,晶体振荡器的温度变化)。而 NTP可能需要很长时间才能再次稳定下来
- chrony 能够在网络拥堵的情况很好地执行
- chrony 占更小的内存,更加的轻量,而且只在必要时唤醒 CPU
ntp比crontab优点是适用性更广,移植的操作系统更多,包含大量的硬件时钟驱动程序。
安装
yum install ntp -y
配置
配置文件:/etc/ntp.conf
driftfile /var/lib/ntp/drift #系统时间与BIOS事件的偏差记录 restrict default kod nomodify notrap nopeer noquery restrict -6 default kod nomodify notrap nopeer noquery # 限制所有的client连接此NTP服务器进行同步、查询等相关操作 restrict 127.0.0.1 restrict -6 ::1 # 本机localhost对NTP服务拥有全部的权限 # Hosts on local network are less restricted. #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # Use public servers from the pool.ntp.org project. # Please consider joining the pool (http://www.pool.ntp.org/join.html). server 0.centos.pool.ntp.org iburst server 1.centos.pool.ntp.org iburst server 2.centos.pool.ntp.org iburst server 3.centos.pool.ntp.org iburst #broadcast 192.168.1.255 autokey # broadcast server #broadcastclient # broadcast client #broadcast 224.0.1.1 autokey # multicast server #multicastclient 224.0.1.1 # multicast client #manycastserver 239.255.254.254 # manycast server #manycastclient 239.255.254.254 autokey # manycast client # Enable public key cryptography. #crypto includefile /etc/ntp/crypto/pw # Key file containing the keys and key identifiers used when operating # with symmetric key cryptography. keys /etc/ntp/keys
server 192.167.121.2 #服务器端ip地址
restrict 192.167.121.2 nomodify notrap noquery #客户端不能更改服务端的时间参数,不提供trap远程登录,不提供时间查询
#restrict 控制相关权限。语法为: restrict IP地址 mask 子网掩码 参数。参数有:
ignore :关闭所有的 NTP 联机服务
kod : 访问违规时发送 KoD 包。
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网
noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器
notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。
nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟
restrict -6 表示IPV6地址的权限设置。
fudge 127.127.1.0 stratum 0 # 这行是时间服务器的层次。设为0则为顶级,如果要向别的NTP服务器更新时间,请不要把它设为0
restrict default ignore # 设置默认策略为允许任何主机进行时间同步
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap # 允许192.168.1.1-192.168.1.255主机与NTP服务器进行时间同步,但不允许更改服务器端参数;同时NTP服务器拒绝为匹配的主机提供trap远程登录及日志记录。
查看
ntpq -p:查看ntp服务器与上层ntp服务器状态
remote:上级时钟源,refid:上上级时钟源
*表示响应的NTP服务器和最精确的服务器,即提供时钟同步的主用服务器
+表示响应这个请求的NTP服务器,即备用服务器
st:正在响应请求的NTP服务器(即remote远程服务器)的级别,层数(Stratum)
poll:下次更新时间,初始状态下为64s,过一段时间后会增大,当when = poll时会触发更新
reach:已经向上级ntp服务器请求的次数
delay:网络延迟
offset:主机通过NTP时钟同步与所同步时间源的时间偏移量,单位为毫秒
操作
service ntpd status:查看ntpd服务状态
service ntpd stop:停止ntpd服务
service ntpd start:开启ntpd服务
systemctl enable ntpd:设置服务自启动
ntpdate:立即强制调整时间(激进调整),ntpdate命令会设置本地日期和时间。不会考虑其他程序是否会阵痛,可能会对程序造成影响,
ntpdate -u 强制同步上级时钟源IP
-u:指定使用无特权的端口发送数据包,可以越过防火墙与主机同步
-d:debug模式,不会真正同步,可以测试和上级NTP时钟源的连通性,还可以获得和上级时钟源的时差
查看本地时间
date –s "2021-03-10 00:00:00":修改本地时间
一键命令
echo "server 192.167.121.2">> /etc/ntp.conf echo "restrict 192.167.121.2 nomodify notrap noquery" >> /etc/ntp.conf ntpdate -u 192.167.121.2 hwclock -w systemctl restart ntpd systemctl enable ntpd ntpq -p