1.什么是NTP

百度百科:网络时间协议,英文名称:Network Time Protocol(NTP)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS等等)做同步化,它可以提供高精准度的时间校正(LAN上与标准间差小于1毫秒,WAN上几十毫秒),且可介由加密确认的方式来防止恶毒的协议攻击。NTP的目的是在无序的Internet环境中提供精确和健壮的时间服务。应用场景:

  • 局域内网:由于不能访问互联网时间,需要建立ntp服务器用于时间的同步。
  • 系统集群:对时间敏感的计算处理系统内不同设备之间控制、计算、处理、应用等数据或操作都具有时序性,需要同步的标准时间用于记录各种事件发生的时序,这对后续的数据分析

注意:在CentOS-8中默认不再支持ntp软件包,时间同步将由chrony来实现,以下内容系统环境为CentOS Linux release 7.9.2009 (Core)

2.NTP时间服务器端搭建

2.1 安装

CentOS Linux release 7.9.2009 (Core)自带ntp软件包的,首先要确认是否已安装ntp和ntpdate服务:

# 查看是否安装
[root@aliyun ~]# yum list | grep ntp
ntp.x86_64                               4.2.6p5-29.el7.centos.2
ntpdate.x86_64                           4.2.6p5-29.el7.centos.2
# 未安装则使用命令进行安装
yum install ntp
# 启动服务
systemctl start ntpd
# 开机自启动
systemctl enable ntpd
# 查看ntp服务器状态
[root@aliyun ~]# ntpstat
synchronised to NTP server (100.100.61.88) at stratum 2time correct to within 14 mspolling server every 1024 s

2.2 服务器端配置

# 修改ntp配置文件
vim /etc/ntp.conf

ntp.conf修改前(已去掉注释信息):

driftfile /var/lib/ntp/driftrestrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1server 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 iburstincludefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

ntp.conf修改后:

driftfile /var/lib/ntp/drift# 1.default表示对所有的计算机进行控制
restrict default nomodify notrap nopeer noquery
# 局域网可放行可以指定网段或列出单独IP
# restrict xxx.xxx.xxx.x mask 255.255.255.0 nomodify
# restrict xxx.xxx.xxx.x nomodify
# 1.end
restrict 127.0.0.1
restrict ::1# 2.注释掉公网NTP服务器
# 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
# 2.end# 3.开启自身同步
server 127.127.1.0
Fudge 127.127.1.0 stratum 8
# 3.end
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

2.3 服务器端测试

# 重启服务
systemctl restart ntpd
# 测试
[root@aliyun ~]# ntpstat
synchronised to local net (127.127.1.0) at stratum 6time correct to within 949 mspolling server every 64 s

3.Linux客户端配置

3.1 安装

安装过程跟服务器端一致,这里不再赘述。

3.2 客户端配置

ntp.conf修改前不再赘述,修改后:

driftfile /var/lib/ntp/driftrestrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1# 1.注释掉公网NTP服务器
# 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
# 1.end# 2.开启服务器同步
server aliyun
Fudge aliyun stratum 8
# 2.end
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

3.3 客户端测试

# 查看NTP服务是否开启
[root@tcloud ~]# timedatectlLocal time: Fri 2022-03-25 15:29:25 CSTUniversal time: Fri 2022-03-25 07:29:25 UTCRTC time: Fri 2022-03-25 07:29:24Time zone: Asia/Shanghai (CST, +0800)NTP enabled: no
NTP synchronized: noRTC in local TZ: noDST active: n/a
# 开启NTP服务
[root@tcloud ~]# timedatectl set-ntp yes
[root@tcloud ~]# timedatectlLocal time: Fri 2022-03-25 15:52:30 CSTUniversal time: Fri 2022-03-25 07:52:30 UTCRTC time: Fri 2022-03-25 07:52:29Time zone: Asia/Shanghai (CST, +0800)NTP enabled: yes
NTP synchronized: yesRTC in local TZ: noDST active: n/a
# 重启服务
systemctl restart ntpd
# 测试
[root@tcloud ~]# ntpstat
unsynchronisedtime server re-startingpolling server every 8 s
# 配置后一般要等待几分钟才能与/etc/ntp.conf中的服务器端完成同步
[root@tcloud ~]# ntpstat
synchronised to NTP server (101.201.69.99) at stratum 7time correct to within 968 mspolling server every 64 s

3.4 另一种方式

使用ntpdate命令结合定时任务实现时间同步。此时要停止ntpd服务器,测试ntpdate aliyun报错,查看报错信息:

[root@tcloud ~]# ntpdate -d aliyun
28 Mar 15:33:22 ntpdate[17145]: ntpdate 4.2.6p5@1.2349-o Tue Jun 23 15:38:19 UTC 2020 (1)
Looking for host aliyun and service ntp
host found : aliyun
transmit(101.201.69.99)
101.201.69.99: Server dropped: no data
server 101.201.69.99, port 123
28 Mar 15:33:30 ntpdate[17145]: no server suitable for synchronization found

解决:关闭防火墙,配置aliyun服务器的安全组放开端口123

# 端口开放后
[root@tcloud ~]# ntpdate aliyun
28 Mar 16:06:57 ntpdate[2330]: adjust time server 101.201.69.99 offset 0.000112 sec
# 添加定时任务
vim /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root# /etc/crontab 内容如下
# For details see man 4 crontabs# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed
# 每分钟同步一次
*/1  *  *  *  * root  ntpdate aliyun

4.Windows客户端配置

4.1 配置并启用客户端

Win键+r输入gpedit.msc打开本地组策略编辑器,找到配置 Windows NTP 客户端(计算间配置-管理模板-系统-Windows 时间服务-时间提供程序)进行客户端配置:

配置页面:

启用客户端配置:

4.2 使用客户端配置

5.ntp服务其他参数配置

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

[root@aliyun ~]# ntpstat
synchronised to NTP server (139.199.214.202) at stratum 3time correct to within 1010 mspolling server every 64 s

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

[root@aliyun ~]# ntpq -premote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
+skitty.itu.ch   84.16.67.12      2 u   49   64   17  174.891   20.582   5.712
+139.199.214.202 100.122.36.196   2 u   47   64   37   46.355   43.764   9.636
-time.cloudflare 10.28.12.207     3 u   49   64   37  216.628   31.101  12.642
*tick.ntp.infoma .GPS.            1 u   49   64   37  156.665   12.054  11.269# 或者使用以下命令查看实时状态
[root@aliyun ~]# watch "ntpq -p"
  • remote:列出源的 IP 地址或主机名。第一列中的字符指示源的质量。星号 ( * ) 表示该源是当前引用。
  • refid:参考上一层ntp主机地址。
  • st:stratum阶层。
  • when:多少秒前曾经同步过时间。指出从轮询源开始已过去的时间(秒)。
  • poll:下次更新在多少秒后。指出轮询间隔时间。该值会根据本地时钟的精度相应增加。
  • reach:已经向上层ntp服务器要求更新的次数。是一个八进制数字,指出源的可存取性。值 377 表示源已应答了前八个连续轮询。
  • delay:网络延迟。
  • offset:时间补偿。是源时钟与本地时钟的时间差(毫秒)。
  • jitter:系统时间与bios时间差。

5.3 从其他博客收集的信息

5.3.1 /etc/ntp.conf 配置内容

driftfile /var/lib/ntp/drift# 1. 权限设置 包括放行上层服务器以及开放局域网用户来源
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
# 放行局域网用户来源 或者列出单独IP
restrict xxx.xxx.xxx.x mask 255.255.255.0 nomodify # 2. 设定主机来源 请先将原本的 [0|1|2|3].centos.pool.ntp.org 注掉
#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
server xxx.xxx.xxx.xx prefer  # 3. 默认的一个内部时钟数据 用在没有外部NTP服务器时 使用它为局域网用户提供服务
server 127.127.1.0
Fudge 127.127.1.0 stratum 8includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

5.3.2 restrict选项格式

restrict [ 客户端IP ] mask [ IP掩码 ] [参数]

客户端IPIP掩码指定了对网络中哪些范围的计算机进行控制,如果使用default关键字,则表示对所有的计算机进行控制,参数指定了具体的限制内容,常见的参数如下:

  • ignore:拒绝连接到NTP服务器。
  • nomodiy: 客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
  • noquery: 不提供客户端的时间查询。
  • notrap: 不提供trap远程登录功能,trap服务是一种远程时间日志服务。
  • notrust: 客户端除非通过认证,否则该客户端来源将被视为不信任子网 。
  • nopeer: 提供时间服务,但不作为对等体。
  • kod: 向不安全的访问者发送Kiss-Of-Death报文。

5.3.3 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秒。

