【ntp】Linux & Windows时间服务器NTP配置

更多内容请点击 我的博客 查看,欢迎来访。

Windows Server2012配置NTP服务

打开注册表

Win+R输入regedit,打开注册表

BLOG_20191230_234708_69

设定成为NTP服务器类型

找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\Parameters,确认Type的值为NTP

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-H7JDLsVm-1577721205736)(https://blog.starmeow.cn/media/blog/images/2019/12/BLOG_20191230_234719_92.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 “博客图集BLOG_20191230_234719_92.png”)]

设定授权时间服务器

找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\ConfigAnnounceFlags的数值数据修改为5,默认为a

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wHmu3czF-1577721205737)(https://blog.starmeow.cn/media/blog/images/2019/12/BLOG_20191230_234746_29.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 “博客图集BLOG_20191230_234746_29.png”)]

启用NTP服务器

找到HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\W32Time\TimeProviders\NtpServerEnabled的数值数据修改为1,默认为0

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-W2GqxqzX-1577721205737)(https://blog.starmeow.cn/media/blog/images/2019/12/BLOG_20191230_234756_37.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 “博客图集BLOG_20191230_234756_37.png”)]

重启WindowsTime服务

使用命令net stop w32time && net start w32time

BLOG_20191230_234811_43

如果出现报错

BLOG_20191230_234820_65

进入服务将Windows Time启动

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-QOq2ewBq-1577721205738)(https://blog.starmeow.cn/media/blog/images/2019/12/BLOG_20191230_234827_88.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 “博客图集BLOG_20191230_234827_88.png”)]

并将服务设置为自动启动

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-zs7CEAYT-1577721205738)(https://blog.starmeow.cn/media/blog/images/2019/12/BLOG_20191230_234835_22.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 “博客图集BLOG_20191230_234835_22.png”)]

此时该命令就可以正常使用了,如果之前服务没启动,启动好就不用使用下面的命令重启。

BLOG_20191230_234843_87

检验监听端口

可以看到端口已经被监听了

BLOG_20191230_234850_13

防火墙开启端口

加入防火墙规则,打开端口 123 ,命令提示符netsh firewall add portopening protocol=UDP port=123 name=NTPSERVER

BLOG_20191230_234856_26

也就是自动在Windows防火墙添加了一条规则

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-AYBv29QO-1577721205740)(https://blog.starmeow.cn/media/blog/images/2019/12/BLOG_20191230_234909_34.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 “博客图集BLOG_20191230_234909_34.png”)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-TMi3LM57-1577721205741)(https://blog.starmeow.cn/media/blog/images/2019/12/BLOG_20191230_234917_80.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 “博客图集BLOG_20191230_234917_80.png”)]

Windows客户端尝试同步

输入服务端IP即可

BLOG_20191230_234924_46

如果修改服务端的时间,客户端同步也会跟着修改。

CentOS配置NTP服务器

安装ntp包

[root@localhost ~]# yum install ntp ntpdate -y

查看ntp配置文件

[root@localhost ~]# cp /etc/ntp.conf /etc/ntp.conf.bak
[root@localhost ~]# 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 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.
#restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap
# 限制本地网络上的主机。限制192.168.1.0掩码255.255.255.0 nomodify notrap

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 使用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

#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

restrict选项格式

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

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

  • ignore:拒绝连接到NTP服务器
  • nomodiy: 客户端不能更改服务端的时间参数,但是客户端可以通过服务端进行网络校时。
  • noquery: 不提供客户端的时间查询,拒绝btodq/ntpdc查询
  • notrap: 不提供trap远程登录功能,trap服务是一种远程时间日志服务。
  • notrust: 客户端除非通过认证,否则该客户端来源将被视为不信任子网 。
  • nopeer: 提供时间服务,但不作为对等体。
  • kod: 向不安全的访问者发送Kiss-Of-Death报文。
restrict 127.0.0.1 :表示当前主机可以使用这个时间服务
restrict 192.168.1.0 mask 255.255.255.0 :表示授权192.168.1.0网络中的全部主机可以使用时间服务
restrict 172.16.1 mask 255.255.255.0 nomodify notrap :限制了ip段可以来访问同步时间但是不能修改时间
restrict 0.0.0.0 mask 0.0.0.0 nomodify notrap :表示所有主机都可以访问这个时间服务
restrict default ignore :设置默认策略,拒绝任意主机进行时间同步
restrict default kod nomodify notrap nopeer noquery:表示默认拒绝所有IP的时间同步

如果增加restrict 0.0.0.0 mask 0.0.0.0 nomodify这项配置,再重启ntpd服务,就可以被所有客户端同步时间了。

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秒。
server times.aliyun.com iburst prefer    # 从多个时间服务器同步时间,perfer表示优先
server ntp.aliyun.com iburst
server cn.pool.ntp.org iburst

启动ntp服务

[root@localhost ~]# systemctl start ntpd
[root@localhost ~]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 六 2019-12-28 21:53:44 CST; 1s ago
  Process: 1560 ExecStart=/usr/sbin/ntpd -u ntp:ntp $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1561 (ntpd)
   CGroup: /system.slice/ntpd.service
           └─1561 /usr/sbin/ntpd -u ntp:ntp -g

设置ntp开机自启

[root@localhost ~]# chkconfig ntpd on
注意:正在将请求转发到“systemctl enable ntpd.service”。
Created symlink from /etc/systemd/system/multi-user.target.wants/ntpd.service to /usr/lib/systemd/system/ntpd.service.

查看ntp服务状态ntpq -p

ntpq -p可以查看本地NTP需进行同步的指定NTP服务器状态。

root@localhost ~]# ntpq -p
ntpq: read: Connection refused
# 表示ntpd服务没有启动,需要先启动服务
[root@localhost ~]# systemctl status ntpd
● ntpd.service - Network Time Service
   Loaded: loaded (/usr/lib/systemd/system/ntpd.service; enabled; vendor preset: disabled)
   Active: inactive (dead)
