GUN/Linux时间同步服务之ntp配置管理

风险告知

  • 本人及本篇博文不为任何人及任何行为的任何风险承担责任,图解仅供参考,请悉知!
  • 相关配置操作是在一个全新的演示环境下进行的,演示环境中没有任何有价值的数据,但这并不代表摆在你面前的环境也是如此。
  • 生产环境全新安装后(业务上线前)或者自行测试都可放心参考该图解内容,该教程是完全站在生产业务角度去做的建议配置(业务厂商有特殊要求的除外)。
  • 图解内容可能存在一些自动执行的shell脚本,所以,一定要全面评估并且备份有价值数据后再进行。因为任何操作都是有丢失数据风险的可能,比如说误操作覆盖或移动有价值数据进而影响到生产业务都无异于删库,无论是你是否即将跑路!
  • 删除跑路的方式千万条,不仅仅有你最熟悉的rm rf /*,有时候一个空格都能要了你的小(老)命,小心是的万年船,最好的归宿是阖家欢乐,是团团圆圆,是你好我好大家好,是博主真好!

废话翩翩

解释下为什么要码这么多文字说明,包括后续的内容中也是如此,注释说明真的多。因为我觉得我们在学习过程过应该有点儿逼数,那就是学习一定不仅仅为了学会某一条命令,某一个技巧......更重要的是尽可能的理解这件事儿的来龙去脉、因果关系,最好能形成自己的思路。图解内容完全手打,所以难免有所纰漏,欢迎评论区指正,同时内容处于一个不断(随时)更新的状态,以最新的内容为准!

开拓视野

时间同步服务器更准确来讲就应该叫时间戳服务器,因为所有的时间戳服务器都是以1970年1月1日00:00:00协调世界时(UTC)为基础定义的时间,时间戳服务器一般是从地球同步卫星上获取一个精确时间,像我们国家就有一个官方的授时中心(中国科学院国家授时中心),位于陕西省西安市临潼区。我们从时间戳服务器上获取的并不是一个某某年几时几分几秒的时间,而是一个相对于基础定义时间之间的一个差值,称为时间戳,然后我们在根据本地操作系统所处的时区配合时间戳做本地时间调整。相关的时间同步协议有NTP、SNTP、PTP等,相关的时间同步应用有NTP、Chrony、Windows 时间服务 (W32Time)等等。

食用范围

目前测试结果来讲适用于几乎所有GNU/Linux发行版。

避坑指南

0、所有配置都是在操作系统安装时时区配置正确的情况下进行的,如果时区不正确的情况,请查阅相关资料进行修改。

1、服务端默认使用UDP端口123,在有防火墙等安全设备时,需要放通,可以暂时先关闭防火墙进行测试,但是不建议永久关闭防火墙。

2、服务端默认不允许任何客户端接入该服务,需要修改使用参数restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap或者更细粒度控制

3、服务端如果局域网内,或者某个独立集群内只需要保持内部时间一致,对精确度要求不高的情况下,可以自定搭建纯纯的本地时间戳服务器,妥妥以以本地时间为准,这时候需要使用参数server 127.127.1.0 fudge 127.127.1.0 stratum 10,参数中的10代表层级,是可以修改的,不过习惯上一般保持默认。

4、客户端不需要特殊配置,只需要配置上游时间戳服务器地址即可。

5、服务端和客户端安装完成后,默认是非开机自动启动,且ntpd服务是停止状态,不重启情况下需要手动开启服务,并配置开机后自动启动,配置文件有任何修改的情况下都需要重启服务生效。

言归正传

ntp是一个开源自由的网络时间协议 NTP 的客户端和服务器软软件。它能让计算机保持系统时钟与时钟服务器(NTP)同步,因此让你的计算机保持精确的时间,ntp也可以作为服务端软件为其他计算机提供时间同步服务。最小化安装方式默认不安装ntp软件包,ntp是网络时间协议(Network Time Protocol)的简称,通过 udp 123 端口进行网络时钟同步。另外还有一个名为chrony的时间同步服务开源软件,而且由于ntp服务本身漏洞问题,实际上新版GNU/Linux发行版较新的版本中默认使用chrony来同步时间,大多数同学也都会选择使用chrony这个软件。

# 安装ntp时间同步软件包
[root@centos79 ~]# yum install ntp
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.bupt.edu.cn
 * extras: mirrors.bupt.edu.cn
 * updates: mirrors.bupt.edu.cn
base                                                                                                                                                                                     | 3.6 kB  00:00:00
extras                                                                                                                                                                                   | 2.9 kB  00:00:00
updates                                                                                                                                                                                  | 2.9 kB  00:00:00
updates/7/x86_64/primary_db                                                                                                                                                              |  25 MB  00:00:01
正在解决依赖关系
--> 正在检查事务
---> 软件包 ntp.x86_64.0.4.2.6p5-29.el7.centos.2 将被 安装
--> 正在处理依赖关系 ntpdate = 4.2.6p5-29.el7.centos.2,它被软件包 ntp-4.2.6p5-29.el7.centos.2.x86_64 需要
--> 正在处理依赖关系 libopts.so.25()(64bit),它被软件包 ntp-4.2.6p5-29.el7.centos.2.x86_64 需要
--> 正在检查事务
---> 软件包 autogen-libopts.x86_64.0.5.18-5.el7 将被 安装
---> 软件包 ntpdate.x86_64.0.4.2.6p5-29.el7.centos.2 将被 安装
--> 解决依赖关系完成

依赖关系解决

================================================================================================================================================================================================================
 Package                                              架构                                        版本                                                          源                                         大小
================================================================================================================================================================================================================
正在安装:
 ntp                                                  x86_64                                      4.2.6p5-29.el7.centos.2                                       base                                      549 k
为依赖而安装:
 autogen-libopts                                      x86_64                                      5.18-5.el7                                                    base                                       66 k
 ntpdate                                              x86_64                                      4.2.6p5-29.el7.centos.2                                       base                                       87 k

事务概要
================================================================================================================================================================================================================
安装  1 软件包 (+2 依赖软件包)

总下载量:701 k
安装大小:1.6 M
Is this ok [y/d/N]: t
Is this ok [y/d/N]: y
Downloading packages:
(1/3): autogen-libopts-5.18-5.el7.x86_64.rpm                                                                                                                                             |  66 kB  00:00:00
(2/3): ntpdate-4.2.6p5-29.el7.centos.2.x86_64.rpm                                                                                                                                        |  87 kB  00:00:00
(3/3): ntp-4.2.6p5-29.el7.centos.2.x86_64.rpm                                                                                                                                            | 549 kB  00:00:00
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
总计                                                                                                                                                                            1.0 MB/s | 701 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : autogen-libopts-5.18-5.el7.x86_64                                                                                                                                                           1/3
  正在安装    : ntpdate-4.2.6p5-29.el7.centos.2.x86_64                                                                                                                                                      2/3
  正在安装    : ntp-4.2.6p5-29.el7.centos.2.x86_64                                                                                                                                                          3/3
  验证中      : ntpdate-4.2.6p5-29.el7.centos.2.x86_64                                                                                                                                                      1/3
  验证中      : ntp-4.2.6p5-29.el7.centos.2.x86_64                                                                                                                                                          2/3
  验证中      : autogen-libopts-5.18-5.el7.x86_64                                                                                                                                                           3/3

已安装:
  ntp.x86_64 0:4.2.6p5-29.el7.centos.2

作为依赖被安装:
  autogen-libopts.x86_64 0:5.18-5.el7                                                                  ntpdate.x86_64 0:4.2.6p5-29.el7.centos.2

完毕!
# 查看ntpd服务刚安装后默认的运行状态
[root@centos79 ~]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
# 手动启动ntpd服务
[root@centos79 ~]# systemctl start ntpd
# 设置ntpd服务开机自动启动
[root@centos79 ~]# systemctl enable ntpd
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.
# 再次查看ntpd服务的运行状态
[root@centos79 ~]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 日 2024-01-28 16:42:39 CST; 2s ago
  Process: 1925 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1926 (ntpd)
   CGroup: /system.slice/ntpd.service
           └─1926 /usr/sbin/ntpd -u ntp:ntp -g

1月 28 16:42:39 centos79 ntpd[1926]: Listen and drop on 1 v6wildcard :: UDP 123
1月 28 16:42:39 centos79 ntpd[1926]: Listen normally on 2 lo 127.0.0.1 UDP 123
1月 28 16:42:39 centos79 ntpd[1926]: Listen normally on 3 ens33 192.168.18.133 UDP 123
1月 28 16:42:39 centos79 ntpd[1926]: Listen normally on 4 lo ::1 UDP 123
1月 28 16:42:39 centos79 ntpd[1926]: Listen normally on 5 ens33 fe80::f1c3:5265:8d2e:fb3c UDP 123
1月 28 16:42:39 centos79 ntpd[1926]: Listening on routing socket on fd #22 for interface updates
1月 28 16:42:39 centos79 systemd[1]: Started Network Time Service.
1月 28 16:42:41 centos79 ntpd[1926]: 0.0.0.0 c016 06 restart
1月 28 16:42:41 centos79 ntpd[1926]: 0.0.0.0 c012 02 freq_set kernel 0.000 PPM
1月 28 16:42:41 centos79 ntpd[1926]: 0.0.0.0 c011 01 freq_not_set
# 配置ntpd服务的默认配置,关键点参照中文注解
[root@centos79 ~]# vi /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.
# 默认拒绝所有客户端通过NTP服务器获取并同步时间,不允许修改同步服务器时间参数
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# 配置允许192.168.10.23通过NTP服务器获取并同步时间,不允许修改同步服务器时间参数,提供trap远端登陆
restrict 192.168.10.23 nomodify notrap
# 配置允许192.168.10.0/24网络范围内的主机通过NTP服务器获取并同步时间,不允许修改同步服务器时间参数,提供trap远端登陆
restrict 192.168.10.0 mask 255.255.255.0 nomodify notrap
# 配置允许所有主机通过NTP服务器获取并同步时间,不允许修改同步服务器时间参数,提供trap远端登陆
restrict 0.0.0.0 mask 0.0.0.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
# 配置上游NTP服务器用于获取准确时间,以阿里云NTP服务器为例
server ntp.aliyun.com iburst
# 如果不选择使用赶上有NTP服务器,以本地时间为准,则配置以下参数
server 127.127.1.0
fudge 127.127.1.0 stratum 10

#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
# 查看上游时间同步服务器状态信息
[root@centos79 ~]# ntpq -pn
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
-193.182.111.143 194.58.202.148   2 u   87  128  377  258.607   31.257  10.772
*84.16.73.33     .GPS.            1 u   95  128  377  201.591   -0.666  11.522
+110.42.98.138   17.253.84.125    2 u   27  128  377   34.358    3.475   2.940
+81.16.177.123   185.131.196.12   2 u   22  128  377  197.473    5.359   8.413

ntp时间同步服务拓展资料

 研读官方文档有助于身体健康!

ntp项目官网 icon-default.png?t=N7T8https://www.ntppool.org/zh/

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT民工王哪跑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值