ntp服务及其配置

集群中使用NTP服务

简介

之前搭建zookeeper时报了一个错,我以为是ntp的问题,结果不是。这里详细学习一下如何在集群中使用ntp服务。

什么是ntp服务

来自ntp的百度百科

NTP服务器【Network Time Protocol(NTP)】是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。时间按NTP服务器的等级传播。按照离外部UTC源的远近把所有服务器归入不同的Stratum(层)中。

为什么要使用ntp服务

比如定时执行任务,如果时间都不一样的话,会造成混乱。

在集群中使用ntp服务

由一台机器访问外网的ntp服务,然后其他的机器访问和外网同步的那个机器。

ntp时间同步方式

ntp的同步方式有两种:

  1. 使用ntpdate命令直接同步
    使用的命令为ntpdate -u 目标ntp服务的ip,这个命令会马上修改当前主机的时间,但是有些隐患,任务的重复执行。
  2. 使用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的错误。

处理:等待几分钟后,重试一般解决。

其它造成无法成功更新的原因:

  1. 客户端的日期必须要设置正确,不能超出正常时间24小时,不然会因为安全原因被拒绝更新。其次客户端的时区必须要设置好,以确保不会更新成其它时区的时间。
  2. fudge 127.127.1.0 stratum 10 如果是LINUX做为NTP服务器,stratum(层级)的值不能太大,如果要向上级NTP更新可以设成2
  3. LINUX的NTP服务器必须记得将从上级NTP更新的时间从系统时间写到硬件里去 hwclock --systohc
    NTP一般只会同步system clock. 但是如果我们也要同步RTC(hwclock)的话那么只需要把下面的选项打开就可以了SYNC_HWCLOCK=yes
  4. Linux如果开启了NTP服务,则不能手动运行ntpdate更新时间(会报端口被占用),它只能根据/etc/ntp.conf 里server 字段后的服务器地址按一定时间间隔自动向上级NTP服务器更新时间。但是为什么我这里可以呢?
  5. 可以运行命令 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)设置系统时间,这有几个非常明显的问题:

  1. 这样做不安全。ntpdate的设置依赖于ntp服务器的安全性,攻击者可以利用一些软件设计上的缺陷,拿下ntp服务器并令与其同步的服务器执行某些消耗性的任务。由于ntpdate采用的方式是跳变,跟随它的服务器无法知道是否发生了异常(时间不一样的时候,唯一的办法是以服务器为准)。

  2. 这样做不精确。一旦ntp服务器宕机,跟随它的服务器也就会无法同步时间。与此不同,ntpd不仅能够校准计算机的时间,而且能够校准计算机的时钟。

  3. 这样做不够优雅。由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。

NTPD 在和时间服务器的同步过程中,会把 BIOS 计时器的振荡频率偏差——或者说 Local Clock 的自然漂移(drift)——记录下来。这样即使网络有问题,本机仍然能维持一个相当精确的走时。

  • 0
    点赞
  • 0
    收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值