Linux杂记-配置ntp时间同步服务

1.概念
NTP是网络时间协议(Network Time Protocol),它是用来同步网络中各个计算机的时间的协议。

2.安装

  • 目标
    现集群有三台主机,分别为centos7-1,centos7-2,centos7-3.
    centos7-1作为master,同步硬件时间.
    centos7-2,centos7-3同步centos7-1的时间.

  • 安装ntp
    三台主机分别安装ntp,并设置一个初始时间.

yum -y install ntp
date -s '2018-05-04 00:00:00'
  • 修改/etc/ntp.conf

    centos7-1

#以centos7-1的硬件时间作为时间源
server 127.127.1.0 # local clock 
fudge 127.127.1.0 stratum 10

#注释掉其他时间源
#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

#授权192.168.163.*网段可以同步
restrict 192.168.163.0 mask 255.255.255.0 nomodify notrap

centos7-2&centos7-3

#只配置centos7-1作为同步时间源
server centos7-1
#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
  • 三台分别启动ntpd服务
systemctl start ntpd
  • 查看与时间源的偏差
ntpdc -c loopinfo
offset:               -0.000019 s
frequency:            -0.006 ppm
poll adjust:          24
watchdog timer:       198 s
  • 查看同步的时间源
ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*centos7-1       LOCAL(0)        11 u   10   64  377    0.228   -0.019   0.032
  • 查看
    稍等几分钟后,执行date查看各主机的时间.

  • 注意
    VMware虚拟机上的主机,在suspend并重启之后,各台主机的时间会出现较大偏差.而ntpd是平滑同步,需要较长的时间,此时最好进行一次ntpdate手工同步.

ntpdate -u 192.168.163.101

猜你喜欢

转载自blog.csdn.net/chixiaoen/article/details/80194419