时间伺服器 ntp config setting

参考文档 :

http://linux.vbird.org/linux_server/0440ntp.php

https://www.cnblogs.com/xwdreamer/p/3448773.html

由于NTP 时间伺服器采用类似阶层架构(stratum) 来处理时间的同步化, 所以他使用的是类似一般server/client 的主从架构。

在ntp协议中,将网络中存在的机器一共分为16层,每一台机器即作为client从上层接收信息校准本机时间,也可以作为server向下层提供时间校准服务。

1.安装ntp


   14.04  : sudo apt-get install ntp

   16.04  : sudo apt-get install ntp  sudo apt-get install ntpdate

( 注: ntp与ntpdate是两种原理不同的同步方法,ntp协议是渐进式对齐,不会发生时间跳变,但要求两机器对齐前的初始时间不能相差过大,
而ntpdate协议是向另一台机器瞬间对齐,会造成时间线的跳变,

在14.04中在同一个软件包中,16.04被分为两个)

2.配置 /etc/ntp.conf 文档

示例/etc/ntp.conf
host4:


   restrict -4 default kod notrap nomodify nopeer noquery

  restrict -6 default kod notrap nomodify nopeer noquery

  restrict 127.0.0.1

  restrict ::1

  restrict 172.23.100.0 mask 255.255.255.0 nomodify

  server 127.127.1.0

host1:


  restrict -4 default kod notrap nomodify nopeer noquery

  restrict -6 default kod notrap nomodify nopeer noquery

  restrict 127.0.0.1

  restrict ::1

  restrict 172.23.100.195

  server 172.23.100.195

其中需要改动的主要有两部分: restrict 和 server

restrict :

   restrict -4 default kod notrap nomodify nopeer noquery      # 禁止所有ipv4地址主机访问本机的权限

  restrict -6 default kod notrap nomodify nopeer noquery      # 禁止所有ipv6地址主机访问本机的权限

  restrict 127.0.0.1      # 与下一行一起开放本机访问和修改自身的权限

  restrict ::1

  restrict 172.23.100.0 mask 255.255.255.0 nomodify     # 开放该网段下主机访问本机的权限 以对其自身时间进行同步

server : ( 在局域网内配置时 以下命令行2选1)

server 172.23.100.195 #  此处以host4机器为本局域网内的server端为例, 除host4外所有机器均用此命令配置server选项

server 127.127.1.0  #  host4使用此命令,将其的server设为自身,( 很重要,若server端的host4找不到其需要对齐的上层服务器,

会导致ntp服务失效,此处令其向自己对齐,缩短对齐时间,避免一些不必要的麻烦 )

3.局域网中时间同步流程

在车上的172.23.100.x局域网中,以host4(172.23.100.195)作为server端,其余主机作为client端

开机后首先host4寻找到其上层server端(一般即其自身),之后其他所有主机采用ntpdate的方法对齐到host4,保证其时间间隔不会过大

对齐后其他主机采用ntp的方法 周期性的访问host4的时间并进行同步。

4.常用命令


  watch ntpq -p                                #查看本机和上层server间的时间差

  sudo service ntp status

  sudo service ntp start

  sudo service ntp stop

  sudo service ntp restart

  sudo /usr/sbin/ntpdate 172.23.100.195  # 采用ntpdate协议将时间立刻对齐到host4

  ntpdate -d 172.23.100.195                     #查看和上层server端host4间的时间间隔

  service --status-all  :                                #查看本机服务启动状态

5.tips

(1)ntp和ntpdate不可同时运行,所以正确的同步流程应该为:

  sudo service ntp stop

  sudo /usr/sbin/ntpdate 172.23.100.195

  sudo service ntp start

(2) synctime.sh的作用: 在client端主机运行,首先将本机时间向前前移10s,然后进行(1)的操作
autosynctime.sh的作用: 在client端主机运行,ping操作检查本机与上层server端的连接状态,连接成功后运行synctime.sh

(3) 备用命令:


        sudo ntpdate 172.23.100.195

        sudo /etc/init.d/ntp restart

        sudo hwclock -w        # 将本机系统时间写入到bios时间

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。