[root@localhost ~]# systemctl start ntpd

[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 119.28.206.193  100.122.36.4     2 u   13   64    1  158.836  130.040   0.344
 makaki.miuku.ne 218.186.3.36     2 u   11   64    1  230.887    5.334   1.887
*162.159.200.1   10.23.12.51      3 u    5   64    1   68.950   75.505   2.802
 ntp8.flashdance .STEP.          16 u    -   64    0    0.000    0.000   0.000
  • remote :本地主机所连接的上层NTP服务器,最左边的符号如下:
    • 如果有*代表目前正在使用当中的上层NTP服务器。
    • 如果有+代表也有连上上层NTP服务器,可以作为提高时间更新的候选NTP服务器
    • 如果有-代表同步的该NTP服务器被认为是不合格的NTP Server
    • 如果有x代表同步的外网NTP服务器不可用
  • refid :指的是给上层NTP服务器提供时间校对的服务器。
  • st:上层NTP服务器的级别。
  • when: 上一次与上层NTP服务器进行时间校对的时间(单位:s)
  • poll :本地主机与上层NTP服务器进行时间校对的周期(单位:s)
  • reach:八进制数,表示最近8次时钟同步包接收情况(1表示接收成功,0表示接收失败。每接收一个包左移一位。对于一个运行较长时间的NTP client而言,这个值应该是377->11,111,111,即最近8次包接收均成功;否则表示有丢包情况发生).
  • delay:网络传输过程当中延迟的时间,单位为 10^(-6) 秒
  • offset:时间补偿的结果,单位为10^(-6) 秒
  • jitter:Linux 系统时间与 BIOS 硬件时间的差异时间, 单位为 10^(-6) 秒。

ntpstat检查是否与Server联通

ntpstat可以确认本地NTP与上层NTP服务器是否联通。

ntpstat 将报告在本地计算机上运行的 NTP 守护程序(ntpd)的同步状态。如果发现本地系统与参考时间源保持同步,则 ntpstat 将报告大致的时间精度。

ntpstat 命令根据 NTP 同步状态返回三种状态码。echo $?详情如下。

  • 0:如果时钟同步则返回 0。
  • 1:如果时钟不同步则返回 1。
  • 2:如果时钟状态不确定,则返回 2,例如 ntpd 不可联系时。
[root@localhost ~]# ntpstat
synchronised to NTP server (94.130.49.186) at stratum 4
   time correct to within 139 ms
   polling server every 64 s

timedatectl 命令检查 NTP 状态

timedatectltimedatectl status命令用于查询和更改系统时钟及其在 systmed 系统中的设置。

[root@localhost ~]# timedatectl status
      Local time: 一 2019-12-30 22:11:52 CST
  Universal time: 一 2019-12-30 14:11:52 UTC
        RTC time: 一 2019-12-30 14:11:51
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

防火墙放行ntp

[root@localhost ~]# firewall-cmd --add-service=ntp
success
[root@localhost ~]# firewall-cmd --permanent --add-service=ntp
success
[root@localhost ~]# firewall-cmd --reload
success
# 或使用
[root@localhost ~]# firewall-cmd --permanent --zone=public --add-port=123/udp
success
[root@localhost ~]# firewall-cmd --reload
success

CentOS NTP服务器客户端实例

服务端

# Server
[root@localhost ~]# vim /etc/ntp.conf
# 参考以下内容
driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery
# 添加下面的行,允许这个网段的客户端访问ntp服务
restrict 192.168.99.0 mask 255.255.255.0 nomodify notrap

restrict 127.0.0.1
restrict ::1

# 指定远程时间服务器的地址
server ntp.ntsc.ac.cn iburst prefer
server edu.ntp.org.cn iburst
server ntp1.aliyun.com iburst
# 允许上层服务器主动修改本机时间
restrict ntp.ntsc.ac.cn nomodify notrap noquery
restrict edu.ntp.org.cn nomodify notrap noquery
restrict ntp1.aliyun.com nomodify notrap noquery

# 外部时间服务器不可用时,以本地时间作为时间服务
server 127.127.1.0
fudge 127.127.1.0 stratum 10

includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

# 首先服务器需要和公网时间服务器对时
[root@localhost ~]# ntpdate ntp.ntsc.ac.cn

# 修改完成后重启服务
[root@localhost ~]# systemctl restart ntpd
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*114.118.7.161   123.139.33.3     2 u   10   64    3   40.749  -11.547  18.492
+202.118.1.130   .PTP.            1 u    8   64    3   56.298  -11.583   3.647
+120.25.115.20   10.137.53.7      2 u    9   64    3   42.701   -8.655   1.109
 LOCAL(0)        .LOCL.          10 l   75   64    2    0.000    0.000   0.000
[root@localhost ~]# ntpstat 
synchronised to NTP server (114.118.7.161) at stratum 3
   time correct to within 254 ms
   polling server every 64 s
[root@localhost ~]# echo $?
0

客户端

# Client
[root@localhost ~]# vim /etc/ntp.conf
# 参考以下内容
driftfile /var/lib/ntp/drift

restrict default nomodify notrap nopeer noquery

restrict 127.0.0.1
restrict ::1

# 只需要指定ntp服务器IP即可
server 192.168.99.103

includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

# 优先加上-u选项同步好时间,在启动ntpd服务
[root@localhost ~]# ntpdate 192.168.99.103
30 Dec 23:15:43 ntpdate[4241]: the NTP socket is in use, exiting
[root@localhost ~]# ntpdate -u 192.168.99.103
30 Dec 23:25:43 ntpdate[1617]: adjust time server 192.168.99.103 offset -0.006181 sec


[root@localhost ~]# service ntpd restart

# 查看时间同步状态,一般需要5-10分钟后才能成功连接和同步。所以,服务启动后需要稍等下。
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
 192.168.99.103  114.118.7.163    3 u   24   64    1    1.382   -7.111   0.000
[root@localhost ~]# ntpstat
unsynchronised
  time server re-starting
   polling server every 8 s

[root@localhost ~]# echo $?
1

# 过会再次查看就同步了
[root@localhost ~]# ntpq -p
     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*192.168.99.103  114.118.7.163    3 u   29   64   37    0.236   -3.687   2.649
[root@localhost ~]# ntpstat
synchronised to NTP server (192.168.99.103) at stratum 4
   time correct to within 496 ms
   polling server every 64 s

ntpdate时间同步

在Debian9下安装同步时间服务器

# apt-get install ntpdate

# ntpdate time.windows.com
 4 Jan 04:10:36 ntpdate[20701]: adjust time server 51.140.65.84 offset -0.043235 sec

查看时间,东部标准时间 (EST),这样会导致看到的时间不一致,需要修改时区。

# date
2019年 01月 04日 星期五 04:12:05 EST

更改时区

# dpkg-reconfigure tzdata

1.查看当前系统时间

# date
2019年 01月 04日 星期五 17:59:20 CST

2.查看当前硬件时间

hwclock

3.设置系统时间,字符串形式

date -s "2018-01-01 11:20:30"

4.将系统时间同步到硬件

hwclock --systohc

国内NTP服务器

0.cn.pool.ntp.org
1.cn.pool.ntp.org
2.cn.pool.ntp.org
3.cn.pool.ntp.org
ntp1.aliyun.com
ntp2.aliyun.com
ntp3.aliyun.com
ntp4.aliyun.com
ntp5.aliyun.com
ntp6.aliyun.com
ntp7.aliyun.com
ntp.ntsc.ac.cn  # 中国国家授时中心
edu.ntp.org.cn # 中国教育网
cn.pool.ntp.org # 最常用的国内NTP服务器
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值