Linux基础:配置ntp时间服务器

1. 概述

大数据产生与处理系统是各种计算设备集群的,计算设备将统一、同步的标准时间用于记录各种事件发生时序,如E-MAIL信息、文件创建和访问时间、数据库处理时间等。大数据系统内不同计算设备之间控制、计算、处理、应用等数据或操作都具有时序性,若计算机时间不同步,这些应用或操作或将无法正常进行。大数据系统是对时间敏感的计算处理系统,时间同步是大数据能够得到正确处理的基础保障,是大数据得以发挥作用的技术支撑。大数据时代,整个处理计算系统内的大数据通信都是通过网络进行。时间同步也是如此,利用大数据的互联网络传送标准时间信息,实现大数据系统内时间同步。网络时间同步协议(NTP)是时间同步的技术基础。

NTP(Network Time Protocol,网络时间协议)是用来使网络中的各个计算机时间同步的一种协议。它的用途是把计算机的时钟同步到世界协调时UTC,其精度在局域网内可达0.1ms,在互联网上绝大多数的地方其精度可以达到1-50ms。它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)进行时间同步,它可以提供高精准度的时间校正,而且可以使用加密确认的方式来防止病毒的协议攻击。

2. Linux Ntp服务器的安装和配置

笔者使用的操作系统是CentOS,其中使用的是yum源进行软件的包的安装和配置,对于Ubuntu操作系统使用apt-get进行软件的安装和配置是一样的。笔者这里使用一台NTP主服务器,两天从服务器同步主服务器的时间进行配置和测试。

2.1 确认当前服务器是否安装Ntp

确认是否已安装ntp

【命令】rpm –qa | grep ntp

若只有ntpdate而未见ntp,则需删除原有ntpdate。如:

ntpdate-4.2.6p5-22.el7_0.x86_64
fontpackages-filesystem-1.44-8.el7.noarch
python-ntplib-0.3.2-1.el7.noarch

删除已安装ntp

【命令】yum –y remove ntpdate-4.2.6p5-22.el7.x86_64

重新安装ntp

【命令】yum –y install ntp

3. 配置NTP服务器

3.1 配置所有NTP节点的IP地址和网关(可以不配置)

【命令】vim /etc/ntp.conf

restrict 11.11.0.25 nomodify notrap nopeer noquery  //当前节点IP地址
restrict 11.11.2.1 mask 255.255.0.0 nomodify notrap  //集群所在网段的网关(Gateway),子网掩码(Genmask)

3.2 配置NTP主服务器

选择一个主节点node24,修改其/etc/ntp.conf

