Chrony 时间与NTP同步详解

目录

Chrony服务器和客户端配置

一、 简介

二、安装与配置

三、使用

四、chronyc 命令

五、timedatectl 命令

六、授时中心

七、系统时钟与硬件时钟之间同步

八、常见时区

 九、实例

 十、chronyc sources 输出结果解析​编辑

 NTP 服务器和客户端配置

一、NTP服务器

二、NTP客户端


Chrony服务器和客户端配置

一、 简介

Chrony是一个开源自由的网络时间协议 NTP 的客户端和服务器软软件。它能让计算机保持系统时钟与时钟服务器(NTP)同步,因此让你的计算机保持精确的时间,Chrony也可以作为服务端软件为其他计算机提供时间同步服务。

Chrony由两个程序组成,分别是chronyd和chronyc

chronyd是一个后台运行的守护进程,用于调整内核中运行的系统时钟和时钟服务器同步。它确定计算机增减时间的比率,并对此进行补偿。
chronyc提供了一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。

NTP 是网络时间协议(Network Time Protocol)的简称,通过 udp 123 端口进行网络时钟同步。
RHEL7中默认使用chrony作为时间服务器,也支持NTP,需要额外安装。
NTP与chrony不能同时存在,只能用其中一个,并将另一个mask掉。

二、安装与配置

1,rpm查询不到可安装服务
#yum -y install chrony
#systemctl enable chronyd
#systemctl start chronyd


2,
rpm -qi chrony
rpm -ql chrony
vim /etc/chrony.cong(修改配置项参考一下示例)
systemctl enable chronyd
systemctl start chronyd

chrony.conf 默认配置

Chrony的配置文件是/etc/chrony.conf

# 使用 pool.ntp.org 项目中的公共服务器。以server开,理论上想添加多少时间服务器都可以。
# 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
 
# 根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,在系统重启后为系统做出最佳时间补偿调整。
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift
 
# 如果系统时钟的偏移量大于1秒,则允许系统时钟在前三次更新中步进。
# Allow the system clock to be stepped in the first three updates if its offset is larger than 1 second.
makestep 1.0 3
 
# 启用实时时钟(RTC)的内核同步。
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync
 
# 通过使用 hwtimestamp 指令启用硬件时间戳
# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *
 
# Increase the minimum number of selectable sources required to adjust the system clock.
#minsources 2
 
# 指定 NTP 客户端地址,以允许或拒绝连接到扮演时钟服务器的机器
# Allow NTP client access from local network.
#allow 192.168.0.0/16
 
# Serve time even if not synchronized to a time source.
#local stratum 10
 
# 指定包含 NTP 身份验证密钥的文件。
# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys
 
# 指定日志文件的目录。
# Specify directory for log files.
logdir /var/log/chrony
 
# 选择日志文件要记录的信息。
# Select which information is logged.
#log measurements statistics tracking

三、使用

1.服务端配置

chrony.conf 修改两处

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server s1a.time.edu.cn iburst
server ntp.aliyun.com iburst
server ntp.ntsc.ac.cn  iburst
server ntp1.aliyun.com iburst

# Allow NTP client access from local network.(此处不指定即放行全部)
allow 192.168.0.0/16


设置开机启动,重启服务

systemctl enable chronyd
systemctl restart chronyd
 
# 查看时间同步状态
timedatectl status
# 开启网络时间同步
timedatectl set-ntp true

2.客户端配置

chrony.conf 修改两处

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 192.168.8.5 iburst
 
# Allow NTP client access from local network.
allow 192.168.8.5



设置开机启动,重启服务
systemctl enable chronyd
systemctl restart chronyd

查看时间同步状态

timedatectl status

开启网络时间同步

timedatectl set-ntp true

四、chronyc 命令

查看 ntp_servers
chronyc sources -v


查看 ntp_servers 状态
chronyc sourcestats -v

查看 ntp_servers 是否在线
chronyc activity -v