5.3.4 层次(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文件中。

5.3.5 同步硬件时钟

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

允许BIOS与系统时间同步,也可以通过hwclock -w 命令。

5.3.6 ntpd、ntpdate的区别

ntpd不仅仅是时间同步服务器,它还可以做客户端与标准时间服务器进行同步时间,而且是平滑同步,并非ntpdate立即同步,在生产环境中慎用ntpdate,两者不可同时运行。时钟的跃变,对于某些程序会导致很严重的问题。

许多应用程序依赖连续的时钟,毕竟这是一项常见的假定,即取得的时间是线性的,一些操作,例如数据库事务,通常会地依赖这样的事实:时间不会往回跳跃。不幸的是,ntpdate调整时间的方式就是所谓的“跃变”:在获得一个时间之后,ntpdate使用settimeofday(2)设置系统时间,这有几个非常明显的问题。

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

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

  • 不优雅:由于是跳变,而不是使时间变快或变慢,依赖时序的程序会出错(例如,如果ntpdate发现你的时间快了,则可能会经历两个相同的时刻,对某些应用而言,这是致命的)。

因而,唯一一个可以令时间发生跳变的点,是计算机刚刚启动,但还没有启动很多服务的那个时候。其余的时候,理想的做法是使用ntpd来校准时钟,而不是调整计算机时钟上的时间。

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

5.3.7 常用NTP服务器地址及IP

210.72.145.44 国家授时中心服务器IP地址
133.100.11.8 日本 福冈大学
time-a.nist.gov 129.6.15.28 NIST, Gaithersburg, Maryland
time-b.nist.gov 129.6.15.29 NIST, Gaithersburg, Maryland
time-a.timefreq.bldrdoc.gov 132.163.4.101 NIST, Boulder, Colorado
time-b.timefreq.bldrdoc.gov 132.163.4.102 NIST, Boulder, Colorado
time-c.timefreq.bldrdoc.gov 132.163.4.103 NIST, Boulder, Colorado
utcnist.colorado.edu 128.138.140.44 University of Colorado, Boulder
time.nist.gov 192.43.244.18 NCAR, Boulder, Colorado
time-nw.nist.gov 131.107.1.10 Microsoft, Redmond, Washington
nist1.symmetricom.com 69.25.96.13 Symmetricom, San Jose, California
nist1-dc.glassey.com 216.200.93.8 Abovenet, Virginia
nist1-ny.glassey.com 208.184.49.9 Abovenet, New York City
nist1-sj.glassey.com 207.126.98.204 Abovenet, San Jose, California
nist1.aol-ca.truetime.com 207.200.81.113 TrueTime, AOL facility, Sunnyvale, California
nist1.aol-va.truetime.com 64.236.96.53 TrueTime, AOL facility, Virginia
ntp.sjtu.edu.cn 202.120.2.101 上海交通大学网络中心NTP服务器地址
s1a.time.edu.cn 北京邮电大学
s1c.time.edu.cn 北京大学
s2m.time.edu.cn 北京大学
s1d.time.edu.cn 东南大学
s1e.time.edu.cn 清华大学
s2a.time.edu.cn 清华大学
s2b.time.edu.cn 清华大学
s1b.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桂林主节点

5.3.8 查看网关方法

[root@aliyun ~]# route -n
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         172.20.111.253  0.0.0.0         UG    0      0        0 eth0
169.254.0.0     0.0.0.0         255.255.0.0     U     1002   0        0 eth0
172.20.96.0     0.0.0.0         255.255.240.0   U     0      0        0 eth0[root@aliyun ~]# ip route show
default via 172.20.111.253 dev eth0
169.254.0.0/16 dev eth0 scope link metric 1002
172.20.96.0/20 dev eth0 proto kernel scope link src 172.20.111.49[root@aliyun ~]# netstat -r
Kernel IP routing table
Destination     Gateway         Genmask         Flags   MSS Window  irtt Iface
default         gateway         0.0.0.0         UG        0 0          0 eth0
link-local      0.0.0.0         255.255.0.0     U         0 0          0 eth0
172.20.96.0     0.0.0.0         255.255.240.0   U         0 0          0 eth0

【Linux部署】NTP时间服务器搭建及Linux+Windows客户端使用(一篇学会使用NTP服务)相关推荐

  1. Linux学习之云服务器搭建嵌入式Linux开发环境

    Linux学习之云服务器搭建嵌入式Linux开发环境 第一步:购买云服务器 进入腾讯云官方网站腾讯云官方网站 选择云+校园云+校园 进入控制台 将云服务器系统设置为Ubuntu系统 使用Xshell6 ...

  2. 【基础服务】NTP时间服务器搭建

    文章目录 1. NTP服务简介 2. NTP服务搭建 2.1. 环境准备 2.2. 安装NTP服务 2.3. 配置NTP服务 2.4. 启动NTP服务 3. 客户机时间同步 4. 注意事项 1. NT ...

  3. Linux NTP时间服务器搭建

    (1)NTP服务器是用于局域网服务器时间同步使用的,可以保证局域网所有的服务器与时间服务器的时间保持一致,某些应用对时间实时性要求高,必须保证时间的统一性 (2)在互联网中的时间服务器也有很多, 例如 ...

  4. NTP时间服务器搭建

    一,搭建时间服务器 NTP----------->时间服务器  (分层部署,不能超过15层)   1.  服务端 #yum -y install chrony(默认装好的)(客户端和服务端都是装 ...

  5. Linux操作系统实践——Samba服务器搭建

    文章目录 Linux操作系统实践--Samba服务器搭建 一.实验目的 二.实验内容 ② 学生教师服务 三. 题目分析及基本设计过程分析 ② 学生教师服务 1) 整体思路 2) 系统用户和组分配设计说 ...

  6. Linux服务部署之NTP时间服务器

    Linux服务部署之NTP时间服务器: 服务器端IP:192.168.11.119 client端Ip: 192.168.11.101 先安装服务器端ntp: 首先先查看是否安装了ntp时间服务器; ...

  7. Linux 服务器内网搭建NTP时间服务器(Network Time Protocol)

    文章目录 Linux 服务器内网搭建NTP时间服务器(Network Time Protocol) 一.在主服务器端安装NTP时间服务器 二.在客户端安装NTP时间服务器 三.Xhell 同时控制多个 ...

  8. Linux下NTP时间同步服务器搭建

    NTP介绍 NTP 服务器是用于局域网服务器时间同步使用的,可以保证局域网所有的服 务器与时间服务器的时间保持一致,某些应用对时间实时性要求高的必须统一时 间. 互联网的时间服务器也有很多,例如 nt ...

  9. 离线NTP服务器搭建(Linux)

    离线NTP服务器搭建(Linux) 如果没能帮你解决问题,请在下方留言 实例: 服务端-192.168.111.143 客户端-192.168.111.170 1. 获取rpm包 解压操作系统的镜像文 ...

