ntp时间同步解析

  其他常见问题
内容纲要

概要描述


网络时间协议,英文名称:Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议。

详细说明


NTP时间同步日志解析:

1. Manager不管理NTP配置

Tue Oct 15 10:24:17 CST 2019 [Manager] Starting task local part ...
##Manager数据库transwarp_manager/system_settings  ntp.synchronization.strategy修改为unmanaged
ntp.synchronization.strategy is set as unmanaged, skip config ntp and sync time
Tue Oct 15 10:24:17 CST 2019 [Manager] Task local part ended

2. 内部NTP服务器作为独立NTP服务器,其它节点向内部NTP服务器同步

Tue Oct 15 09:59:03 CST 2019 [Manager] Starting task local part ...
##Manager数据库transwarp_manager/system_settings  ntp.synchronization.strategy修改为internal_only
ntp.synchronization.strategy is set as internal_only
##停止NTP服务
Tue Oct 15 09:59:03 CST 2019 [Agent] execute command: systemctl stop ntpd
Tue Oct 15 09:59:03 CST 2019 [Agent] exit status: 0
Tue Oct 15 09:59:03 CST 2019 [Agent] stdout: 
##将ntp.conf配置文件中以fudge.开头或者server.开头的行删除
Tue Oct 15 09:59:03 CST 2019 [Agent] execute command: sed -i '/^fudge.*/d' /etc/ntp.conf && sed -i '/^server.*/d' /etc/ntp.conf 
Tue Oct 15 09:59:03 CST 2019 [Agent] exit status: 0
Tue Oct 15 09:59:03 CST 2019 [Agent] stdout: 
##将server 127.127.1.0 fudge  127.127.1.0 stratum 10 追加到/etc/ntp.conf文件的尾部
Tue Oct 15 09:59:03 CST 2019 [Agent] execute command: echo $'server 127.127.1.0
fudge  127.127.1.0 stratum 10' >> /etc/ntp.conf
Tue Oct 15 09:59:03 CST 2019 [Agent] exit status: 0
Tue Oct 15 09:59:03 CST 2019 [Agent] stdout: 
##启动ntp服务
Tue Oct 15 09:59:03 CST 2019 [Agent] execute command: systemctl start ntpd
Tue Oct 15 09:59:03 CST 2019 [Agent] exit status: 0
Tue Oct 15 09:59:03 CST 2019 [Agent] stdout: 
##配置开机自启(可通过 systemctl list-dependencies|grep ntpd检查是否开机自启)
Tue Oct 15 09:59:03 CST 2019 [Agent] execute command: systemctl enable ntpd
Tue Oct 15 09:59:05 CST 2019 [Agent] exit status: 0
Tue Oct 15 09:59:05 CST 2019 [Agent] stdout: 
Tue Oct 15 09:59:05 CST 2019 [Manager] Task local part ended

-----------------------

Tue Oct 15 09:59:05 CST 2019 [Manager] Starting task local part ...
##Manager数据库transwarp_manager/system_settings  ntp.synchronization.strategy修改为internal_only
ntp.synchronization.strategy is set as internal_only
##停止NTP服务
Tue Oct 15 09:59:05 CST 2019 [Agent] execute command: systemctl stop ntpd
Tue Oct 15 09:59:05 CST 2019 [Agent] exit status: 0
Tue Oct 15 09:59:05 CST 2019 [Agent] stdout: 
##将ntp.conf配置文件中以fudge.开头或者server.开头的行删除
Tue Oct 15 09:59:05 CST 2019 [Agent] execute command: sed -i '/^fudge.*/d' /etc/ntp.conf && sed -i '/^server.*/d' /etc/ntp.conf 
Tue Oct 15 09:59:05 CST 2019 [Agent] exit status: 0
Tue Oct 15 09:59:05 CST 2019 [Agent] stdout: 
##将server 172.22.23.1 追加到/etc/ntp.conf文件的尾部
Tue Oct 15 09:59:05 CST 2019 [Agent] execute command: echo server 172.22.23.1 >> /etc/ntp.conf
Tue Oct 15 09:59:05 CST 2019 [Agent] exit status: 0
Tue Oct 15 09:59:05 CST 2019 [Agent] stdout: 
##指定格式输出当前时间
Tue Oct 15 09:59:05 CST 2019 [Manager] execute command: date "+%Y-%m-%d %H:%M:%S"
Tue Oct 15 09:59:05 CST 2019 [Manager] exit status: 0
Tue Oct 15 09:59:05 CST 2019 [Manager] stdout: 2019-10-15 09:59:05
##系统时间设置,同时修改硬件时间和系统时间同步
Tue Oct 15 09:59:05 CST 2019 [Agent] execute command: date -s "2019-10-15 09:59:05" && hwclock --systohc
Tue Oct 15 09:59:05 CST 2019 [Agent] exit status: 0
Tue Oct 15 09:59:05 CST 2019 [Agent] stdout: Tue Oct 15 09:59:05 CST 2019
##启动ntp服务
Tue Oct 15 09:59:05 CST 2019 [Agent] execute command: systemctl start ntpd
Tue Oct 15 09:59:06 CST 2019 [Agent] exit status: 0
Tue Oct 15 09:59:06 CST 2019 [Agent] stdout: 
##配置开机自启(可通过 systemctl list-dependencies|grep ntpd检查是否开机自启)
Tue Oct 15 09:59:06 CST 2019 [Agent] execute command: systemctl enable ntpd
Tue Oct 15 09:59:06 CST 2019 [Agent] exit status: 0
Tue Oct 15 09:59:06 CST 2019 [Agent] stdout: 
Tue Oct 15 09:59:06 CST 2019 [Manager] Task local part ended

