一、ntp服务是什么

1. 定义

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

2. 发展

首次记载在Internet Engineering Note之中,其精确度为数百毫秒。
稍后出现了首个时间协议的规范RFC-778,它被命名为DCNET互联网时间服务,这种服务还是借助于Internet control Message Protocol (ICMP)中的时间戳和时间戳应答消息作为NTP。
名称的首次出现是在RFC-958之中,该版本也被称为NTP v0,其目的是为ARPA网提供时间同步。已脱离ICMP,作为独立协议运行,
美国特拉华大学的David L.Mills主持了网络时间同步项目,成功的开发出了NTP协议的第1, 2, 3版。

v1:出现于1988年6月,在RFC-1059中描述了首个完整的NTP的规范和相关算法。这个版本已经采用了client/server模式以及对称操作,但是它不支持授权鉴别和NTP的控制消息。

v2:1989年9月推出了取代RFC-958和RFC-1059的NTP v2版本即RFC-1119。

v3:在1992 年3月,NTP v3版本RFC-1305问世,该版本总结和综合了NTP先前版本和DTSS,正式引入了校正原则,并改进了时钟选择和时钟滤波的算法,而且还引入了时间消息发送的广播模式,这个版本取代了NTP的先前版本。NTP v 3 发布后,一直在不断地进行改进,NTP实现的一个重要功能是对计算机操作系统的时钟调整。

v4:截止到2010年6月,最新的NTP版本是第4版(NTPv4),其标准化文档为 RFC 5905,它继承自RFC 1305所描述的NTP v3。网络时间同步技术也将向更高精度、更强的兼容性和多平台的适应性方向发展。网络时间协议NTP是用于互联网中时间同步的标准之一,它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.lms,在Internet上绝大多数的地方其精度可以达到1- 50ms.

值得提一下的是,简单的NTP(SNTP)version4已经在RFC2030描述了。

3. 主要结构

(1) 时间来源

现在的标准时间是由原子钟报时的国际标准时间UTC(Universal Time Coordinated,世界协调时),所以NTP获得UTC的时间来源可以是原子钟、天文台、卫星,也可以从Internet上获取。

(2) 传播

在NTP中,定义了时间按照服务器的等级传播,按照离外部UTC源远近将所有的服务器归入不同的Stratum(层)中,例如把通过GPS取得发送标准时间的服务器叫Stratum-1的NTP服务器,而Stratum-2则从Stratum-1获取时间,Stratum-3从Stratum-2获取时间,以此类推,但Stratum层的总数限制在15以内。所有这些服务器在逻辑上形成阶梯式的架构相互连接,而Stratum-1的时间服务器是整个系统的基础,这种阶梯式的架构示意图如下图所示:

计算机主机一般同多个时钟服务器连接,利用统计学的算法过滤来自不同服务器的时间,以选择最佳的路径和来源以便校正主机时间。即使在主机长时间无法与某一时钟服务器联系的情况下,NTP服务依然可以有效运转。

二、架构及安装

1. 架构

如上图所示,对于一些服务依赖于时间的集群(如hadoop集群),需要有一部主机作为ntp服务器,其他客户端主机从这部主机进行时间同步,另外ntp服务主机从更高一层的服务器获得时间信息。

2. 安装

centos系统,直接用yum进行安装就可以了:

yum -y install ntp

三、主要配置

1. 配置文件

centos7的ntp配置文件存放路径为:/etc/ntp.conf。

2. restrict 控制相关权限。

语法为: restrict IP地址 mask 子网掩码 参数

其中IP地址也可以是default ,default 就是指所有的IP。

参数有以下几个:

ignore  :关闭所有的 NTP 联机服务

nomodify:客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。

notrust :客户端除非通过认证,否则该客户端来源将被视为不信任子网

noquery :不提供客户端的时间查询:用户端不能使用ntpq,ntpc等命令来查询ntp服务器

notrap :不提供trap远端登陆:拒绝为匹配的主机提供模式 6 控制消息陷阱服务。陷阱服务是 ntpdq 控制消息协议的子系统,用于远程事件日志记录程序。