【内容】在server部分添加一下部分,并注释掉server 0 ~ n。主服务器可以不用全部注释,可以保证一个有一个外网的同步时间服务器(比如0.rhel.pool.ntp.org

server 127.127.1.0
Fudge 127.127.1.0 stratum 10
# 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 kod nomodify notrap nopeer noquery
restrict -6 default kod 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 -6 ::1

# Hosts on local network are less restricted.
#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.rhel.pool.ntp.org
#server 1.rhel.pool.ntp.org
#server 2.rhel.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

# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available. 
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

3.3 配置NTP从服务器

配置node25,node26,node27 NTP从服务器。用来同步到node24主服务器,从服务器不断的发送同步请求给主服务器进行时间同步。

【命令】vi /etc/ntp.conf

【内容】在server部分添加一下部分,并注释掉server 0 ~ n。

【内容】在server部分添加如下语句,将server指向主节点。

server node24
Fudge node24 stratum 10
# 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 kod nomodify notrap nopeer noquery
restrict -6 default kod 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 -6 ::1

# Hosts on local network are less restricted.
#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.rhel.pool.ntp.org
#server 1.rhel.pool.ntp.org
#server 2.rhel.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

# Undisciplined Local Clock. This is a fake driver intended for backup
# and when no outside source of synchronized time is available. 
server	node24	# local clock
fudge	node24 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

4. NTP服务器相关命令

4.1 启动ntp服务

ssh node$val "/etc/init.d/ntpd start"

4.2 关闭ntp服务

ssh node$val "/etc/init.d/ntpd stop"

4.3 查看ntp服务器有无和上层ntp连通

ssh node$val "ntpstat"

在这里插入图片描述

4.4 查看ntp服务器与上层ntp的状态

【命令】ntpq -p

在这里插入图片描述

  • remote:本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先
  • refid:参考上一层ntp主机地址
  • st:stratum阶层
  • when:多少秒前曾经同步过时间
  • poll:下次更新在多少秒后
  • reach:已经向上层ntp服务器要求更新的次数
  • delay:网络延迟
  • offset:时间补偿
  • jitter:系统时间与bios时间差

4.5 查看ntpd进程的状态

【命令】watch "ntpq -p"
【终止】按 Ctrl+C 停止查看进程。

在这里插入图片描述

第一列中的字符指示源的质量。星号 ( * ) 表示该源是当前引用。

  • remote:列出源的 IP 地址或主机名。
  • when:指出从轮询源开始已过去的时间(秒)。
  • poll:指出轮询间隔时间。该值会根据本地时钟的精度相应增加。
  • reach:是一个八进制数字,指出源的可存取性。值 377 表示源已应答了前八个连续轮询。
  • offset:是源时钟与本地时钟的时间差(毫秒)。

4.6 设置开机启动

【命令】chkconfig ntpd on

5. /etc/ntp.conf 配置内容

# 1. 先处理权限方面的问题,包括放行上层服务器以及开放局域网用户来源:
restrict default kod nomodify notrap nopeer noquery     <==拒绝 IPv4 的用户
restrict -6 default kod nomodify notrap nopeer noquery  <==拒绝 IPv6 的用户
restrict 220.130.158.71   <==放行 tock.stdtime.gov.tw 进入本 NTP 的服务器
restrict 59.124.196.83    <==放行 tick.stdtime.gov.tw 进入本 NTP 的服务器
restrict 59.124.196.84    <==放行 time.stdtime.gov.tw 进入本 NTP 的服务器
restrict 127.0.0.1        <==底下两个是默认值,放行本机来源
restrict -6 ::1
restrict 192.168.100.0 mask 255.255.255.0 nomodify <==放行局域网用户来源,或者列出单独IP

# 2. 设定主机来源,请先将原本的 [0|1|2].centos.pool.ntp.org 的设定批注掉:
server 220.130.158.71 prefer  <==以这部主机为最优先的server
server 59.124.196.83
server 59.124.196.84

# 3.默认的一个内部时钟数据,用在没有外部 NTP 服务器时,使用它为局域网用户提供服务:
# server    127.127.1.0     # local clock
# fudge     127.127.1.0 stratum 10

# 4.预设时间差异分析档案与暂不用到的 keys 等,不需要更动它:
driftfile /var/lib/ntp/drift
keys      /etc/ntp/keys

6. NTP 时间同步 层次(stratum)

stratum根据上层server的层次而设定(+1)。对于提供network time service provider的主机来说,stratum的设定要尽可能准确。而作为局域网的time service provider,通常将stratum设置为10。

在这里插入图片描述

0层的服务器采用的是原子钟、GPS钟等物理设备,stratum 1与stratum 0 是直接相连的,往后的stratum与上一层stratum通过网络相连,同一层的server也可以交互。ntpd对下层client来说是service server,对于上层server来说它是client。

ntpd根据配置文件的参数决定是要为其他服务器提供时钟服务或者是从其他服务器同步时钟。所有的配置都在/etc/ntp.conf文件中。

在这里插入图片描述

7. 配置NTP服务器相关问题

查看ntp状态时,可能会出现如下所示情况

① unsynchronised time server re-starting polling server every 8 s
② unsynchronised polling server every 8 s

这种情况属于正常,ntp服务器配置完毕后,需要等待5-10分钟才能与/etc/ntp.conf中配置的标准时间进行同步。等一段时间之后,再次使用ntpstat命令查看状态,就会变成如下正常结果:

在这里插入图片描述

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值