查看 ntp 详细信息
chronyc tracking -v

强制同步下系统时钟
chronyc -a makestep

五、timedatectl 命令

查看日期时间、时区及 NTP 状态
timedatectl

查看时区列表
timedatectl list-timezones
timedatectl list-timezones |  grep  -E "Asia/S.*"

修改时区
timedatectl set-timezone Asia/Shanghai

修改日期时间(可以只修改其中一个)
timedatectl set-time "2021-04-15 15:50:20"

开启 NTP
timedatectl set-ntp true/flase

六、授时中心

210.72.145.44 国家授时中心
ntp.aliyun.com 阿里云
s1a.time.edu.cn 北京邮电大学
s1b.time.edu.cn 清华大学
s1c.time.edu.cn 北京大学
s1d.time.edu.cn 东南大学
s1e.time.edu.cn 清华大学
s2a.time.edu.cn 清华大学
s2b.time.edu.cn 清华大学
s2c.time.edu.cn 北京邮电大学
s2d.time.edu.cn 西南地区网络中心
s2e.time.edu.cn 西北地区网络中心
s2f.time.edu.cn 东北地区网络中心
s2g.time.edu.cn 华东南地区网络中心
s2h.time.edu.cn 四川大学网络管理中心
s2j.time.edu.cn 大连理工大学网络中心
s2k.time.edu.cn CERNET桂林主节点
s2m.time.edu.cn 北京大学
ntp.sjtu.edu.cn 202.120.2.101 上海交通大学

七、系统时钟与硬件时钟之间同步

设置硬件时钟
-w,--systohc
hwclock -w

设置系统时钟
-s, --hctosys
hwclock -s


修改配置文件方式
vim /etc/sysconfig/ntpd
将系统时间写入BIOS,与 hwclock -w 效果相同
SYNC_HWCLOCK=yes

八、常见时区

(1)UTC 整个地球分为二十四时区,每个时区都有自己的本地时间。在国际无线电通信场合,为了统一起见,使用一个统一的时间,称为通用协调时(UTC, Universal Time Coordinated)。

(2)GMT  格林威治标准时间 (Greenwich Mean Time)指位于英国伦敦郊区的×××格林尼治天文台的标准时间,因为本初子午线被定义在通过那里的经线。(UTC与GMT时间基本相同,本文中不做区分)

(3)CST  中国标准时间 (China Standard Time)
GMT + 8 = UTC + 8 = CST

(4)DST夏令时(Daylight Saving Time) 指在夏天太阳升起的比较早时,将时间拨快一小时,以提早日光的使用。(中国不使用)

 九、实例