nopeer :用于阻止主机尝试与服务器对等,并允许欺诈性服务器控制时钟

kod : 访问违规时发送 KoD 包。

restrict -6 表示IPV6地址的权限设置。

3. server 设定NTP主机来源

语法为:server 主机ip或域名 参数

参数有以下几个:

burst:When the server is reachable, send a burst of eight packets instead of the usual one. The packet spacing is nor‐mally 2 s; however, the spacing between the first and second packets can be changed with the calldelay command to allow additional time for a modem or ISDN call to complete. This option is valid only with the server command and type s addressesa. It is a recommended option when the maxpoll option is greater than 10 (1024 s).

iburst:When the server is unreachable, send a burst of eight packets instead of the usual one.  The  packet  spacing  is normally 2 s; however, the spacing between the first and second packets can be changed with the calldelay command to allow additional time for a modem or ISDN call to complete. This option is valid only with the server  command and type s addresses. It is a recommended option with this command.

prefer:Mark  the server as preferred. All other things being equal, this host will be chosen for synchronization among a set of correctly operating hosts. See the Mitigation Rules and the prefer Keyword page for  further  information. This option is valid only with the server and peer commands.

true:Mark  the  association  to  assume  truechimer status; that is, always survive the selection and clustering algorithms. This option can be used with any association, but is most useful for reference clocks with  large  jitter on  the  serial  port  and  precision pulse-per-second (PPS) signals. Caution: this option defeats the algorithms designed to cast out falsetickers and can allow these sources to set the system clock. This option is valid  only with the server and peer commands.

因为翻译捉急就不翻译了。

例如:以下表示优先连接192.168.7.49这部主机进行时间同步。

server 192.168.7.49 prefer

4. fudge 主机ip stratum 层数

这个配置主要用来设定本机作为为其他机器的时间源的时候,其层数为多少,层数必须在15层之内,一般取10。

四、服务端与客户端配置

1. 服务端配置

driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict :: server .cn.pool.ntp.org
server .asia.pool.ntp.org
server .asia.pool.ntp.org
server 127.0.0.1 fudge 127.0.0.1 stratum includefile /etc/ntp/crypto/pw keys /etc/ntp/keys disable monitor

实际上修改的地方不多,主要是修改了几个时间源:

server .cn.pool.ntp.org
server .asia.pool.ntp.org
server .asia.pool.ntp.org
server 127.0.0.1

前面一个是中国的时间源,后面两个是亚洲洲际的时间源,最后一个表示允许在前面同步失败的情况从本机同步。

然后就是设定本机的时间服务层次为10:

fudge 127.0.0.1 stratum 

2. 客户端配置

driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict :: server host1 includefile /etc/ntp/crypto/pw keys /etc/ntp/keys disable monitor

客户端只改了一个地方,就是将时间源服务器定为host1,这是时间服务主机的域名,也可以直接指定ip地址。

3. 检查状态

以服务端为例子,设置完之后启动服务:

systemctl start ntpd.service

检查是否成功,用ntpstat命令查看同步状态,出现以下状态代表启动成功:

synchronised to NTP server (85.199.214.101) at stratum
time correct to within ms
polling server every s

如果出现异常请等待几分钟,一般等待5-10分钟才能同步。

如果客户端无法连接服务端,请禁用防火墙和selinux后再试试。

五、参考

1. 百度百科:NTP

2. Linux的NTP配置总结

(完)

