本文共 7039 字,大约阅读时间需要 23 分钟。
NTP通信协议原理
- 首先主机启动NTP。
- 客户端会向NTP服务器发送调整时间的message。
- 然后NTP server会送出当前的标准时间给client
- client接受来自server的时间后,会根据这个信息来调整自己的时间。这样就实现了网络对时。
NTP这个deamon采用了UDP 123端口。
语法为: restrict [ 客户端IP ] mask [ IP掩码 ] [参数]
其中IP地址也可以是default ,default 就是指所有的IP
参数有以下几个:
nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
notrust :拒絕沒有認證的用戶端
noquery :客户端不能使用ntpq,ntpc来查询ntp服务器,等于不提供校对时间服务
notrap :不提供trap远程登录功能,trap服务是一种远程时间日志服务
拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。
nopeer :用于阻止主机尝试与服务器对等
kod : 访问违规时发送 KoD 包,向不安全的访问者发送Kiss-Of-Death报文。
restrict -6 表示IPV6地址的权限设置。
server host [ key n ] [ version n ] [ prefer ] [ mode n ] [ minpoll n ] [ maxpoll n ] [ iburst ]
其中host是上层NTP服务器的IP地址或域名,随后所跟的参数解释如下所示:
◆ key: 表示所有发往服务器的报文包含有秘钥加密的认证信息,n是32位的整数,表示秘钥号。
◆ version: 表示发往上层服务器的报文使用的版本号,n默认是3,可以是1或者2。
◆ prefer: 优先使用。
◆ mode: 指定数据报文mode字段的值。
◆ minpoll: 指定与查询该服务器的最小时间间隔为2的n次方秒,n默认为6,范围为4-14。
◆ maxpoll: 指定与查询该服务器的最大时间间隔为2的n次方秒,n默认为10,范围为4-14。
◆ iburst: 当初始同步请求时,采用突发方式接连发送8个报文,时间间隔为2秒。
restrict default kod nomodify notrap nopeer noquery
拒绝所有ip4 的请求
# restrict -6 default kod nomodify notrap nopeer noquery #针对ipv6设置
# 允许本地所有操作
restrict 127.0.0.1
#restrict -6 ::1
# 允许的局域网络段或单独ip 某些权限
restrict 10.0.0.0 mask 255.0.0.0 nomodify motrap
restrict 192.168.0.0 mask 255.255.255.0 nomodify motrap
restrict 192.168.1.123 mask 255.255.255.255 nomodify motrap
# 使用上层的internet ntp服务器
restrict cn.pool.ntp.org
restrict 1.cn.poo.ntp.org
restrict 0.asia.pool.ntp.org
restrict 3.asia.pool.ntp.org
server cn.pool.ntp.org prefer
server 1.cn.poo.ntp.org
server 0.asia.pool.ntp.org
server 3.asia.pool.ntp.org
# 如果无法与上层ntp server通信以本地时间为标准时间
server 127.127.1.0
# local clock
fudge 127.127.1.0 stratum 10
# 计算本ntp server 与上层ntpserver的频率误差
driftfile
/var/lib/ntp/drift
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography.
keys
/etc/ntp/keys
#日志文件
logfile
/var/log/ntp
.log
配置/etc/sysconfig/ntpd文件
ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。
#允许BIOS与系统时间同步,也可以通过hwclock -w 命令
SYNC_HWCLOCK=yes
启动ntpd 服务 # service ntpd start
# 查看ntp的端口,应该看到123端口
#netstat
-unlnp
# 查看ntp服务器有无和上层连通
#ntpstat
synchronised to NTP server () at stratum 2
time
correct to within 74 ms
polling server every 128 s
# 注意:此命令出现上述synchronised结果比较慢,我的用了大概5分钟。
# ntpstat
unsynchronised
time server re-starting
polling server every 64 s
连接并同步后:
synchronised to NTP server (202.112.10.36) at stratum 3
time correct to within 275 ms
polling server every 256 s
ntptrace -n 127.0.0.1
127.0.0.1: stratum 3, offset -0.001095, synch distance 0.532610
116.193.83.174: timed out, nothing received
查看ntp服务器与上层ntp服务器的状态:ntpq
#ntpq -p
# 其中:
# remote - 本机和上层ntp的ip或主机名,“+”有连线可做候选,“*”正在使用的
# refid - 更上一层的ntp地址
# st - stratum的 级别
# when - 多少秒前曾经同步过时间
# poll - 下次更新在多少秒后
# reach - 已经向上层ntp服务器要求更新的次数
# delay - 网络延迟
# offset - 时间补偿
# jitter - 系统时间与bios时间差
# ntpq -p remote refid st t when poll reach delay offset jitter ============================================================================== *time4.aliyun.co 10.137.38.86 2 u 111 128 377 27.480 -5.995 1.852
2.配置NTP客户端: 在所有客户端上vim /etc/ntp.conf,添加: server x.x.x.x
x.x.x.x 为上面配置的ntp服务器地址
注意:当server与client之间的时间误差过大时(可能是1000秒),处于对修改时间可能对系统和应用带来不可预知的问题,NTP将停止时间同步! 所以如果发现NTP启动之后时间并不进行同步时,应该考虑到可能是时间差过大引起的, 此时需要先手动进行时间同步 ntpdate !
转载地址:https://blog.csdn.net/zonghua521/article/details/78239212 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!