寇油
2025-9-24 12:42:55
介绍
- NTP属于运用层协议(依据UDP传输,运用的端口号为123),用来同步网络中分布式时间服务器和客户端之间的时间,使网络中的设备供应依据一起时间的运用成为可能。
- 时间服务器和客户端是相对的。供应时间规范的设备为时间服务器,接收时间服务的设备为时间客户端。
- 设备运转NTP之后,通过沟通NTP报文,既可以作为时间服务器供应时间规范,又可以作为时间客户端接收时间服务。
安装ntp服务
服务器和客户端都安装- yum install -y ntp
- yum install -y ntpdate
复制代码 配置ntp服务端
编辑服务端配置文件
- vim /etc/ntp.conf
- # 和上层NTP服务器频率误差的记录文件
- driftfile /var/lib/ntp/drift
- # 对于默认的client拒绝所有操作
- restrict default nomodify notrap nopeer noquery
- # 允许本机地址的一切操作
- restrict 127.0.0.1
- restrict ::1
- # 指定可以和NTP通信的IP或者网段
- restrict 10.0.0.0 mask 255.255.255.0 nomodify notrap
- # 远程的同步时间服务器地址(需要同步外网时间,必改)
- # prefer表示优先同步的主机
- # `iburst` 选项表示在初始同步时发送多个请求,加快同步速度
- server cn.pool.ntp.org prefer
- server ntp1.aliyun.com iburst
- server ntp2.aliyun.com iburst
- server ntp3.aliyun.com iburst
- server ntp4.aliyun.com iburst
- server ntp5.aliyun.com iburst
- # 远程时间服务器不可用时,以本地时间作为服务时间
- server 127.127.1.0
- # stratum指定层数,越小,离时间源,越近。 层级1为国际时间源服务器...
- fudge 127.127.1.0 stratum 10
- # 仅允许上层服务器为本机提供时间同步服务,而禁止它执行任何可能影响本机 NTP 服务配置或获取状态信息的操作,属于对外部公共服务器的安全限制最佳实践。
- restrict cn.pool.ntp.org nomodify notrap noquery
- restrict ntp1.aliyun.com nomodify notrap noquery
- restrict ntp2.aliyun.com nomodify notrap noquery
- restrict ntp3.aliyun.com nomodify notrap noquery
- restrict ntp4.aliyun.com nomodify notrap noquery
- restrict ntp5.aliyun.com nomodify notrap noquery
- # 引入 NTP 服务的加密密码文件
- includefile /etc/ntp/crypto/pw
- # 秘钥文件
- keys /etc/ntp/keys
- # 指定日志文件
- logfile /var/log/ntp.log
- # 指定日志级别,info、all、event三种可以选择
- logconfig all
- # 禁用 NTP 服务的 监控功能
- disable monitor
- # 总结一下,restrict用来设置访问权限,server用来设置上层时间服务器,driftfile用来设置保存漂移时间的文件。
复制代码 启动ntp服务端
在启动NTP服务前,先对提供服务的这台主机手动的校正一次时间。因为:
- 如果这台主机时间相差太大,ntp时间同步是渐近同步的,会需要很久才会同步一致。
- 启动服务器,端口会被服务端占用,就不能手动同步时间了
- ntpdate ntp.aliyun.com
- systemctl enable ntpd
- systemctl start ntpd
- systemctl status ntpd
- netstat -lntup|grep 123
复制代码 验证是否正在同步
- ntpq -p
- remote refid st t when poll reach delay offset jitter
- ==============================================================================
- time.cloudflare 10.70.8.4 3 u 43 64 3 224.168 46.850 44.197
- +118.31.40.99 100.100.61.92 2 u 42 64 3 13.740 -36.312 20.364
- *203.107.6.88 100.107.25.114 2 u 39 64 3 21.035 -33.513 11.831
- LOCAL(0) .LOCL. 10 l 43 64 3 0.000 0.000 0.000
- # 指令“ntpq -p”可以列出目前我们的NTP与相关的上层NTP的状态,以上的几个字段的意义如下:
- * 当前同步的源(最优时间源)
- + 候选时间源(符合条件,可作为备用同步源)
- - 被排除的源(因误差过大或不可靠被拒绝)
- # 备用时间源(当其他源不可用时可能被选择)
- o PPS源(通过脉冲信号同步的高精度源,如GPS)
- x 虚假源(时钟差异过大,可能被攻击或配置错误)
- . 未使用的源(未达到候选标准)
- (空格)无效或不可达的源
- remote 远程NTP服务器的IP或主机名
- refid 服务器同步的上层时间源(如GPS、原子钟或其他NTP服务器)
- st Stratum(层级),表示时间源的精度等级(0-15,数值越小精度越高)
- t 类型:u=单播,b=广播,l=本地,s=对称节点,A=多播,B=广播
- when 上次成功同步后经过的时间(分钟)
- poll 轮询间隔(秒),表示与服务器同步的频率
- reach 8位八进制数,表示最近8次同步的成功/失败状态(377=全部成功)
- delay 网络延迟(毫秒),从本地到远程服务器的往返时间
- offset 时间偏移量(毫秒),本地时钟与远程服务器的时间差异
- jitter 时间偏移的波动值(毫秒),值越小表示时间越稳定
复制代码复制代码 配置系统时间与硬件时间同步
ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。
配置ntp客户端
- # 首先需要使用ntpdate同步
- ntpdate 10.0.0.50
- # 配置ntp服务
- vim /etc/ntp.conf
- server 10.0.0.50 iburst #指定上层NTP服务器
- # 重启
- systemctl restart ntpd
- systemctl enable ntpd
- systemctl status ntpd
复制代码 之后使用ntpstat和ntpq -p检查是否同步即可
来源:程序园用户自行投稿发布,如果侵权,请联系站长删除
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |
|
|
|
|
|
相关推荐
|
|
|