博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
linux 时钟同步
阅读量:4054 次
发布时间:2019-05-25

本文共 4145 字,大约阅读时间需要 13 分钟。

Network Time Protocol(,网络时间协议)用于同步它所有客户端时钟的服务。NTP服务器将本地系统的时钟与一个公共的NTP服务器同步然后作为时间主机提供服务,使本地网络的所有客户端能同步时钟。

    同步时钟最大的好处就是相关系统上文件中的数据,如果网络中使用中央日志主机集中管理日志,得到的日志结果就更能反映真实情况。在同步了时钟的网络中,集中式的性能监控、服务监控系统能实时的反应系统信息,系统管理员可以快速的检测和解决系统错误。

   安装配置NTP服务

    下面将介绍NTP服务器的简单:

   第一步,安装NTP服务

    一般的发行版都会带ntp软件包,如果你的系统中还没有安装,就使用rpm命令安装此包

,以下以centos系统为例配置一台时间服务器:

 查找当前系统是否已安装ntp

 [root@localhost ~]# rpm -qa | grep ntp
 chkfontpath-1.10.1-1.1
 ntp-4.2.2p1-8.el5.centos.1       (这个就是已经安装的RPM包)

 如果没有安装,可用下例命令安装:

 [root@localhost ~]# rpm -ivh ntp-4.2.2p1-8.el5.centos.1.rpm

   第二步,配置NTP服务器

 
   NTP服务器配置如下:

    编辑配置文件/etc/ntp.conf   

restrict default kod nomodify notrap nopeer noquery

restrict -6 default kod nomodify notrap nopeer noquery

restrict 127.0.0.1

restrict -6 ::1

restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

server 192.168.146.225

server 0.centos.pool.ntp.org
server 1.centos.pool.ntp.org
server 2.centos.pool.ntp.org

server  127.127.1.0     # local clock
fudge   127.127.1.0 stratum 10

   配置文件说明如下:

    第一行restrict、default定义默认访问规则,nomodify禁止远程主机修改本地服务器配置,notrap拒绝特殊的ntpdq捕获消息,noquery拒绝btodq/ntpdc查询(这里的查询是服务器本身状态查询)。

    restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

    这句是手动增加的,意思是从192.168.1.1-192.168.1.254的服务器都可以使用我们的NTP服务器来同步时间。

    server 192.168.146.225

    这句也是手动增加的,指明局域网中作为NTP服务器的IP;

  配置文件的最后两行作用是当服务器与公用的时间服务器失去联系时以本地时间为客户端提供时间服务。

   端口
  ntp使用udp协议,记得开放其123端口。

  启动NTPD

    为了使NTP服务可以在系统引导的时候自动启动,执行:

    #chkconfig ntpd on
    启动ntpd:
  service ntpd start

   NTP客户端配置:

    在客户端手动执行“ntpdate 服务器IP”来同步时间;

    另可以使用crond来定时同步时间:

    以root身份运行周期性任务:

  [root@supersun root]# crontab -e

  添加以下内容,每15分钟更新一下时间:

  */15空格*空格 *空格 *空格 *空格 ntpdate空格 服务器IP

 

    此处的ntpdate命令包含在ntp软件包中,记得确认系统中是否已安装。

本人自己设置的ntp时钟同步

通过crontab -e命令将如下命令周期性执行

10 3 * * 3 /usr/sbin/ntpdate 219.233.237.2 && /sbin/hwclock -w
ps:每周三的3:10分 与时间服务器219.233.237.2同步时间,并将当前时间写入BIOS中

   第三步,检查时间服务器是否正确同步

    使用下面的命令检查时间服务器同步的状态:

    #ntpq -p
   
    一个可以证明同步有问题的证据是:所有远程服务器的jitter值是4000并且delay和reach的值是0。

可能的原因有:

    有防火墙阻断了与server之间的通讯,即123端口是否正常开放;

    此外每次重启NTP服务器之后大约要3-5分钟客户端才能与server建立正常的通讯连接,否则你在客户端执行“ntpdate 服务器ip”的时候将返回:

    27 Jun 10:20:17 ntpdate[21920]: no server suitable for synchronization found

 

 

