Linux NTP时间同步服务搭建

Linux NTP时间同步服务搭建

ntp常用到的命名
查看ntp服务器同步状态 : ntpstat
与ntp服务器时间同步: ntpdate -u 192.168.1.21
查看时间同步状态:  ntpq -p

NTP服务器部署安装

1. NTP服务器安装

[root@ntp-server ~]# rpm -qa ntp
[root@ntp-server ~]# yum install -y ntp
[root@ntp-server ~]# rpm -qa ntp
ntp-4.2.6p5-28.el7.centos.x86_64
[root@ntp-server ~]# 

2. 配置NTP服务

  • 默认配置含意
#系统时间与BIOS事件的偏差记录
driftfile /var/lib/ntp/drift

restrict default kod nomodify notrap nopeer noquery    # 拒绝所有IPv4的client连接此NTP服务器
restrict -6 default kod nomodify notrap nopeer noquery    # 拒绝所有IPv6的client连接此NTP服务器

restrict 127.0.0.1    # 放行本机localhost对NTP服务的访问
restrict -6 ::1

# Hosts on local network are less restricted.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap    # 放行192.168.1.0网段主机与NTP服务器进行时间同步

# 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

#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

# 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

-------------参数解释--------------------------
◆ 利用restrict来管理权限控制
===restrict选项格式===
restrict [ 客户端IP ]  mask  [ IP掩码 ]  [参数]
    default则表示对所有的计算机进行控制
    gnore:拒绝连接到NTP服务器
    nomodiy: 客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
    noquery: 不提供客户端的时间查询
    notrap: 不提供trap远程登录功能,trap服务是一种远程时间日志服务。
    notrust: 客户端除非通过认证,否则该客户端来源将被视为不信任子网 。
    nopeer: 提供时间服务,但不作为对等体。
    kod: 向不安全的访问者发送Kiss-Of-Death报文。

◆ 利用server 设定上层NTP服务器,格式如下:
===server选项格式===
server host  [ key n ] [ version n ] [ prefer ] [ mode n ] [ minpoll n ] [ maxpoll n ] [ iburst ]
其中host是上层NTP服务器的IP地址或域名,随后所跟的参数解释如下所示:
	key: 表示所有发往服务器的报文包含有秘钥加密的认证信息,n是32位的整数,表示秘钥号。
	version: 表示发往上层服务器的报文使用的版本号,n默认是3,可以是1或者2。
	prefer: 如果有多个server选项,具有该参数的服务器有限使用。
	mode: 指定数据报文mode字段的值。
	minpoll: 指定与查询该服务器的最小时间间隔为2的n次方秒,n默认为6,范围为4-14。
	maxpoll:  指定与查询该服务器的最大时间间隔为2的n次方秒,n默认为10,范围为4-14。
	iburst: 当初始同步请求时,采用突发方式接连发送8个报文,时间间隔为2秒。
参数主要如下:
  perfer:表示优先级最高
  burst :当一个运程NTP服务器可用时,向它发送一系列的并发包进行检测。
  iburst :当一个运程NTP服务器不可用时,向它发送一系列的并发包进行检测。
  
===层次(stratum)===
stratum根据上层server的层次而设定(+1)。
对于提供network time service provider的主机来说,stratum的设定要尽可能准确。
而作为局域网的time service provider,通常将stratum设置为10
0层的服务器采用的是原子钟、GPS钟等物理设备,stratum 1与stratum 0 是直接相连的,ntpd对下层client来说是service server,对于上层server来说它是client。

  • 调整NTP配置文件

# Hosts on local network are less restricted.

# 允许局域网内设备与这台服务器进行同步时间.但是拒绝让他们修改服务器上的时间,
# 即放行局域网用户来源,或者列出单独IP
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
restrict 192.168.10.0 mask 255.255.255.0 nomodify  notrap
restrict 192.168.20.0 mask 255.255.255.0 nomodify  notrap
restrict 172.16.10.0 mask 255.255.255.0 nomodify  notrap
restrict 192.168.100.0 mask 255.255.255.0 nomodify  notrap

# 允许上层时间服务器修改本机时间
restrict times.aliyun.com nomodify
restrict 210.72.145.44 nomodify
restrict cn.pool.ntp.org nomodify

# 定义要同步的时间服务器
server times.aliyun.com iburst prefer     # prefer表示为优先,表示本机优先同步该服务器时间,
server 210.72.145.44 iburst         # iburst当一个运程NTP服务器不可用时,向它发送一系列的并发包进行检测
server cn.pool.ntp.org iburst

logfile /var/log/ntpstats/ntpd.log    # 定义ntp日志目录
pidfile  /var/run/ntp.pid        # 定义pid路径

interface listen 192.168.1.21    # 添加监听,使时间同步生效

#server 127.127.1.0
#Fudge 127.127.1.0 stratum 10

keys /etc/ntp/keys

  • ntp常用服务器:

中国国家授时中心:210.72.145.44 或者 cn.ntp.org.cn
NTP服务器(上海) :ntp.api.bz

3. 启动NTP服务器,并实现clock时间与system时间同步