[root@rhel7.9 ~]# timedatectl set-time "2020-02-23 12:23:01"       <<========设置系统时间,因为开启了时间同步所以报错
Failed to set time: Automatic time synchronization is enabled
[root@rhel7.9 ~]# systemctl stop chronyd
[root@rhel7.9 ~]# timedatectl set-time "2020-02-23 12:23:01"         <<==========stop chronyd 后修改系统时间,报错依旧
Failed to set time: Automatic time synchronization is enabled
[root@rhel7.9 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: inactive (dead) since 四 2021-04-15 15:45:37 CST; 21s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 13722 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 13716 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 13720 (code=exited, status=0/SUCCESS)
 
4月 15 15:40:54 rhel7.9 systemd[1]: Starting NTP client/server...
4月 15 15:40:54 rhel7.9 chronyd[13720]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
4月 15 15:40:54 rhel7.9 chronyd[13720]: Frequency 8.664 +/- 0.118 ppm read from /var/lib/chrony/drift
4月 15 15:40:54 rhel7.9 systemd[1]: Started NTP client/server.
4月 15 15:41:02 rhel7.9 chronyd[13720]: Selected source 119.28.206.193
4月 15 15:43:16 rhel7.9 chronyd[13720]: Source 124.108.20.1 replaced with 84.16.67.12
4月 15 15:45:37 rhel7.9 systemd[1]: Stopping NTP client/server...
4月 15 15:45:37 rhel7.9 chronyd[13720]: chronyd exiting
4月 15 15:45:37 rhel7.9 systemd[1]: Stopped NTP client/server.
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# date
2021年 04月 15日 星期四 15:46:13 CST
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# timedatectl set-ntp false
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# timedatectl set-time "2020-02-23 12:23:01"
[root@rhel7.9 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:chronyd(8)
           man:chrony.conf(5)
 
4月 15 15:40:34 rhel7.9 systemd[1]: Stopped NTP client/server.
4月 15 15:40:54 rhel7.9 systemd[1]: Starting NTP client/server...
4月 15 15:40:54 rhel7.9 chronyd[13720]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
4月 15 15:40:54 rhel7.9 chronyd[13720]: Frequency 8.664 +/- 0.118 ppm read from /var/lib/chrony/drift
4月 15 15:40:54 rhel7.9 systemd[1]: Started NTP client/server.
4月 15 15:41:02 rhel7.9 chronyd[13720]: Selected source 119.28.206.193
4月 15 15:43:16 rhel7.9 chronyd[13720]: Source 124.108.20.1 replaced with 84.16.67.12
4月 15 15:45:37 rhel7.9 systemd[1]: Stopping NTP client/server...
4月 15 15:45:37 rhel7.9 chronyd[13720]: chronyd exiting
4月 15 15:45:37 rhel7.9 systemd[1]: Stopped NTP client/server.
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# timedatectl status          <<============ 显示当前系统和RTC设置
      Local time: 日 2020-02-23 12:23:39 CST
  Universal time: 日 2020-02-23 04:23:39 UTC
        RTC time: 日 2020-02-23 04:23:39
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@rhel7.9 ~]# timedatectl set-ntp true
[root@rhel7.9 ~]# timedatectl status      
      Local time: 日 2020-02-23 12:24:14 CST
  Universal time: 日 2020-02-23 04:24:14 UTC
        RTC time: 日 2020-02-23 04:24:14
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@rhel7.9 ~]# systemctl start chronyd
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# timedatectl status
      Local time: 四 2021-04-15 15:48:52 CST
  Universal time: 四 2021-04-15 07:48:52 UTC
        RTC time: 日 2020-02-23 04:24:44
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
[root@rhel7.9 ~]# timedatectl set-time "2020-02-23 12:23:01"
Failed to set time: Automatic time synchronization is enabled
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# timedatectl set-ntp false          <<============= 禁用基于NTP的网络时间同步           
[root@rhel7.9 ~]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; disabled; vendor preset: enabled)
   Active: inactive (dead)
     Docs: man:chronyd(8)
           man:chrony.conf(5)
 
2月 23 12:24:12 rhel7.9 systemd[1]: Starting NTP client/server...
2月 23 12:24:12 rhel7.9 chronyd[15212]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
2月 23 12:24:12 rhel7.9 chronyd[15212]: Frequency 8.646 +/- 0.520 ppm read from /var/lib/chrony/drift
2月 23 12:24:12 rhel7.9 systemd[1]: Started NTP client/server.
2月 23 12:24:18 rhel7.9 chronyd[15212]: Selected source 84.16.67.12
2月 23 12:24:18 rhel7.9 chronyd[15212]: System clock wrong by 36041047.965352 seconds, adjustment started
4月 15 15:48:26 rhel7.9 chronyd[15212]: System clock was stepped by 36041047.965352 seconds
4月 15 15:51:05 rhel7.9 chronyd[15212]: chronyd exiting
4月 15 15:51:05 rhel7.9 systemd[1]: Stopping NTP client/server...
4月 15 15:51:05 rhel7.9 systemd[1]: Stopped NTP client/server.
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# timedatectl set-time "2020-02-23 12:23:01"      <<=========== 再次设置时间成功
[root@rhel7.9 ~]# timedatectl set-ntp true      <<============ 启用基于NTP的网络时间同步
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# systemctl status chronyd                  
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since 日 2020-02-23 12:23:25 CST; 4s ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 16110 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 16103 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 16105 (chronyd)
    Tasks: 1
   CGroup: /system.slice/chronyd.service
           └─16105 /usr/sbin/chronyd
 