最新文章

  1. python安装gdal包_python安装gdal的两种方法
  2. zabbix报错:Zabbix服务启动不了
  3. H5的Websocket基本使用
  4. vs2019 缺android sdk,VS2019由于缺少NuGet Microsoft.NET.Sdk.Functions程序包而无法加载项目,但也无法添加此程序包(示例代码)...
  5. 【51nod-1289】大鱼吃小鱼
  6. C Primer Plus (第6版) 读书笔记_Chapter 1
  7. ACM/CF赛制getstart模板
  8. Excel VBA 入门(零)
  9. 计算机程序的构造和解释1构造过程抽象1.2过程与它们所产生的计算
  10. grafana-----Time Range Controls
  11. 2176. 统计数组中相等且可以被整除的数对
  12. C语言的这些事情有关内存
  13. unicode编码表,转载自:近來情轉深的博客
  14. 阿里云 mysql 导出数据_mysql数据库导出数据库
  15. 拓嘉启远:拼多多购物运输中的商品能拒收吗
  16. 将两个有序列表合并,并保持顺序(一)
  17. ANSI编码文件批量转换为UTF-8编码小tips
  18. 【通信仿真】基于matlab噪声调频干扰仿真【含Matlab源码 1951期】
  19. Qt Creator老是提示红色信息In included file:unknown type name ‘b‘,怎么解决?
  20. Spring框架对redis的封装

热门文章

  1. Ubuntu/linux 系统彻底杀死一个进程的方法
  2. 解决Eclipse代码分析插件SonarLint在Console输出乱码问题
  3. Android Studio 从入门到精通
  4. linux笔记_文件搜索命令
  5. DB2数据库报 [SQL0805N Package NULLID.SQLLD003 was not found.]
  6. 使用前端框架Foundation 4来帮助简化响应式设计开发
  7. MSSQLSERVER数据库- 递归查询例子
  8. pr如何处理音效_学视频剪辑 PR通关教学课程 教程
  9. 前端获取后端传来的session_java后台如何获取,前台传来的表单数据
  10. linux ls 升序,ls 命令查看文件时候,按修改时间倒序或升序排列