3. 所有节点向外部NTP同步

Tue Oct 15 10:08:42 CST 2019 [Manager] Starting task local part ...
##Manager数据库transwarp_manager/system_settings  ntp.synchronization.strategy修改为 external_only
ntp.synchronization.strategy is set as external_only
##停止NTP服务
Tue Oct 15 10:08:42 CST 2019 [Agent] execute command: systemctl stop ntpd
Tue Oct 15 10:08:42 CST 2019 [Agent] exit status: 0
Tue Oct 15 10:08:42 CST 2019 [Agent] stdout: 
##将ntp.conf配置文件中以fudge.开头或者server.开头的行删除
Tue Oct 15 10:08:42 CST 2019 [Agent] execute command: sed -i '/^fudge.*/d' /etc/ntp.conf && sed -i '/^server.*/d' /etc/ntp.conf 
Tue Oct 15 10:08:42 CST 2019 [Agent] exit status: 0
Tue Oct 15 10:08:42 CST 2019 [Agent] stdout: 
##将server 0.cn.pool.ntp.org 追加到/etc/ntp.conf文件的尾部
Tue Oct 15 10:08:42 CST 2019 [Agent] execute command: echo server 0.cn.pool.ntp.org >> /etc/ntp.conf
Tue Oct 15 10:08:42 CST 2019 [Agent] exit status: 0
Tue Oct 15 10:08:42 CST 2019 [Agent] stdout: 
##执行ntp同步
Tue Oct 15 10:08:42 CST 2019 [Agent] execute command: ntpdate 0.cn.pool.ntp.org
Tue Oct 15 10:08:49 CST 2019 [Agent] exit status: 0
Tue Oct 15 10:08:49 CST 2019 [Agent] stdout: 15 Oct 10:08:49 ntpdate[17444]: adjust time server 120.25.115.20 offset -0.010340 sec
##启动ntp服务
Tue Oct 15 10:08:49 CST 2019 [Agent] execute command: systemctl start ntpd
Tue Oct 15 10:08:49 CST 2019 [Agent] exit status: 0
Tue Oct 15 10:08:49 CST 2019 [Agent] stdout: 
##配置开机自启(可通过 systemctl list-dependencies|grep ntpd检查是否开机自启)
Tue Oct 15 10:08:49 CST 2019 [Agent] execute command: systemctl enable ntpd
Tue Oct 15 10:08:50 CST 2019 [Agent] exit status: 0
Tue Oct 15 10:08:50 CST 2019 [Agent] stdout: 
Tue Oct 15 10:08:50 CST 2019 [Manager] Task local part ended

4. 内部NTP服务器向外部NTP同步,其它节点向内部NTP服务器同步