2月 23 12:23:25 rhel7.9 systemd[1]: Starting NTP client/server...
2月 23 12:23:25 rhel7.9 chronyd[16105]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECHASH +IPV6 +DEBUG)
2月 23 12:23:25 rhel7.9 chronyd[16105]: Frequency 8.645 +/- 0.714 ppm read from /var/lib/chrony/drift
2月 23 12:23:25 rhel7.9 systemd[1]: Started NTP client/server.
[root@rhel7.9 ~]# 
[root@rhel7.9 ~]# date
2021年 04月 15日 星期四 15:52:14 CST

在使用timedatectl 设置系统时间时间必须停止NTP时间同步

 十、chronyc sources 输出结果解析

M
这表示信号源的模式。^表示服务器,=表示对等方,#表示本地连接的参考时钟。

S
此列指示源的状态。
* 表示chronyd当前同步到的源。
+ 表示可接受的信号源,与选定的信号源组合在一起。
- 表示被合并算法排除的可接受源。
? 指示已失去连接性或其数据包未通过所有测试的源。它也显示在启动时,直到从中至少收集了3个样本为止。
x 表示chronyd认为是虚假行情的时钟(即,其时间与大多数其他来源不一致)。
〜 表示时间似乎具有太多可变性的来源。


Name/IP address
这显示了源的名称或IP地址,或参考时钟的参考ID。


Stratum
这显示了来源的层,如其最近收到的样本中所报告的那样。层1表示一台具有本地连接的参考时钟的计算机。与第1层计算机同步的计算机位于第2层。与第2层计算机同步的计算机位于第3层,依此类推。


Poll
这显示轮询源的速率,以秒为单位的时间间隔的以2为底的对数。因此,值为6表示每64秒进行一次测量。chronyd会根据当前情况自动更改轮询速率。

Reach
这显示了源的可达性寄存器以八进制数字打印。寄存器有8位,并在每个从源接收或丢失的数据包上更新。值377表示从最后八次传输中收到了对所有用户的有效答复。

LastRx
此列显示多长时间前从来源接收到了最后一个好的样本(在下一列中显示)。未通过某些测试的测量将被忽略。通常以秒为单位。字母m,h,d或y表示分钟,小时,天或年。

Last sample
此列显示上次测量时本地时钟与源之间的偏移。方括号中的数字表示实际测得的偏移量。可以用ns(表示纳秒),us (表示微秒),ms(表示毫秒)或s(表示秒)作为后缀。方括号左侧的数字表示原始测量值,已调整为允许此后施加于本地时钟的任何摆度。

+/-指示器后面的数字表示测量中的误差范围。正偏移表示本地时钟位于源时钟之前。




[root@rhel7.9 ~]# chronyc add server 10.39.149.249
200 OK
[root@rhel7.9 ~]# chronyc sources -v
210 Number of sources = 5
 
  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^- ntp1.ams1.nl.leaseweb.net     3  10   377   651  -4572us[-4572us] +/-  227ms
^- 114.118.7.163                   2   9   377    17  +2553us[+2553us] +/-   26ms
^* 120.25.115.20                  2  10   377   656  -1432us[-1106us] +/-   19ms
^- 123.57.102.16        1   9   377   52m    -16ms[  -16ms] +/-  103ms
^? 10.39.149.249                  3   6     1    10  +1089us[+1089us] +/-   34ms
[root@rhel7.9 ~]# chronyc sources -v
210 Number of sources = 5
 
  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^- ntp1.ams1.nl.leaseweb.net     3  10   377   843  -4572us[-4572us] +/-  227ms
