Linux【环境部署 01】NTP时间服务器搭建及Linux+Windows客户端使用(一篇学会使用NTP服务)

简介: Linux【环境部署 01】NTP时间服务器搭建及Linux+Windows客户端使用(一篇学会使用NTP服务)

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 2
   time correct to within 14 ms
   polling server every 1024 s

2.2 服务器端配置

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

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

driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1 
restrict ::1
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
includefile /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 6
   time correct to within 949 ms
   polling server every 64 s

3.Linux客户端配置

3.1 安装

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

3.2 客户端配置

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

driftfile /var/lib/ntp/drift
restrict 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 ~]# timedatectl
      Local time: Fri 2022-03-25 15:29:25 CST
  Universal time: Fri 2022-03-25 07:29:25 UTC
        RTC time: Fri 2022-03-25 07:29:24
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
# 开启NTP服务
[root@tcloud ~]# timedatectl set-ntp yes
[root@tcloud ~]# timedatectl
      Local time: Fri 2022-03-25 15:52:30 CST
  Universal time: Fri 2022-03-25 07:52:30 UTC
        RTC time: Fri 2022-03-25 07:52:29
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
# 重启服务
systemctl restart ntpd
# 测试
[root@tcloud ~]# ntpstat
unsynchronised
  time server re-starting
   polling server every 8 s
# 配置后一般要等待几分钟才能与/etc/ntp.conf中的服务器端完成同步
[root@tcloud ~]# ntpstat
synchronised to NTP server (101.201.69.99) at stratum 7
   time correct to within 968 ms
   polling 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 3
   time correct to within 1010 ms
   polling server every 64 s

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

[root@aliyun ~]# ntpq -p
     remote           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 8
includefile /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
目录
相关文章
|
1月前
|
Linux Shell Windows
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
通过Linux挂载Windows端NFS服务实现板端Linux传输文件到PC
|
1月前
|
安全 Linux Shell
全面对比linux和windows,选择哪个系统比较好
全面对比linux和windows,选择哪个系统比较好
74 0
|
23天前
|
网络协议 Python
pythonTCP客户端编程连接服务器
【4月更文挑战第6天】本教程介绍了TCP客户端如何连接服务器,包括指定服务器IP和端口、发送连接请求、处理异常、进行数据传输及关闭连接。在Python中,使用`socket`模块创建Socket对象,然后通过`connect()`方法尝试连接服务器 `(server_ip, server_port)`。成功连接后,利用`send()`和`recv()`进行数据交互,记得在通信完成后调用`close()`关闭连接,确保资源释放和程序稳定性。
|
21天前
|
开发框架 JavaScript 中间件
node+express搭建服务器环境
node+express搭建服务器环境
node+express搭建服务器环境
|
13天前
|
应用服务中间件 Linux 开发工具
如何在阿里云服务器快速搭建部署Nginx环境
以下是内容的摘要: 本文档主要介绍了在阿里云上购买和配置服务器的步骤,包括注册阿里云账号、实名认证、选择和购买云服务器、配置安全组、使用Xshell和Xftp进行远程连接和文件传输,以及安装和配置Nginx服务器的过程。在完成这些步骤后,你将能够在服务器上部署和运行自己的网站或应用。
|
15天前
|
SQL 监控 安全
Linux&Windows 日志分析 陇剑杯 CTF
Linux&Windows 日志分析 陇剑杯 CTF
|
18天前
|
Linux Windows
Windows、Mac、Linux解决端口被占用的问题
Windows、Mac、Linux解决端口被占用的问题
24 1
|
25天前
|
安全 Ubuntu Linux
Linux远程访问Windows实现步骤
在Windows上启用远程桌面连接并获取IP地址后,Linux用户需安装SSH客户端( Debian系:`sudo apt-get update; sudo apt-get install openssh-client`,RPM系:`sudo yum install openssh-clients`)。然后使用命令`ssh 用户名@Windows_IP地址`连接,其中`用户名`和`Windows_IP地址`按实际情况填写。
16 4
|
26天前
|
JavaScript API
本地开发环境请求服务器接口跨域的问题(vue的问题)
本地开发环境请求服务器接口跨域的问题(vue的问题)
18 1
|
1月前
|
网络协议 安全 Shell
【Shell 命令集合 系统设置 】Linux 从远程时间服务器获取当前日期和时间 rdate命令 使用指南
【Shell 命令集合 系统设置 】Linux 从远程时间服务器获取当前日期和时间 rdate命令 使用指南
38 0