ntp服务及其配置
集群中使用NTP服务
简介
之前搭建zookeeper时报了一个错,我以为是ntp的问题,结果不是。这里详细学习一下如何在集群中使用ntp服务。
什么是ntp服务
来自ntp的百度百科:
NTP服务器【Network Time Protocol(NTP)】是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。时间按NTP服务器的等级传播。按照离外部UTC源的远近把所有服务器归入不同的Stratum(层)中。
为什么要使用ntp服务
比如定时执行任务,如果时间都不一样的话,会造成混乱。
在集群中使用ntp服务
由一台机器访问外网的ntp服务,然后其他的机器访问和外网同步的那个机器。
ntp时间同步方式
ntp的同步方式有两种:
- 使用ntpdate命令直接同步
使用的命令为ntpdate -u 目标ntp服务的ip
,这个命令会马上修改当前主机的时间,但是有些隐患,任务的重复执行。 - 使用ntpd服务平滑同步
时间平滑更改,不会让一个时间点在一天内经历两次,这就是NTPD服务方式平滑同步时间,它每次同步时间的偏移量不会太陡,是慢慢来的。但是如果时间相差太大的话,ntpd可能不会正确同步。
安装和配置为其他主机提供服务的ntp服务器
注意一下环境都是在CentOS 7 x64。
在CentOS中可以直接使用yum install ntp
来安装ntp。
查看ntp服务的状态
[root@backup ~]# systemctl status ntpd.service
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
Active: inactive (dead)
配置ntp自动开启
systemctl enable ntpd.service
在配置前,先使用ntpdate手动同步下时间,免得本机与外部时间服务器时间差距太大,让ntpd不能正常同步。
手动先更新一下时间
ntpdate -u 1.cn.pool.ntp.org
配置内网的NPTD服务器/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 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 ::1
# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# 允许内网的其他机器来同步时间
restrict 192.168.1.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 cn.pool.ntp.org
server 0.cn.pool.ntp.org
server 1.cn.pool.ntp.org
server 2.cn.pool.ntp.org
server 3.cn.pool.ntp.org
#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
# 外部时间服务器不可用时,以本地时间作为时间服务
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
# 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
# Disable the monitoring facility to prevent amplification attacks using ntpdc
# monlist command when default restrict does not include the noquery flag. See
# CVE-2013-5211 for more details.
# Note: Monitoring will not be disabled with the limited restriction flag.
disable monitor
重启/打开ntp服务
systemctl start ntpd.service
查询同步情况
启动后,一般需要5-10分钟左右的时候才能与外部时间服务器开始同步时间。可以通过命令查询NTPD服务情况。
[root@backup ~]# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
ntp4.itcomplian 5.103.128.88 3 u 2 64 1 342.482 44.556 0.000
marla.ludost.ne .INIT. 16 u - 64 0 0.000 0.000 0.000
ntp.wdc1.us.lea .INIT. 16 u - 64 0 0.000 0.000 0.000
ntp1.ams1.nl.le .INIT. 16 u - 64 0 0.000 0.000 0.000
ntp1.flashdance .INIT. 16 u - 64 0 0.000 0.000 0.000
配置客户端的ntp
内网其他设备作为NTP的客户端配置,相对就比较简单,而且所有设备的配置都相同。
首先需要安装NTPD服务,然后配置为自启动(与NTP-Server完全一样)。然后找其中一台配置/etc/ntp.conf文件,配置完成验证通过后,拷贝到其他客户端机器,直接使用即可。
# 配置时间服务器为本地的时间服务器
server 192.168.1.135
restrict 192.168.1.135 nomodify notrap noquery
server 127.127.1.0 # local clock
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
在请求服务器之前可以先用ntpdate同步一下时间。
下面两个部分是参考ntp服务的细节全解析
可能出现的问题
错误1:ntpdate -u ip -> no server suitable for synchronization found
判断:在ntp客户端用ntpdate –d serverIP查看,发现有“Server dropped: strata too high”的错误,并且显示“stratum 16”。而正常情况下stratum这个值得范围是“0~15”。
原因:NTP server还没有和其自身或者它的server同步上。在ntp server上重新启动ntp服务后,ntp server自身或者与其server的同步的需要一个时间段,这个过程可能是5分钟,在这个时间之内在客户端运行ntpdate命令时会产生no server suitable for synchronization found的错误。
处理:等待几分钟后,重试一般解决。
其它造成无法成功更新的原因:
- 客户端的日期必须要设置正确,不能超出正常时间24小时,不然会因为安全原因被拒绝更新。其次客户端的时区必须要设置好,以确保不会更新成其它时区的时间。
- fudge 127.127.1.0 stratum 10 如果是LINUX做为NTP服务器,stratum(层级)的值不能太大,如果要向上级NTP更新可以设成2
- LINUX的NTP服务器必须记得将从上级NTP更新的时间从系统时间写到硬件里去 hwclock --systohc
NTP一般只会同步system clock. 但是如果我们也要同步RTC(hwclock)的话那么只需要把下面的选项打开就可以了SYNC_HWCLOCK=yes
- Linux如果开启了NTP服务,则不能手动运行ntpdate更新时间(会报端口被占用),它只能根据/etc/ntp.conf 里server 字段后的服务器地址按一定时间间隔自动向上级NTP服务器更新时间。但是为什么我这里可以呢?
- 可以运行命令 ntpstat 查看每次更新间隔如:
[root@backup ~]# ntpstat
synchronised to NTP server (193.228.143.12) at stratum 3
time correct to within 701 ms
polling server every 64 s
[root@backup ~]# ntpdate -u 1.cn.pool.ntp.org
23 Apr 14:37:02 ntpdate[3279]: adjust time server 61.216.153.105 offset 0.051112 sec
[root@backup ~]# date
Sun Apr 23 14:37:13 CST 2017
[root@backup ~]# systemctl restart ntpd.service
[root@backup ~]# ntpdate -u 1.cn.pool.ntp.org
23 Apr 14:37:45 ntpdate[3292]: adjust time server 61.216.153.107 offset 0.032012 sec
[root@backup ~]# ntpdate -u ntp.sjtu.edu.cn 202.120.2.101
23 Apr 14:38:33 ntpdate[3293]: adjust time server 202.120.2.101 offset 0.004910 sec
[root@backup ~]# systemctl status ntpd.service
● ntpd.service - Network Time Service
Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
Active: active (running) since Sun 2017-04-23 14:37:33 CST; 1min 9s ago
Process: 3290 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 3291 (ntpd)
CGroup: /system.slice/ntpd.service
└─3291 /usr/sbin/ntpd -u ntp:ntp -g
Apr 23 14:37:33 backup ntpd[3291]: Listen and drop on 1 v6wildcard :: UDP 123
Apr 23 14:37:33 backup ntpd[3291]: Listen normally on 2 lo 127.0.0.1 UDP 123
Apr 23 14:37:33 backup ntpd[3291]: Listen normally on 3 ens33 192.168.31.100 UDP 123
Apr 23 14:37:33 backup ntpd[3291]: Listen normally on 4 lo ::1 UDP 123
Apr 23 14:37:33 backup ntpd[3291]: Listen normally on 5 ens33 fe80::b307:e4a5:944a:4fd UDP 123
Apr 23 14:37:33 backup ntpd[3291]: Listening on routing socket on fd #22 for interface updates
Apr 23 14:37:33 backup ntpd[3291]: 0.0.0.0 c016 06 restart
Apr 23 14:37:33 backup ntpd[3291]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
Apr 23 14:37:33 backup ntpd[3291]: 0.0.0.0 c011 01 freq_not_set
Apr 23 14:37:39 backup ntpd[3291]: 0.0.0.0 c514 04 freq_mode
[root@backup ~]# ntpstat
synchronised to NTP server (193.228.143.12) at stratum 3 // 本NTP服务器层次为2,已向193.228.143.12 NTP同步过
time correct to within 701 ms // 时间校正到相差701ms之内
polling server every 64 s // 每64秒会向上级NTP轮询更新一次时间
ntpd、ntpdate的区别
下面是网上关于ntpd与ntpdate区别的相关资料。如下所示所示:
使用之前得弄清楚一个问题,ntpd与ntpdate在更新时间时有什么区别。ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,也正如此两者不可同时运行。
时钟的跃变,对于某些程序会导致很严重的问题。许多应用程序依赖连续的时钟——毕竟,这是一项常见的假定,即,取得的时间是线性的,一些操作,例如数据库事务,通常会地依赖这样的事实:时间不会往回跳跃。不幸的是,ntpdate调整时间的方式就是我们所说的”跃变“:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,这有几个非常明显的问题:
这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。
这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。
这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。
NTPD 在和时间服务器的同步过程中,会把 BIOS 计时器的振荡频率偏差——或者说 Local Clock 的自然漂移(drift)——记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的走时。
ntp服务及其配置的更多相关文章
- Debian以下的ntp服务(ntpdate)的安装
/********************************************************************* * Author : Samson * Date ...
- 时间同步ntp服务的安装与配置(作为客户端的配置
在linux环境下,我们不仅可以自己设置时间,也可以对系统进行时间的同步,比如同步时间到某台物理机上或虚拟机,皆可!接下来我们就以同步时间到某台物理机为例, 一起学习学习. 1.配置本地yum源(挂载 ...
- 配置ntp服务
配置ntp服务(hadoop搭建可参考) 一:修改选定的服务器的本地时间 date -s '2016-10-07 16:29:30' +'%F %T' //需要设置的时间 二:修改后将时间写入到硬件时 ...
- NTP服务配置
一.NTP简介 在计算机的世界里,时间非常地重要,例如对于火箭发射这种科研活动,对时间的统一性和准确性要求就非常地高,是按照A这台计算机的时间,还是按照B这台计算机的时间?NTP就是用来解决这个问题的 ...
- 配置NTP服务ntpd/ntp.conf(搭建Hadoop集群可参考)
本文拟定是在一个局域网内(比如一个Hadoop集群)设定一台NTP服务器作为整个网络的标准时间参考,使用网络(集群)内的所有机器保持时间一致!以下是详细的操作步骤: 1. 修改选定的服务器的本地时间 ...
- 配置Linux 11G R2 RAC NTP服务
安装Oracle 11g RAC时,我们需要配置ntp服务.在使用虚拟机的情况下对于时钟同步方式的配置有很多种方式,可以使用vmware自带的时钟同步功能,也可以直接将本地的一个节点用作时间服务器.本 ...
- centos6.3 配置NTP服务
NTP简介: NTP(Network Time Protocol)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源做同步化,它可以提供高精准度的时间校正.本例讲解如何在CentOS ...
- CentOS7+CDH5.14.0安装全流程记录,图文详解全程实测-4配置NTP服务
配置NTP服务.标准的做法是配置NTP服务器,但是这里为了方便就用简化的方式配置了. 这个在安装初期,不是必须的,只要保证各机器的时间同步就行,使用如下命令可以查看时间是否同步: [root@cdh1 ...
- 基于【CentOS-7+ Ambari 2.7.0 + HDP 3.0】搭建HAWQ数据仓库——安装配置NTP服务,保证集群时间保持同步
一.所有节点上使用yum安装配置NTP服务yum install ntp -y 二.选定一台节点作为NTP server, 192.168.58.11修改/etc/ntp.conf vim /etc/ ...
随机推荐
- Codeforces 1027F. Session in BSU
题目直通车:Codeforces 1027F. Session in BSU 思路: 对第一门考试,使用前一个时间,做标记,表示该时间已经用过,并让第一个时间指向第二个时间,表示,若之后的考试时间和当 ...
- 6.NFC之非NDEF格式
先看流程图 使用步骤: 第一步:声明权限 <!-- 允许应用程序使用NFC功能 --> <uses-permission android:name="android.per ...
- Delphi 使用 SPcomm 调试串口程序出现总是在程序断开的时候,才发送指令的问题。
问题如上, 在与嵌入式程序串口程序通讯的时候, 总是出现如上问题, 造成的原因把下面的True改成false就可以了. 下图Spcomm的属性页,几个True全改成False再试试
- UIWebView to view self signed websites (No private api, not NSURLConnection) - is it possible?
What it actually does is to intercept the UIWebView to launch a NSURLConnection to allow the server ...
- 理解 VMWare的3种网络模型 z
在说到VMware的网络模型之前,先说一下VMware的几个虚拟设备: ■ VMnet0:这是VMware用于虚拟桥接网络下的虚拟交换机: ■ VMnet1:这是VMware用于虚拟Host-Only ...
- ubuntu 安装 regex模块时 fatal error: Python.h: No such file or directory
原因是 python-dev包没有安装 根据Py2还是py3 sudo apt-get install python-dev 或者 sudo apt-get install python3-dev 安 ...
- DotnetBrowser高级教程-(4)使用MVC框架5-使用视图
mvc框架理所当然的要支持view了,我们看下前面上传文件的地方,在展示页面时,我们使用了如下的代码: public string UploadImgPage() { return "< ...
- 删除windows服务命令
打开命令框:输入sc delete 服务名 例如删除elasticsearch-service-x64服务 sc delete elasticsearch-service-x64
- 【Hadoop】Hadoop MR Job工作流引擎
Apache Oozie 是用于 Hadoop 平台的一种工作流调度引擎.该框架(如图 1 所示)使用 Oozie 协调器促进了相互依赖的重复工作之间的协调,您可以使用预定的时间或数据可用性来触发 A ...
- Java高级特性—并发包
1). java并发包介绍 JDK5.0 以后的版本都引入了高级并发特性,大多数的特性在java.util.concurrent 包中,是专门用于多线程发编程的, 主要包含原子量.并发集合.同步器.可 ...