^- 114.118.7.163                 2   9   377   208  +2553us[+2553us] +/-   26ms
^* 120.25.115.20                  2  10   377   847  -1432us[-1106us] +/-   19ms
^- 123.57.102.16        1   9   377   56m    -16ms[  -16ms] +/-  103ms
^- 10.39.149.249                  3   6    17     6  +1225us[+1225us] +/-   37ms

上面示例中刚加入的NTP部分显示是 ?即不可达的,过了两分钟再查看状态变成了 - 

 NTP 服务器和客户端配置

一、NTP服务器

1 NTP服务器安装

[root@DBTest:/root]# yum install ntp -y
[root@DBTest:/root]# rpm -qa | grep ntp
ntpdate-4.2.6p5-29.el7.centos.2.x86_64
ntp-4.2.6p5-29.el7.centos.2.x86_64

2 NTP服务器配置及初步试运行

服务试开启
systemctl enable ntpd
systemctl start ntpd
systemctl status ntpd

3 配置NTP文件

1.配置文件默认位置:/etc/ntp.conf
2.NTP官方网站:https://www.ntppool.org/zone/cn
3.修改配置文件中的NTP服务器地址
[root@DBTest:/root]# vim /etc/ntp.conf
//位于20行左右-->修改为:
server 0.cn.pool.ntp.org iburst
server 1.cn.pool.ntp.org iburst

4 重启服务并查看

    1.重启服务
[root@DBTest:/root]# systemctl restart ntpd
    
    2.查看ntp服务器有无和上层ntp连通
[root@DBTest:/root]# ntpstat
synchronised to NTP server (84.16.73.33) at stratum 2
   time correct to within 1059 ms
   polling server every 64 s

    3.查看ntp服务器与上层ntp的状态
[root@DBTest:/root]# watch ntpq -p
Every 2.0s: ntpq -p                                                            Wed Feb 24 10:00:16 2021
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*202.118.1.130   .PTP.            1 u    3   64    3   57.615    2.717   0.854
+time.cloudflare 10.20.10.78      3 u    1   64    3  266.444  -19.620   1.578

补充:
ntpq –p 参数详解
remote:响应这个请求的NTP服务器的名称
refid:NTP服务器使用的上一级ntp服务器
st :remote远程服务器的级别.由于NTP是层型结构,有顶端的服务器,多层的Relay Server再到客户端.所以服务器从高到低级别可以设定为1-16.为了减缓负荷和网络堵塞,原则上应该避免直接连接到级别为1的服务器的.
when: 上一次成功请求之后到现在的秒数。
poll : 本地机和远程服务器多少时间进行一次同步(单位为秒).在一开始运行NTP的时候这个poll值会比较小,那样和服务器同步的频率也就增加了,可以尽快调整到正确的时间范围,之后poll值会逐渐增大,同步的频率也就会相应减小
reach:这是一个八进制值,用来测试能否和服务器连接.每成功连接一次它的值就会增加
delay:从本地机发送同步要求到ntp服务器的round trip time
offset:主机通过NTP时钟同步与所同步时间源的时间偏移量,单位为毫秒(ms),offset越接近于0,主机和ntp服务器的时间越接近
jitter:这是一个用来做统计的值.它统计了在特定个连续的连接数里offset的分布情况.简单地说这个数值的绝对值越小,主机的时间就越精确



5 此步骤作为补充

