1、NTP服务介绍

Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议***。NTP可以利用多个途径和来源的时间服务器来更加精确的校正时间。

现在绝大部分的操作系统都支持NTP,比如在Unix和Windows中都有他们自己的NTP服务。比如在Windows中的SNTP。由于各个系统厂商会根据自己的系统情况对NTP来进行优化和设置,更加适合他自己的系统,对于其他系统的兼容性就会差很多。还有不同时期的系统会使用不同版本的NTP服务。最新版本位V4,由于每个版本都要他自己的一些特征。所以在设置NTP服务器时会有一些兼容性问题。因此,在多类型操作系统的环境中,尽量使用NTP官方组织提供的软件进行设置而不使用其他厂商在系统中默认提供的NTP服务器。NTP官方组织为 www.ntp.org


2、NTP服务器的运行方式


NTP服务器的使用方式包括,固定连接和动态连接方式。在固定连接中,客户端仅仅接收指定的服务器端的连接,无论服务端是否正常连接他都不会去换另外的服务器端接收时间。一般客户/服务器方式为这种模式。另外一直为动态连接,客户端在不停接收服务器端的广播信息,当一台服务器停止服务,只要有服务器广播出来的正确可靠的时间信息,客户端就可以正常的更新时间。一般为广播方式连接。

 

1.服务器/客户端方式

客户服务器方式为在互联网上使用最为广泛的一种方法,在设定服务器后,他可以灵活的配置客户端,让客户端选择是否使用NTP服务器或使用那台NTP服务器。客户服务器方式使用常用的RPC(remote-procedure-call)方式来传输时间信息。他们运行的过程为,客户端发出时间请求给服务器端,然后根据服务器端的答复来调节时间。

一般在客户端可以定义他们同步时间的间隔,和他们请求最大和最小的时间间隔。客户端会根据自己时钟时间和NTP服务器时间进行比对,如果客户端本机时钟比较准确,他们查询的时间间隔就会变长,如果本机时间出现误差较大,客户端会在比较短的时间内做出时间请求,来保障客户端时间的精确度。

 

2.广播/多播方式

多播和广播方式利用在以太网上的广播和多播协议来进行时间同步,时间服务器会定时发出广播和多播信息给指定的网段。使用广播和多播需要网络设备支持该协议,同时客户端也支持该协议。可以使用multicasting和manycasting在不同网段进行广播信息,来达到在包含多个网段的复杂网络中使用广播和多播来提供NTP服务。

 

主动/被动方式

主动/被动方式最多使用在集群系统中,由于集群系统对同一集群内的所有服务器的时间同步要求比较严格,他们相互配置为NTP服务器端和客户端,在不停的提供时钟同步服务,当其中的一台服务器出现停顿时不会影响到其他服务器的时间同步。可以说时互备的方式来进行时间同步服务。


3、NTP服务器安装

安装环境:

[root@ntp-server ~]# cat /etc/redhat-release 
CentOS release 6.8 (Final)
[root@ntp-server ~]# uname -r
2.6.32-642.el6.x86_64

NTP服务在安装系统的时候会被默认安装上

[root@ntp-server ~]# rpm -qa ntp

ntp-4.2.6p5-10.el6.centos.x86_64

如果没有安装

1.配置yum源

1)备份

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

2)下载新的CentOS-Base.repo 到/etc/yum.repos.d/

CentOS 5

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-5.repo

CentOS 6

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-6.repo

CentOS 7

wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

3)之后运行yum makecache生成缓存

2.安装NTP服务

yum install -y ntp


4、配置文件

[root@ntp-server ~]# cat /etc/ntp.conf
# For more information about this file, see the man pages
# ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).
driftfile /var/lib/ntp/drift
# Permit time synchronization with our time source, but do not
# permit the source to query or modify the service on this system.
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
# Permit all access over the loopback interface.  This could
# be tightened as well, but to do so would effect some of
# the administrative functions.
restrict 127.0.0.1 
restrict -6 ::1
# Hosts on local network are less restricted.
#restrict 10.0.0.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 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    注释掉默认的时间服务器
server pool.ntp.org              配置时间服务器
server time.nist.gov             配置时间服务器
#broadcast 192.168.1.255 autokey        # broadcast server
#broadcastclient                        # broadcast client
#broadcast 224.0.1.1 autokey            # multicast server
#multicastclient 224.0.1.1              # multicast client
#manycastserver 239.255.254.254         # manycast server
#manycastclient 239.255.254.254 autokey # manycast client
# Enable public key cryptography.
#crypto
includefile /etc/ntp/crypto/pw
# Key file containing the keys and key identifiers used when operating
# with symmetric key cryptography. 
keys /etc/ntp/keys
# Specify the key identifiers which are trusted.
#trustedkey 4 8 42
# Specify the key identifier to use with the ntpdc utility.
#requestkey 8
# Specify the key identifier to use with the ntpq utility.
#controlkey 8
# Enable writing of statistics records.
#statistics clockstats cryptostats loopstats peerstats


5、启动NTP服务器

如果有定时时间同步先注释掉,以免发生冲突

[root@ntp-server ~]# cat /var/spool/cron/root    
#*/5 * * * * /usr/sbin/ntpdate pool.ntp.org &>/dev/null

启动NTP服务

[root@ntp-server ~]# /etc/init.d/ntpd start
Starting ntpd:                                           [  OK  ]


ntpq用来监视ntpd操作,ntpq -p查询网络中的NTP服务器,同时显示客户端和每个服务器的关系

[root@ntp-server ~]#  ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*news.neu.edu.cn 202.118.1.47     2 u   48   64    1   89.492   25.310   0.000
 nist1-lnk.binar .INIT.          16 u    -   64    0    0.000    0.000   0.000
位置
标志
含义
remote之前
*
响应的NTP服务器和最精确的服务器
+
响应这个查询请求的NTP服务器
blank(空格)
没有响应的NTP服务器
列表上方
remote
响应这个请求的NTP服务器的名称
refid
NTP服务器使用的更高一级服务器的名称
st
正在响应请求的NTP服务器的级别
when
上一次成功请求之后到现在的秒数
poll
本地和远程服务器多少时间进行一次同步,单位秒,在一开始运行NTP的时候这个poll值会比较小,服务器同步的频率大,可以尽快调整到正确的时间范围,之后poll值会逐渐增大,同步的频率也就会相应减小
reach
用来测试能否和服务器连接,是一个八进制值,每成功连接一次它的值就会增加
delay
从本地机发送同步要求到ntp服务器的往返时间
offset
主机通过NTP时钟同步与所同步时间源的时间偏移量,单位为毫秒,offset越接近于0,主机和ntp服务器的时间越接近
jitter
统计了在特定个连续的连接数里offset的分布情况。简单地说这个数值的绝对值越小,主机的时间就越精确

ntpstat 命令查看时间同步状态,这个一般需要5-10分钟后才能成功连接和同步。

[root@ntp-server ~]# ntpstat
synchronised to NTP server (216.229.0.179) at stratum 2 
   time correct to within 4121 ms
   polling server every 64 s
[root@ntp-server ~]# ntpdate 10.0.0.129 
 6 Jun 00:01:39 ntpdate[1496]: the NTP socket is in use, exiting

6、客户机时间同步

客户机要等几分钟再与新启动的ntp服务器进行时间同步,否则会提示no server suitable for synchronization found错误。

[root@ntp-server ~]# ntpdate 10.0.0.129 
 8 Sep 15:43:16 ntpdate[2962]: adjust time server 10.0.0.129 offset 0.117714 sec

然后将ntpdate 10.0.0.129写入定时任务中