天天看点

服务器/客户端方式NTP服务器配置

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服务器安装

安装环境:

<code>[root@ntp-server ~]</code><code># cat /etc/redhat-release </code>

<code>CentOS release 6.8 (Final)</code>

<code>[root@ntp-server ~]</code><code># uname -r</code>

<code>2.6.32-642.el6.x86_64</code>

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

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

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

如果没有安装

1.配置yum源

1)备份

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

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

CentOS 5

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

CentOS 6

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

CentOS 7

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

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

2.安装NTP服务

yum install -y ntp

4、配置文件

<code>[root@ntp-server ~]</code><code># cat /etc/ntp.conf</code>

<code># For more information about this file, see the man pages</code>

<code># ntp.conf(5), ntp_acc(5), ntp_auth(5), ntp_clock(5), ntp_misc(5), ntp_mon(5).</code>

<code>driftfile </code><code>/var/lib/ntp/drift</code>

<code># Permit time synchronization with our time source, but do not</code>

<code># permit the source to query or modify the service on this system.</code>

<code>restrict default kod nomodify notrap nopeer noquery</code>

<code>restrict -6 default kod nomodify notrap nopeer noquery</code>

<code># Permit all access over the loopback interface.  This could</code>

<code># be tightened as well, but to do so would effect some of</code>

<code># the administrative functions.</code>

<code>restrict 127.0.0.1 </code>

<code>restrict -6 ::1</code>

<code># Hosts on local network are less restricted.</code>

<code>#restrict 10.0.0.0 mask 255.255.255.0 nomodify notrap</code>

<code># Use public servers from the pool.ntp.org project.</code>

<code># Please consider joining the pool (http://www.pool.ntp.org/join.html).</code>

<code>#server 0.centos.pool.ntp.org iburst    注释掉默认的时间服务器</code>

<code>#server 1.centos.pool.ntp.org iburst    注释掉默认的时间服务器</code>

<code>#server 2.centos.pool.ntp.org iburst    注释掉默认的时间服务器</code>

<code>#server 3.centos.pool.ntp.org iburst    注释掉默认的时间服务器</code>

<code>server pool.ntp.org              配置时间服务器</code>

<code>server </code><code>time</code><code>.nist.gov             配置时间服务器</code>

<code>#broadcast 192.168.1.255 autokey        # broadcast server</code>

<code>#broadcastclient                        # broadcast client</code>

<code>#broadcast 224.0.1.1 autokey            # multicast server</code>

<code>#multicastclient 224.0.1.1              # multicast client</code>

<code>#manycastserver 239.255.254.254         # manycast server</code>

<code>#manycastclient 239.255.254.254 autokey # manycast client</code>

<code># Enable public key cryptography.</code>

<code>#crypto</code>

<code>includefile </code><code>/etc/ntp/crypto/pw</code>

<code># Key file containing the keys and key identifiers used when operating</code>

<code># with symmetric key cryptography. </code>

<code>keys </code><code>/etc/ntp/keys</code>

<code># Specify the key identifiers which are trusted.</code>

<code>#trustedkey 4 8 42</code>

<code># Specify the key identifier to use with the ntpdc utility.</code>

<code>#requestkey 8</code>

<code># Specify the key identifier to use with the ntpq utility.</code>

<code>#controlkey 8</code>

<code># Enable writing of statistics records.</code>

<code>#statistics clockstats cryptostats loopstats peerstats</code>

5、启动NTP服务器

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

<code>[root@ntp-server ~]</code><code># cat /var/spool/cron/root    </code>

<code>#*/5 * * * * /usr/sbin/ntpdate pool.ntp.org &amp;&gt;/dev/null</code>

启动NTP服务

<code>[root@ntp-server ~]</code><code># /etc/init.d/ntpd start</code>

<code>Starting ntpd:                                           [  OK  ]</code>

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

<code>[root@ntp-server ~]</code><code>#  ntpq -p</code>

<code>     </code><code>remote           refid      st t when poll reach   delay   offset  jitter</code>

<code>==============================================================================</code>

<code>*news.neu.edu.cn 202.118.1.47     2 u   48   64    1   89.492   25.310   0.000</code>

<code> </code><code>nist1-lnk.binar .INIT.          16 u    -   64    0    0.000    0.000   0.000</code>

位置

标志

含义

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分钟后才能成功连接和同步。

<code>[root@ntp-server ~]</code><code># ntpstat</code>

<code>synchronised to NTP server (216.229.0.179) at stratum 2 </code>

<code>   </code><code>time</code> <code>correct to within 4121 ms</code>

<code>   </code><code>polling server every 64 s</code>

<code>[root@ntp-server ~]</code><code># ntpdate 10.0.0.129 </code>

<code> </code><code>6 Jun 00:01:39 ntpdate[1496]: the NTP socket is </code><code>in</code> <code>use, exiting</code>

6、客户机时间同步

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

<code> </code><code>8 Sep 15:43:16 ntpdate[2962]: adjust </code><code>time</code> <code>server 10.0.0.129 offset 0.117714 sec</code>

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

本文转自 炫维 51CTO博客,原文链接:http://blog.51cto.com/xuanwei/1850769

继续阅读