一)介绍

    NTP(The Network Time Protocol) 是网络时间协议,用以同步网络内计算机的时间。它通过udp包交换,用特定算法进行协商,从而把计算机上的时间与时间服务器上的时间保持一致。通过互联网它支持的误差是10毫秒,局域网则可以达到200微秒。NTP时间服务器是分层的,就算自己搭建一个时间服务器也需要指向上层的一个时间服务器然后再向局域网中的服务器提供服务。

二)安装NTP,采用yum安装

[root@localhost ~]# yum -y install ntp

三)修改配置文件

vim /etc/ntp.conf

把这行注释掉restrict default kod nomodify notrap nopeer noquery

换成如下行:
restrict default nomodify意思是允许任何主机都可以进行时间同步

也可以增加一个网段:

restrict 192.168.1.0 mask 255.255.255.0 nomodify

四)启动ntp服务器,查看端口

[root@localhost ~]# /etc/init.d/ntpd start

[root@localhost ~]# ss -lnutp | grep ntp 端口123

六)等待一段时间查看服务端时间效果

[root@localhost ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
*tock.stdtime.go 129.6.15.29 2 u 76 64 256 174.231 16.475 14.579
59-124-196-83.H .INIT. 16 u - 128 0 0.000 0.000 0.000
59-124-196-84.H .INIT. 16 u - 128 0 0.000 0.000 0.000
[root@localhost ~]# ntpstat
synchronised to NTP server (220.130.158.71) at stratum 3
time correct to within 889 ms
polling server every 64 s

七)客户端设置cront按时同步即可

客户端同步需要等待一段时间否则会报错:no server suitable for synchronization found

耐心等待一杯茶的时间O(∩_∩)0

crontab -e

* * * * * /usr/sbin/ntpdate 192.168.1.120