当用ntpdate -d 来查询时会发现导致 no server suitable for synchronization found 的错误的信息有以下2个: 

错误1.Server dropped: Strata too high

在ntp客户端运行ntpdate serverIP,出现no server suitable for synchronization found的错误。

在ntp客户端用ntpdate –d serverIP查看,发现有“Server dropped: strata too high”的错误,并且显示“stratum 16”。而正常情况下stratum这个值得范围是“0~15”。

这是因为NTP server还没有和其自身或者它的server同步上。

以下的定义是让NTP Server和其自身保持同步,如果在/ntp.conf中定义的server都不可用时,将使用local时间作为ntp服务提供给ntp客户端。

server 127.127.1.0 fudge

127.127.1.0 stratum 8

在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。

那么如何知道何时ntp server完成了和自身同步的过程呢?

在ntp server上使用命令:

# watch ntpq -p

出现画面:

Every 2.0s: ntpq -p                                                                                                             Thu Jul 10 02:28:32 2008

     remote           refid      st t when poll reach   delay   offset jitter

==============================================================================

 192.168.30.22   LOCAL(0)         8 u   22   64    1    2.113 179133.   0.001

 LOCAL(0)        LOCAL(0)        10 l   21   64    1    0.000   0.000  0.001

 

注意LOCAL的这个就是与自身同步的ntp server。

注意reach这个值,在启动ntp server服务后,这个值就从0开始不断增加,当增加到17的时候,从0到17是5次的变更,每一次是poll的值的秒数,是64秒*5=320秒的时间。

如果之后从ntp客户端同步ntp server还失败的话,用ntpdate –d来查询详细错误信息,再做判断。

错误2.Server dropped: no data
从客户端执行netdate –d时有错误信息如下:

transmit(192.168.30.22)

transmit(192.168.30.22)

transmit(192.168.30.22)

transmit(192.168.30.22)

transmit(192.168.30.22)

192.168.30.22: Server dropped: no data

server 192.168.30.22, port 123

.....

28 Jul 17:42:24 ntpdate[14148]: no server suitable for synchronization found
出现这个问题的原因可能有2:

1。检查ntp的版本,如果你使用的是ntp4.2(包括4.2)之后的版本,在restrict的定义中使用了notrust的话,会导致以上错误。

使用以下命令检查ntp的版本:

# ntpq -c version

下面是来自ntp官方网站的说明:

The behavior of notrust changed between versions 4.1 and 4.2.

In 4.1 (and earlier) notrust meant "Don't trust this host/subnet for time".

In 4.2 (and later) notrust means "Ignore all NTP packets that are not cryptographically authenticated." This forces remote time servers to authenticate themselves to your (client) ntpd

解决:

把notrust去掉。

2。检查ntp server的防火墙。可能是server的防火墙屏蔽了upd 123端口。

可以用命令

#service iptables stop

来关掉iptables服务后再尝试从ntp客户端的同步,如果成功,证明是防火墙的问题,需要更改iptables的设置。

转载地址:http://mdlci.baihongyu.com/

你可能感兴趣的文章
Linux 内核api man 手册安装
查看>>
Linux 内核宏 container_of
查看>>
Ubuntu 安装bcompare
查看>>
电阻屏较准
查看>>
imx6 内核停止启动
查看>>
RTL8188EUS Anaroid M Porting
查看>>
omap 的framebuffer驱动程序
查看>>
android2.3 dvsdk
查看>>
QT Creater的安装配置
查看>>
QT5学习总结
查看>>
ubuntu 安装使用dbus
查看>>
QT QDbus
查看>>
android init launch
查看>>
nand booting
查看>>
android boot animation
查看>>
Python环境搭建
查看>>
Python3 基础
查看>>
Python3 基础(itcast学习笔记)
查看>>
ubuntu_fastboot
查看>>
ubuntu18.04 Android 7.1.2 编译配置
查看>>