Tue Oct 15 10:11:24 CST 2019 [Manager] Starting task local part ...
##Manager数据库transwarp_manager/system_settings  ntp.synchronization.strategy修改为 external_and_internal
ntp.synchronization.strategy is set as external_and_internal
##停止NTP服务
Tue Oct 15 10:11:24 CST 2019 [Agent] execute command: systemctl stop ntpd
Tue Oct 15 10:11:24 CST 2019 [Agent] exit status: 0
Tue Oct 15 10:11:24 CST 2019 [Agent] stdout: 
##将ntp.conf配置文件中以fudge.开头或者server.开头的行删除
Tue Oct 15 10:11:24 CST 2019 [Agent] execute command: sed -i '/^fudge.*/d' /etc/ntp.conf && sed -i '/^server.*/d' /etc/ntp.conf 
Tue Oct 15 10:11:24 CST 2019 [Agent] exit status: 0
Tue Oct 15 10:11:24 CST 2019 [Agent] stdout: 
##将server 0.cn.pool.ntp.org 追加到/etc/ntp.conf文件的尾部
Tue Oct 15 10:11:24 CST 2019 [Agent] execute command: echo server 0.cn.pool.ntp.org >> /etc/ntp.conf
Tue Oct 15 10:11:24 CST 2019 [Agent] exit status: 0
Tue Oct 15 10:11:24 CST 2019 [Agent] stdout: 
##执行ntp同步
Tue Oct 15 10:11:24 CST 2019 [Agent] execute command: ntpdate 0.cn.pool.ntp.org
Tue Oct 15 10:11:32 CST 2019 [Agent] exit status: 0
Tue Oct 15 10:11:32 CST 2019 [Agent] stdout: 15 Oct 10:11:32 ntpdate[26821]: adjust time server 84.16.67.12 offset 0.013110 sec
##启动ntp服务
Tue Oct 15 10:11:32 CST 2019 [Agent] execute command: systemctl start ntpd
Tue Oct 15 10:11:32 CST 2019 [Agent] exit status: 0
Tue Oct 15 10:11:32 CST 2019 [Agent] stdout: 
##配置开机自启(可通过 systemctl list-dependencies|grep ntpd检查是否开机自启)
Tue Oct 15 10:11:32 CST 2019 [Agent] execute command: systemctl enable ntpd
Tue Oct 15 10:11:32 CST 2019 [Agent] exit status: 0
Tue Oct 15 10:11:32 CST 2019 [Agent] stdout: 
Tue Oct 15 10:11:32 CST 2019 [Manager] Task local part ended

常用命令


1. ntptat

  • 查看ntp服务器有无和上层ntp连通
[root@kevin1/var/log/transwarp-manager/master]> ntpstat
synchronised to local net at stratum 11 
   time correct to within 7948 ms
   polling server every 64 s

2. ntpq -p

  • 列出目前我们的NTP与相关的上层NTP的状态
[root@kevin1/var/log/transwarp-manager/master]> ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*LOCAL(0)        .LOCL.          10 l   21   64    1    0.000    0.000   0.000

3. watch ntpq -p

Every 2.0s: ntpq -p                                                                                                                         Wed Oct 16 09:25:55 2019

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 kevin1          LOCAL(0)        11 u   32   64    3    0.137  970.277   0.002
  • remote:本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
  • when:几秒前曾做过时间同步更新的操作
  • poll:下次更新在多少秒后
  • offset: 这是个最关键的值, 它告诉了我们本地机和服务器之间的时间差别. offset越接近于0,我们就和服务器的时间越接近
  • reach:已经向上层NTP服务器要求更新的次数;ntp客户端和ntp服务器同步需要一个时间段,这个过程大概3分钟,
    在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。
    reach这个值,在启动ntp server之后,会从0不断累加,当增加到7的时候,0到7是3次变更(1,3,7),每一次是poll值的秒数,也就是64秒*3=3分钟的时间

常见报错


1.ntpdate命令执行报错:the NTP socket is in use, exiting

  • 原因:一般是因为系统NTP服务正在运行,导致123端口被ntpd占用,可以通过service ntpd status或者 ps aux | grep ntpd 命令加以验证
  • 解决办法:可使用命令 service ntpd stop 终止ntpd服务。实在不行可强行kill掉ntpd对应的pid。关闭ntpd服务之后就可以继续执行命令了

2.no server suitable for synchronization found

  • 在ntp服务重启之后,客户机需要等几分钟再与服务器进行同步,期间会报这个错误,可以通过watch ntpq -p命令监测同步进程,等remote值前面出现*号之后代表同步成功
  • 网上有说是防火墙的原因,对于TDH默认关闭状态 systemctl status firewalld ,如果系统防火墙启动的情况下必须开放udp协议下ntp服务的123端口
    [root@kevin1~]> /sbin/iptables -I INPUT -p udp --dport 123 -j ACCEPT
    [root@kevin1~]> iptables -L -n -v

3.ntpstat查看报 unsynchronised polling server every 64

  • ntp服务器配置完毕后,需要等待3分钟才能与/etc/ntp.conf中配置的标准时间进行同步

这篇文章对您有帮助吗?

平均评分 5 / 5. 次数: 1

尚无评价,您可以第一个评哦!

非常抱歉,这篇文章对您没有帮助.

烦请您告诉我们您的建议与意见,以便我们改进,谢谢您。