5.1.配置/etc/sysconfig/ntpd文件
ntp服务,默认只会同步系统时间。如果想要让ntp同时同步硬件时间,可以设置/etc/sysconfig/ntpd文件,在/etc/sysconfig/ntpd文件中,添加 SYNC_HWCLOCK=yes 这样,就可以让硬件时间与系统时间一起同步。
#允许BIOS与系统时间同步,也可以通过hwclock -w 命令
SYNC_HWCLOCK=yes
# Drop root to id 'ntp:ntp' by default.
OPTIONS="-x -p /var/run/ntpd.pid -g"
(OPTIONS="-x -u ntp:ntp -p /var/run/ntpd.pid -g")感觉有点问题
有时会在messages日志里看到NTP进程正在做DELETING动作,不过NTP进程并不会真正删除虚拟网络接口,但这个动作会造成网络短暂不通。红帽官方有个对应的解决办法是:
就是在这个文件里增加-L参数。
还有-x和-g参数:
ntpd服务的方式,又有两种策略,一种是平滑、缓慢的渐进式调整(adjusts the clock in small steps所谓的微调);一种是步进式调整(跳跃式调整)。两种策略的区别就在于,微调方式在启动NTP服务时加了个“-x”的参数,而默认的是不加“-x”参数。
假如使用了-x选项,那么ntpd只做微调,不跳跃调整时间,但是要注意,-x参数的负作用:当时钟差大的时候,同步时间将花费很长的时间。-x也有一个阈值,就是600s,当系统时钟与标准时间差距大于600s时,ntpd会使用较大“步进值”的方式来调整时间,将时钟“步进”调整到正确时间。
假如不使用-x选项,那么ntpd在时钟差距小于128ms时,使用微调方式调整时间,当时差大于128ms时,使用“跳跃”式调整。
这两种方式都会在本地时钟与远端的NTP服务器时钟相差大于1000s时,ntpd会停止工作。在启动NTP时加了参数“-g”就可以忽略1000S的问题。
[root@DBTest:/root]# vim /etc/sysconfig/ntpd
# Command line options for ntpd
SYNC_HWCLOCK=yes
OPTIONS="-x -u ntp:ntp -p /var/run/ntpd.pid -g"

5.2.重启服务
systemctl restart ntpd

5.3.linux的硬件时间
Linux硬件时间的设置:可以用hwclock或者clock命令,用法相近,只不过clock命令除了支持x86硬件体系外,还支持Alpha硬件体系。
//查看硬件时间 可以是用 hwclock ,hwclock --show 或者 hwclock -r
[root@DBTest:/root]# hwclock --show
Wed 24 Feb 2021 10:35:47 AM CST  -0.173780 seconds

5.4.ntpq -c asso 用法
运行ntpq -c asso, 如果显示sys.peer则已经处于同步状态了.
[root@DBTest:/root]# ntpq -c asso

ind assid status  conf reach auth condition  last_event cnt
===========================================================
  1 48693  943a   yes   yes  none candidate    sys_peer  3
  2 48694  963a   yes   yes  none  sys.peer    sys_peer  3

5.5.ntpq -c version 用法
检查ntp的版本
[root@DBTest:/root]# ntpq -c version 
ntpq 4.2.6p5@1.2349-o Tue Jun 23 15:38:21 UTC 2020 (1)



6 放行防火墙
firewall-cmd --permanent --add-service=ntp
firewall-cmd --reload

考虑到更加安全点,系统防火墙ntp服务开放至指定IP;同时可以在接入层交换机增添ACL
已开放192.168.7.102为例,更改如下
firewall-cmd --permanent --remove-service=ntp
firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=192.168.7.102 port protocol=udp port=123 accept'
firewall-cmd --reload


7 NTP 服务端安全设置
权限管理使用 restrict 公式如下:
restrict IP mask [参数] / restrict 192.168.0.0 mask 255.255.0.0 nomodify
其中参数主要有底下这些:
    * ignore:拒绝所有类型的NTP的连线;
    * nomodfiy:用户端不能使用NTPC与ntpq这两支程式来修改伺服器的时间参数,但使用者端仍可透过这部主机来进行网路校时的;
    * noquery:用户端不能够使用ntpq,NTPC等指令来查询发表伺服器,等于不提供的NTP的网路校时幂;
    * notrap:不提供陷阱这个远端事件邮箱(远程事件日志)的功能;
    * notrust:拒绝没有认证的用户端.


8 示例

8.1.配置ntp.conf文件
PS:①首先对于默认的client拒绝所有的操作
    ②允许本机地址一切的操作
    ③允许局域网内特定client连接到这台服务器同步时间.但是拒绝让他们修改服务器上的时间