# 如果计划任务有时间同步,先注释,两种用法会冲突。
[root@ntp-server ~]# crontab -e
# time sync by oldboy at 2010-2-1
#*/1 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1
[root@ntp-server ~]#
  • 启动ntp服务器
[root@ntp-server ~]# chkconfig ntp on  # 加入开机自启
[root@ntp-server ~]# service ntpd start   # systemctl start ntpd

  • 实现clock时间与system时间同步,配置/etc/sysconfig/ntpd文件

ntp服务默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步

[root@ntp-server ~]$ vim /etc/sysconfig/ntpd
# 实现硬件时间与系统时间同步
# Command line options for ntpd
# OPTIONS="-g"
OPTIONS="-u ntp:ntp -p /var/run/ntp.pid -g"
SYNC_HWCLOCK=yes
 
 
# 确认本地NTP与上层NTP服务器是否联通
[root@ntp-server ~]$ ntpstat 
synchronised to NTP server (120.25.115.19) at stratum 3     #本NTP服务器层次为10,已向120.25.115.19 NTP同步过
time correct to within 147 ms    # 时间校正到相差147ms之内
polling server every 64 s        # 每64秒会向上级NTP轮询更新一次时间
[root@ntp-server ~]$

# 查看本地NTP需进行同步的公网NTP服务器状态
[root@ntp-server ~]$ ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*120.25.115.19   10.137.53.7      2 u   34   64  377   29.773  -84.201  27.169
 210.72.145.44   .INIT.          16 u    -  512    0    0.000    0.000   0.000
+119.28.183.184  100.122.36.196   2 u   31   64  233   60.095  -71.293  42.574
[root@ntp-server ~]$ 

remote :本地主机所连接的上层NTP服务器,最左边的符号如下:
    如果有[*] 代表目前正在使用当中的上层NTP服务器。
    如果有[+] 代表也有连上上层NTP服务器,可以作为提高时间更新的候选NTP服务器
    如果有[-] 代表同步的该NTP服务器被认为是不合格的NTP Server
    如果有[x] 代表同步的外网NTP服务器不可用
refid :指的是给上层NTP服务器提供时间校对的服务器。 
St:上层NTP服务器的级别。
When: 上一次与上层NTP服务器进行时间校对的时间(单位:s)
Poll :本地主机与上层NTP服务器进行时间校对的周期(单位:s)
reach:已经向上层 NTP 服务器要求更新的次数 
delay:网络传输过程当中延迟的时间,单位为 10^(-6) 秒 
offset:时间补偿的结果,单位为10^(-6) 秒
jitter:Linux 系统时间与 BIOS 硬件时间的差异时间, 单位为 10^(-6) 秒。

客户端与ntp服务器时间同步部署

Linux客户端与ntp服务器时间同步

yum install ntp ntpdate -y
# 客户机要等几分钟再与新启动的ntp服务器进行时间同步,否则会提示no server suitable for synchronization found错误,如果还是有报错,重启下ntp服务器
[root@mysql-slave /]# ntpdate 192.168.1.21
 3 Jul 20:01:01 ntpdate[1840]: no server suitable for synchronization found
[root@mysql-slave /]# ntpdate 192.168.1.21
 3 Jul 21:46:48 ntpdate[1853]: step time server 192.168.1.21 offset 6225.168941 sec
[root@mysql-slave /]#

# 如果与时间同步服务器同步后,是12小时计时法可手动创建链接
[root@kvm-node1 /etc/yum.repos.d]# ntpdate -u 192.168.1.21
 3 Jul 09:49:57 ntpdate[9303]: adjust time server 192.168.1.21 offset 0.009749 sec
[root@kvm-node1 /etc/yum.repos.d]# date
Wed Jul  3 09:50:01 EDT 2019
[root@kvm-node1 /etc/yum.repos.d]# ln -sf /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
[root@kvm-node1 /etc/yum.repos.d]# date
Wed Jul  3 21:51:34 CST 2019
[root@kvm-node1 /etc/yum.repos.d]# 

  • 两种方法实现客户端与ntp服务器时间同步。 第一种配置周期性任务计划时间同步,第二种方法配置ntp服务
  1. 配置周期性任务计划
# 每分钟同步一次
[root@kvm-node1 ~]# crontab -e
*/1 * * * * /usr/sbin/ntpdate -u 192.168.1.21 &> /dev/null;/sbin/hwclock -w &> /dev/null
  1. 客户端配置ntp服务
# 修改同步的时间服务器,别的不用修改,重启ntpd,一段时间后自动同步
[root@kvm-node1 ~]# vim /etc/ntp.conf 
server 192.168.1.21  prefer
restrict 192.168.1.21 nomodify notrap noquery

Windows客户端与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)——记录下来。

这样即使网络有问题,本机仍然能维持一个相当精确的走时。

可能的报错

  • 客户机时间同步ntpdate时提示no server suitable for synchronization found错误
    • 查看ntp服务器是否正常同步,ntp服务器启动后要过段时间在进行同步
  • ntp服务器报错:unsynchronised
    • 在服务器上启用了ntpd服务,显示服务正常运行,但实际上时间同步并没有生效。解决办法:在/etc/ntp.conf 中添加 interface listen 本机ip
  • 1
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值