centos7 ntp服务器配置的更多相关文章

  1. Centos7 Ntp 时间服务器

    Centos7 Ntp 时间服务器 安装环境 [root@m02 ~]# cat /etc/redhat-release CentOS Linux release 7.4.1708 (Core) 安装 ...

  2. CentOS7 NTP 安装配置

    NTP 网络时间协议用来同步网络上不同主机的系统时间.你管理的所有主机都可以和一个指定的被称为 NTP 服务器的时间服务器同步它们的时间.而另一方面,一个 NTP 服务器会将它的时间和任意公共 NTP ...

  3. NTP 服务器配置

    1.yum安装ntp 1 yum install ntp* 2.更新时间 1 ntpdate 202.120.2.101 3.加入任务计划 1 2 crontab -e */10 * * * * nt ...

  4. Linux下NTP服务器配置

    简介 原理 配置ntp服务器 进行同步 一.简介 在计算时间的时候,最准确的计算应该是使用『原子震荡周期』所计算的物理时钟了( Atomic Clock, 也被称为原子钟 ),这也被定义为标准时间(I ...

  5. CentOS之NTP服务器配置

    本文使用CentOS 6.5作为搭建环境 一.服务器端配置 1.安装所需软件包 yum -y install ntp ntpdate---------------------------------- ...

  6. NTP服务器配置

    #/etc/ntp.conf# driftfile /var/lib/ntp/ntp.drift logfile /var/log/ntpd.log statistics loopstats peer ...

  7. centos7 ntp server & samba

    最近公司内部一个需求:必须 Linux建个 ntp server ,并且 Windows可以net time \\ip 访问. 想要解决问题,还得解决前置问题. 服务器不能上网,无法直接访问外部 yu ...

  8. 企业内部在centos7.2系统中必杀技NTP时间服务器及内网服务器时间同步(windows和linux客户端同步)

    网络时间协议NTP(Network Time Protocol)是用于互联网中时间同步的标准互联网协议.NTP的用途是把计算机的时间同步到某些时间标准.目前采用的时间标准是世界协调时UTC(Unive ...

  9. Centos7部署ntp服务器同步时间以及直接将本地时间同步为北京时间

    一.查看配置 查看时区列表: timedatectl list-timezones|grep Asia 查看当前时间: date 查看当前设置: [root@localhost ~]# timedat ...

随机推荐

  1. bacnet ip转MQTT

    迈思德网关最新支持BACNET IP协议,可以将BACNET IP转换为MODBUS.MQTT.OPC等协议,与百度天工,阿里云等无缝对接. 支持AI.AO.DI.DO.AV.DV六个对象的读写.

  2. CtaAlgo vs PyAlgoTrade

    转自知乎:https://zhuanlan.zhihu.com/p/21971854 在Python量化领域,PyAlgoTrade和zipline并列两大策略回测框架的先驱,其中PyAlgoTrad ...

  3. maven打jar到私服

    <dependency> <groupId>fakepath</groupId> <artifactId>wcs-java-sdk</artifa ...

  4. TestCase和TestSuite详解

    一.TestCast和TestSuite概念介绍 TestCase:字面意思,测试用例.为一个或多个方法提供测试方法,一般是一个test. TestSuite:测试集合,即一组测试.一个test su ...

  5. SQL中利用脚本创建database mail.

    SQL中利用脚本创建database mail   编写人:CC阿爸 2014-6-14 多话不讲,请参考以下脚本 use  

  6. VS2017新建windows控制台程序打印中文乱码问题

    最近刚换上VS2017,由于手头又要做个MFC的程序,所以写控制台程序做功能测试,然后发现居然乱码了. 于是用VS2017新建windows控制台应用程序,在main函数种加一句printf(&quo ...

  7. JAVA面向对象编程课程设计——web版斗地主

    一.团队课程设计博客链接 JAVA面向对象编程课程设计--网络版单机斗地主 二.个人负责模块或任务说明 实体类的设计 斗地主规则的实现 人机自动出牌的算法 实现数据库的DAO模式 三.自己的代码提交记 ...

  8. java.nio.charset.UnsupportedCharsetException: cp0

    使用jython调用python,提示console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0. ...

  9. nginx学习1

    访问abc.xxx.com就跳转到www.xxx.com/abc.html server { listen 80; server_name abc.xxx.com;   rewrite ^.*$ ww ...

  10. web开发,click,touch,tap事件浅析

    一.click 和 tap 比较 两者都会在点击时触发,但是在手机WEB端,click会有 200~300 ms,所以请用tap代替click作为点击事件. singleTap和doubleTap 分 ...