PS:grep -Ev "^#|^$" /etc/ntp.conf
[root@DBTest:/root]# vim /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 kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 111.230.189.174 mask 255.255.255.255 nomodify notrap
restrict time1.cloud.tencent.com nomodify notrap
restrict time2.cloud.tencent.com nomodify notrap
# 使用time1.cloud.tencent.com等域名在系统重启后,会报“ntpd_intres[1081]: host name not found:”保险起见此处添加111.230.189.174
# 111.230.189.174为time2.cloud.tencent.com中其中一个地址
# 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.7.102 mask 255.255.255.255 nomodify notrap
#restrict 192.168.7.199 mask 255.255.255.255 nomodify notrap
restrict 0.0.0.0 mask 0.0.0.0 notrust
interface ignore wildcard
interface listen enp2s0

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).

server 111.230.189.174 prefer
server time1.cloud.tencent.com iburst
server time2.cloud.tencent.com 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

# 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

8.2.特殊补充说明:

8.3其中“restrict 192.168.7.102 mask 255.255.255.255 nomodify notrap” 
可以修改为(可根据实际情况开放整个IP段)-->
“restrict 192.168.7.1 mask 255.255.255.0 nomodify notrap”

8.4 其中“interface listen enp2s0” 中的 “enp2s0” 已实际服务器为主,有的是“eth0”

8.5.重启ntpd服务
[root@DBTest:/root]# systemctl restart ntpd
[root@DBTest:/root]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*139.199.215.251 100.122.36.196   2 u   11   64    1   29.245  -14.986   0.116
+111.230.189.174 100.122.36.4     2 u   10   64    1   25.432  -15.905   0.115

9 NTP防御方式建议
9.1.NTP服务端加固服务
9.2 NTP 服务器升级到 4.2.7p26或更高版本
9.3 关闭现在 NTP 服务的 monlist 功能,在ntp.conf配置文件中增加disable monitor选项
9.4 改用Chrony服务
9.5 防御 NTP 反射和放大攻击
9.6 由于这种攻击的特征比较明显,所以可以通过网络层或者借助运营商实施 ACL 来防御
9.7 使用防 DDoS 设备进行清洗

二、NTP客户端

NTP客户端安装
yum install ntp -y

配置NTP配置文件

[root@host-192-168-2-134 ~]# cat /etc/ntp.conf
//修改位于20行左右的server几行--->修改为:
server 10.39.149.249 iburst


2.2.1 手动更新时间

ntpdate 10.39.149.249


[root@DbMysql:/root]# ntpdate 10.39.149.249
24 Feb 10:19:42 ntpdate[7367]: adjust time server 10.39.149.249 offset 0.000320 sec
 
运行NTP服务
systemctl enable ntpd
systemctl start ntpd
systemctl status ntpd

查看状态
[root@host-192-168-2-134 ~]# ntpstat
synchronised to NTP server (10.39.149.249) at stratum 3
   time correct to within 993 ms
   polling server every 64 s
[root@host-192-168-2-134 ~]# watch ntpq -p
Every 2.0s: ntpq -p                                                         Wed Feb 24 10:24:42 2021
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*10.39.149.249   10.39.149.250    2 u    3   64    7    0.257   -0.115   0.096



NTP客户端安全设置示例
1.配置/etc/ntp.conf
[root@host-192-168-2-134 ~]# egrep -Ev "^#|^$" /etc/ntp.conf
driftfile /var/lib/ntp/drift
restrict default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 10.39.149.249 mask 255.255.255.0 nomodify notrap
restrict 127.0.0.1 
restrict ::1
restrict 0.0.0.0 mask 0.0.0.0 notrust
interface ignore wildcard
interface listen enp2s0
server 10.39.149.249 iburst
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

2.重启ntp服务
systemctl restart ntpd
systemctl status ntpd

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值