一、介绍:

同步时间,可以使用ntpdate命令,也可以使用ntpd服务,使用之前得弄清楚一个问题,ntpd与ntpdate在更新时间时有什么区别。ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行。

使用ntpd服务,要好于ntpdate加cron的组合。因为,ntpdate同步时间,会造成时间的跳跃,对一些依赖时间的程序和服务会造成影响。比如sleep,timer等。而且,ntpd服务可以在修正时间的同时,修正cpu tick。理想的做法为,在开机的时候,使用ntpdate强制同步时间,在其他时候使用ntpd服务来同步时间。

要注意的是,ntpd有一个自我保护设置: 如果本机与上源时间相差太大, ntpd不运行. 所以新设置的时间服务器一定要先ntpdate从上源取得时间初值, 然后启动ntpd服务。ntpd服务运行后, 先是每64秒与上源服务器同步一次, 根据每次同步时测得的误差值经复杂计算逐步调整自己的时间, 随着误差减小, 逐步增加同步的间隔. 每次跳动, 都会重复这个调整的过程.

时钟的跃变,对于某些程序会导致很严重的问题。许多应用程序依赖连续的时钟——毕竟,这是一项常见的假定,即,取得的时间是线性的,一些操作,例如数据库事务,通常会地依赖这样的事实:时间不会往回跳跃。不幸的是,ntpdate调整时间的方式就是我们所说的”跃变“:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,这有几个非常明显的问题:

第一,这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。

第二,这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。

第三,这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。

NTPD 在和时间服务器的同步过程中,会把 BIOS 计时器的振荡频率偏差——或者说 Local Clock 的自然漂移(drift)——记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的走时。

示例:使用ntpdate比较简单。格式如下

# ntpdate [-nv] [NTP IP/hostname]
 # ntpdate 192.168.0.2
 # ntpdate time.ntp.org

但这样的同步,只是强制性的将系统时间设置为ntp服务器时间。如果cpu tick有问题,只是治标不治本。所以,一般配合cron命令,来进行定期同步设置。比如,在crontab中添加: root路径下 ()

在/etc/crontab 文件下 添加
0 12 * * * * /usr/sbin/ntpdate 192.168.0.1

这样,会在每天的12点整,同步一次时间。ntp服务器为192.168.0.1。

配置案例:

二、服务端配置
1.首先安装检查服务器是否安装了ntp、ntpdate

# rpm -qa | grep ntp
 ntpdate-4.2.6p5-29.el7.centos.x86_64
 ntp-4.2.6p5-29.el7.centos.x86_642.如果没有,需要使用安装
 # yum -y install ntp ntpdate3.修改ntp配置文件/etc/ntp.conf
1)注释以下配置
 # Use public servers from the pool.ntp.org project.
 # Please consider joining the pool (http://www.pool.ntp.org/join.html).
 #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
 2)新增如下配置#日志文件
 logfile /var/log/ntpd.log#授权172.16.1.0网段上所有机器可以从这台机器上查询和时间同步
 restrict 172.16.1.0 mask 225.225.225.0 nomotify notrap#时间服务器列表
 server 210.72.145.44    #中国国家授时中心
 server ntp1.aliyun.com
 server ntp2.aliyun.com
 server ntp3.aliyun.com#当外部时间不可用时,使用本地时间
 server 127.0.0.1
 fudge 127.0.0.1 stratum 10#允许上层时间服务器主动修改本机时间
 restrict 210.72.145.44    nomodify notrap noquery
 restrict ntp1.aliyun.com  nomodify notrap noquery
 restrict ntp2.aliyun.com  nomodify notrap noquery
 restrict ntp3.aliyun.com  nomodify notrap noquery4.保存退出,重启ntp服务、加入开启自启
 # systemctl enable ntpd
 # systemctl restart ntpd5.启动ntp服务时,先手动同步下本地时间
 [root@m01 ~]# ntpdate -u ntp1.aliyun.com6.查询ntp同步时间是否启动
 等待几分钟后,当出现如同第二次命令执行结果时即可# ntpstat 
 unsynchronised
   time server re-starting
    polling server every 8 s# ntpstat 
 synchronised to NTP server (120.25.115.20) at stratum 3
    time correct to within 154 ms
    polling server every 64 s

三、客户端配置
1.安装ntp、ntpdate

[root@web01 ~]# yum install ntp ntpdate

2.修改配置文件/etc/ntp.conf

1)注释以下配置

# Use public servers from the pool.ntp.org project.
 # Please consider joining the pool (http://www.pool.ntp.org/join.html).
 #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

2)新增如下配置

#ntp服务器地址
 server 172.16.1.61#新增:允许上层时间服务器主动修改本机时间
 restrict 172.16.1.61 nomodify notrap noquery#新增:当外部时间不可用时,使用本地时间
 server 127.0.0.1  #local clock
 fudge 127.0.0.1 stratum 10

3.保存配置,重启ntp服务并加入开启自启

[root@web01 ~]# systemctl enable ntpd
 [root@web01 ~]# systemctl restart ntpd

4.查看ntp服务器信息

[root@web01 zabbix]# ntpq -p
      remote           refid      st t when poll reach   delay   offset  jitter
 ==============================================================================
  172.16.1.61     120.25.115.20    3 u   30   64    1    0.512  -42.740   0.000
  localhost       .INIT.          16 l    -   64    0    0.000    0.000   0.000

四、测试
1.查看ntp服务端时间

[root@m01 ~]# date
 Sun Mar 29 00:10:52 CST 2020

2.修改客户端时间

[root@web01 ~]# date -s 2020-01-01
 Wed Jan  1 00:00:00 CST 2020
 [root@web01 ~]# date
 Wed Jan  1 00:00:03 CST 2020

3.等待一段时间后客户端时间自动同步,这里用手动同步验证下

[root@web02 ~]# ntpdate -u 172.16.1.61
 29 Mar 00:12:14 ntpdate[19563]: adjust time server 172.16.1.61 offset 0.008159 sec
 [root@web01 ~]# date
 Sun Mar 29 00:12:22 CST 2020

4.如果觉得自动同步时间间隔比较久,可以设定个定时任务,使用ntpdate命令来同步服务端时间
这里以每分钟同步为例

[root@m01 ~]# crontab -e 
 * * * * * /usr/sbin/ntpdate -u 172.16.1.61 &> /dev/null

 

至此,ntp时间同步服务器搭建完成