zoukankan      html  css  js  c++  java
  • ntp时间同步

    NTP时间服务器

    NTP简介

    NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把 
    
    计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1- 
    
    50ms。 
    
    NTP服务器就是利用NTP协议提供时间同步服务的。 
    
    简易NTP实现原理图 
    

    环境准备

    主机规划

    服务端安装部署

    #安装ntp服务和ntpdate 
    
    #启动ntp服务 
    
    [root@ntpserver ~]# cat /etc/redhat-release 
    
    CentOS Linux release 7.5.1804 (Core) 
    
    NTP服务端:ntpserver IP:10.0.0.61 
    
    NTP客户端:ntpclient IP:10.0.0.7 
    yum install ntp ntpdate -y#查看是否启动成功 
    
    
    

    配置ntp配置文件

    #重启ntp服务

    注意:如果有同步时间的定时任务要将其注销,否则会冲突

    #查看ntp服务器与上层ntp的状态

    systemctl start ntpd.service 
    
    systemctl enable ntpd.service 
    
    netstat -lntup|grep ntpd 
    
    [root@ntpserver ~]# vim /etc/ntp.conf 
    
    driftfile /var/lib/ntp/drift 
    
    restrict default kod nomodify notrap nopeer noquery 
    
    restrict -6 default kod nomodify notrap nopeer noquery 
    
    restrict 127.0.0.1 
    
    restrict -6 ::1 
    
    允许内网其他机器同步时间
    restrict 172.16.1.0 mask 255.255.255.0 nomodify notrap 
    
    #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 
    
    定义使用的上游 ntp服务器,将原来的注释
    server time1.aliyun.com 
    
    server ntp1.aliyun.com 
    
    允许上层时间服务器主动修改本机时间
    restrict time1.aliyun.com nomodify notrap noquery 
    
    restrict ntp1.aliyun.com nomodify notrap noquery 
    
    
    
    外部时间服务器不可用时,以本地时间作为时间服务
    server 127.127.1.0 
    
    fudge 127.127.1.0 stratum 10 
    
    includefile /etc/ntp/crypto/pw 
    
    keys /etc/ntp/keys 
    
    systemctl restart ntpd.service 
    
    [root@ntpserver ~]# ntpq -p 
    
    remote refid st t when poll reach delay offset jitter 
    
    ============================================================================== 
    
    203.107.6.88 10.165.84.13 2 u 66 64 7 27.784 10.499 1.787 
    
    *120.25.115.20 10.137.53.7 2 u 3 64 17 33.749 9.611 1.618 
    
    LOCAL(0) .LOCL. 10 l 10 64 17 0.000 0.000 0.000 
    
    参数含义
    remote:本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先**客户端部署** 
    
    安装ntp服务和ntpdate
    #配置ntp服务配置文件 
    
    #手动同步一次时间 (服务端主机IP,这里需要先关闭NTP服务哦) 
    
    注意:客户机要等几分钟再与新启动的ntp服务器进行时间同步,否则会提示以下这个错误。 
    
    no server suitable for synchronization found 
    
    在这里,你可以设置定时任务,定时向服务端同步时间,亦可以启动ntp服务,使其自动向服务端同步时间。 
    
    #启动ntp服务 
    
    refid:参考上一层ntp主机地址 
    
    st:stratum阶层 
    
    when:多少秒前曾经同步过时间 
    
    poll:下次更新在多少秒后 
    
    reach:已经向上层ntp服务器要求更新的次数 
    
    delay:网络延迟 
    
    offset:时间补偿 
    
    jitter:系统时间与bios时间差 
    
    yum install ntp ntpdate -y 
    
    [root@web01 ~]# cat /etc/ntp.conf 
    
    ... 
    
    ... 
    
    restrict 127.0.0.1 
    
    restrict ::1 
    
    #新增此行,进行权限配置 
    
    restrict 172.16.1.0 mask 255.255.255.0 nomodify notrap 
    # 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 172.16.1.51 prefer 
    
    #broadcast 192.168.1.255 autokey # broadcast server 
    
    #broadcastclient # broadcast client 
    
    .... 
    
    .... 
    
    [root@ntpclient ~]# ntpdate 172.16.1.61 
    
    25 Mar 14:38:04 ntpdate[2937]: step time server 172.16.1.51 offset 139055.717574 sec#观察时间同步状况 
    
    查看时间同步结果 
    

    拓展一:系统时间与硬件时间同步

    如果主从服务时间超过1000秒则不再进行同步了,这时候要手动同步,即:/usr/sbin/ntpdate命令,如果怕服务器 
    
    时差会经常变动比较大可以再Linux中添加计划任务,例如: 
    
    ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfifig/ntpd文件, 
    
    在/etc/sysconfifig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。 
    
    拓展2:不同机器之间的时间同步
    为了避免主机时间因为长期运作下所导致的时间偏差,进行时间同步(synchronize)的工作是非常必要的。 
    
    同步时间,可以使用ntpdate命令,也可以使用ntp服务。 
    
    方法一:使用ntpdate 比较简单。格式如下:
    [root@client ~]# ntpdate 172.16.1.61 
    
    23 May 19:50:44 ntpdate[7507]: step time server 172.16.1.61 offset 1.239826 sec 
    
    systemctl start ntpd.service 
    
    systemctl enable ntpd.service 
    
    [root@ntpcilent ~]# ntpq -p 
    
    remote refid st t when poll reach delay offset jitter 
    
    ============================================================================== 
    
    172.16.1.61 203.107.6.88 3 u - 64 1 2.485 400.150 0.000 
    
    [root@ntpcilent ~]# ntpstat 
    
    unsynchronised 
    
    time server re-starting 
    
    polling server every 8 s 
    
    #同步失败,同步也需要时间嘛,需等待5-10分钟再次查询 
    
    [root@ntpcilent ~]# ntpstat 
    
    synchronised to NTP server (172.16.1.61) at stratum 3 
    
    time correct to within 28 ms 
    
    polling server every 64 s 
    
    #OK!时间同步完成,date一下是不是和服务器主机时间一致呢 
    10 5 * * * root /usr/sbin/ntpdate 192.168.31.223 && /sbin/hwclock -w 
    
    hwclock -r 读出BIOS的时间参数 
    
    hwclock -w 将当前系统时间写入BIOS中但这样的同步,只是强制性的将系统时间设置为ntp 服务器时间。只是治标不治本。所以,一般配合cron命令,来进 
    
    行定期同步设置。比如,在crontab 中添加: 
    
    [root@client ~]# crontab -e 
    
    0 10 * * * /usr/sbin/ntpdate 172.16.1.61 
    
    方法二:使用ntpd 服务进行同步。
    要注意的是,ntpd 有一个自我保护设置:如果本机与上源时间相差太大,ntpd 不运行。所以新设置的时间服务器
    一定要先ntpdate 从上源取得时间初值。然后启动ntpd 服务。 
    
    ntpd,ntpdate的区别
    下面是网上关于ntpd与ntpdate区别的相关资料。如下所示所示: 
    
    使用之前得弄清楚一个问题,ntpd与ntpdate在更新时间时有什么区别。ntpd不仅仅是时间同步服务器,它还可以做客户端 
    
    与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可 
    
    同时运行。 
    
    时钟的跃变,对于某些程序会导致很严重的问题。许多应用程序依赖连续的时钟——毕竟,这是一项常见的假定,即,取得的时 
    
    间是线性的,一些操作,例如数据库事务,通常会地依赖这样的事实:时间不会往回跳跃。不幸的是,ntpdate调整时间的方 
    
    式就是我们所说的”跃变“:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,这有几个非常明显的问 
    
    题:
    
    第一,这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并 
    
    令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时 
    
    间不一样的时候,唯一的办法是以服务器为准)。 
    
    第二,这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时 
    
    间,而且能够校准计算机的时钟。 
    
    第三,这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间 
    
    快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。因而,唯一一个可以令时间发生跳变的点,是计算机 
    
    刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的 
    
    时间。 
    
    NTPD 在和时间服务器的同步过程中,会把 BIOS 计时器的振荡频率偏差——或者说 Local Clock 的自然漂移(drift)—— 
    
    记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的
    
  • 相关阅读:
    随记(二)--是否能打开手机某个应用
    asp.net 登陆后在ashx处理程序中获取不到Session
    Highcharts Pie 饼图提示标签IE下重叠解决方法,及json数据绑定方法
    怎样用ZBrush对模型进行渲染(二)
    怎样用ZBrush对模型进行渲染
    ZBrush中该如何调节多个SubTool
    如何用ZBrush快速绘制身体
    MIDI信息为什么不能通过FL Studio输出
    怎样制作FL Studio步进音序器中的节奏
    不同材质怎么通过ZBrush赋予同一个模型上
  • 原文地址:https://www.cnblogs.com/223zhp/p/11663506.html
Copyright © 2011-2022 走看看