centos7 ntp服务器配置

一、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的时间服务器是整个系统的基础,这种阶梯式的架构示意图如下图所示:

d0c8a786c9177f3e95090eb873cf3bc79f3d5642副本

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

二、架构及安装

1. 架构

未命名文件

如上图所示,对于一些服务依赖于时间的集群(如hadoop集群),需要有一部主机作为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 ::1

server 0.cn.pool.ntp.org 
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 127.0.0.1 

fudge 127.0.0.1 stratum 10

includefile /etc/ntp/crypto/pw

keys /etc/ntp/keys

disable monitor

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

server 0.cn.pool.ntp.org 
server 1.asia.pool.ntp.org
server 2.asia.pool.ntp.org
server 127.0.0.1

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

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

fudge 127.0.0.1 stratum 10

2. 客户端配置

driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1 
restrict ::1

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 2 
   time correct to within 155 ms
    polling server every 64 s

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

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

五、参考

1. 百度百科:NTP

2. Linux的NTP配置总结

(完)

猜你喜欢

转载自www.cnblogs.com/harrymore/p/9566229.html