搭建内网NTP时间服务器

时间服务器作用

大数据产生与处理系统是各种计算设备集群的,计算设备将统一、同步的标准时间用于记录各种事件发生时序,如E-MAIL信息、文件创建和访问时间、数据库处理时间等。
大数据系统内不同计算设备之间控制、计算、处理、应用等数据或操作都具有时序性,若计算机时间不同步,这些应用或操作或将无法正常进行。
大数据系统是对时间敏感的计算处理系统,时间同步是大数据能够得到正确处理的基础保障,是大数据得以发挥作用的技术支撑。
大数据时代,整个处理计算系统内的大数据通信都是通过网络进行。
时间同步也是如此,利用大数据的互联网络传送标准时间信息,实现大数据系统内时间同步。
网络时间同步协议(NTP)是时间同步的技术基础。

ntp安装

确认是否已安装ntp

1
rpm –qa | grep ntp

若只有ntpdate而未见ntp,则需删除原有ntpdate。如:

1
2
3
ntpdate-4.2.6p5-22.el7_0.x86_64
fontpackages-filesystem-1.44-8.el7.noarch
python-ntplib-0.3.2-1.el7.noarch

删除已安装ntp

1
yum –y remove ntpdate-4.2.6p5-22.el7.x86_64

重新安装ntp

安装ntp

1
yum –y install ntp

配置开机自启

1
systemctl enable ntpd.service

配置ntp服务

国家授时中心NTP服务器地址

1
ntp.ntsc.ac.cn

阿里云时间同步服务器地址

1
2
ntp1.aliyun.com
time1.aliyun.com

修改配置文件

1
vim /etc/ntp.conf

修改前

1
2
3
4
5
6
7
8
9
10
11
12
# cat /etc/ntp.conf |grep -Ev '^#|^$'
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

修改后

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# cat /etc/ntp.conf |grep -Ev '^#|^$'
driftfile /var/lib/ntp/drift
restrict default nomodify notrap nopeer noquery
restrict 127.0.0.1
restrict ::1
#允许内网其他服务器同步时间
restrict 10.186.1.0 mask 255.255.255.0 nomodify notrap
#定义使用的上游ntp服务器,将原来的注释
server ntp.ntsc.ac.cn
server ntp1.aliyun.com
server time1.aliyun.com
#允许上层时间服务器主动修改本机时间,也可以注释
#restrict ntp.ntsc.ac.cn nomodify notrap nopeer noquery
#restrict ntp1.aliyun.com nomodify notrap nopeer noquery
#restrict ntp1.aliyun.com nomodify notrap nopeer noquery
#外部时间服务器不可用时,以本地时间作为时间服务
server 127.127.1.0
fudge 127.127.1.0 stratum 10
includefile /etc/ntp/crypto/pw
keys /etc/ntp/keys
disable monitor

启动服务

1
systemctl start ntpd.service

检查时间服务器是否正确同步,列出本NTP服务器与上游服务器的连接状态

1
2
3
4
5
6
7
# ntpq -p
remote refid st t when poll reach delay offset jitter
==============================================================================
+114.118.7.163 210.72.145.18 2 u 230 512 377 27.560 -8.168 0.719
+120.25.115.20 10.137.53.7 2 u 269 512 377 32.112 -8.144 1.597
*203.107.6.88 10.165.84.13 2 u 329 512 377 14.344 -8.753 1.291
LOCAL(0) .LOCL. 10 l 164m 64 0 0.000 0.000 0.000

ntpq -p可以列出目前我们的NTP与相关的上层NTP的状态,以上的几个字段的意义如下:

remote:即remote - 本机和上层ntp的ip或主机名,“+”表示优先,“*”表示次优先。
refid:参考的上一层NTP主机的地址
st:即stratum阶层
poll:下次更新在几秒之后
offset:时间补偿的结果

列出是否与上游服务器连接,需要过5分钟

1
2
3
4
# ntpstat
synchronised to NTP server (203.107.6.88) at stratum 3
time correct to within 41 ms
polling server every 512 s

如果没有连接,则需要检查UDP端口,端口号123

1
2
3
4
5
6
7
# netstat -ln|grep 123
udp 0 0 10.186.61.39:123 0.0.0.0:*
udp 0 0 127.0.0.1:123 0.0.0.0:*
udp 0 0 0.0.0.0:123 0.0.0.0:*
udp6 0 0 fe80::aff:feba:3d27:123 :::*
udp6 0 0 ::1:123 :::*
udp6 0 0 :::123 :::*

客户端测试

先修改成一个错误时间

1
2
# date -s "2018-12-23 11:23:34"
Sun Dec 23 11:23:34 CST 2018

进行时间同步

1
2
# ntpdate 10.186.61.39
27 Feb 14:33:46 ntpdate[23929]: step time server 10.186.61.39 offset 5713761.244970 sec

10.186.61.39即位搭建的内网时间服务器的地址
可以看到时间已经同步成功。

修改时区

当前系统版本为centos7
时间同步后可以查看搭建的时间服务器的本机时间

1
2
# date
Wed Feb 27 06:23:10 UTC 2019

UTC是世界统一时间,我们可以更改为东八区北京时间CST
更改前

1
2
# date
Wed Feb 27 06:25:59 UTC 2019

更改

1
2
mv /etc/localtime /etc/localtime.bak
ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime

更改后

1
2
# date
Wed Feb 27 14:26:29 CST 2019

本文标题:搭建内网NTP时间服务器

文章作者:Francis

原始链接:http://www.cnops.com/posts/aec0db0.html

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。