前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >服务器如何同步网络时间

服务器如何同步网络时间

作者头像
震八方紫面昆仑侠
发布2020-08-12 14:32:24
5.3K0
发布2020-08-12 14:32:24
举报

网络时间同步协议(NTP)是时间同步的技术基础。Linux操作系统都默认安装了ntp组件。如果服务器能访问外网并且配置了DNS服务器地址,直接ntpdate 0.cn.pool.ntp.org就可以同步外网时间。但很多服务器出于安全考虑,虽然通外网,但没有配置DNS,这时需要直接从时间服务器的ip地址同步时间了。可以使用阿里云(ntp1.aliyun.com)的时间服务器ip直接同步,ntpdate 120.24.81.91,或者清华的时间服务器84.16.73.33。 服务器第一次同步时间显示如下

代码语言:javascript
复制
[root@localhost ~]# ntpdate 120.24.81.91
10 Aug 09:46:07 ntpdate[15071]: step time server 120.24.81.91 offset 1.423469 sec

之后每次执行同步时间命令显示和第一次是不一样的

代码语言:javascript
复制
[root@localhost ~]# ntpdate 120.24.81.91
10 Aug 14:16:14 ntpdate[12150]: adjust time server 120.24.81.91 offset -0.030012 sec

假设A B两台Linux服务器。 A是开通了外网的,虽然没配置DNS服务器,但能ping通120.24.81.91。 B没开通外网,ping 120.24.81.91会报connect: Network is unreachable,这种肯定无法从外网服务器同步时间。如果执行ntpdate 120.24.81.91,会报no server suitable for synchronization found,或者no servers can be used, exiting。

最简单的办法是A从外网校正过时间后,把A配置成ntp服务器(NTP server),B从A同步时间。配置ntp服务器首先修改配置文件ntp.conf确保各项正确,然后执行# /etc/init.d/ntpd start启动ntp服务器,等待5分钟后,在B上执行# ntpdate AIP (AIP是A的内网IP地址),就可以同步A的系统时间到B,间接同步了外网时间。 配置A为NTP server,首先修改配置文件,配置文件/etc/ntp.conf修改完成后执行如下指令,除了192.168.2.0改成你自己局域网的网段,其余要求必须和下面一样,有比下面多出的语句就注释掉,没有的语句就加上。

代码语言:javascript
复制
#cat /etc/ntp.conf |awk '{if($0 !~ /^$/ && $0 !~ /^#/) {print $0}}'  

restrict default ignore                                //#设置默认策略为允许任何主机进行时间同步
restrict 127.0.0.1                                         //给于本机所有权限
restrict 192.168.2.0 mask 255.255.255.0 nomodify notrap     //给于局域网机的机器有同步时间的权限
server 0.127.127.1.0               //设置时间服务器为本机,可以设为120.24.81.91外网服务器
server  127.127.1.0  # local clock
fudge  127.127.1.0 stratum 10
driftfile /var/lib/ntp/drift
broadcastdelay  0.008
keys    /etc/ntp/keys

然后执行/etc/init.d/ntpd start,会显示OK,表示成功。如果配置文件反复修改,执行/etc/init.d/ntpd restart更方便重新加载配置文件。 等待5分钟,这个时间是给NTP server同步本机时间用的。然后在B.上执行ntpdate 192.168.2.10 [root@DB1 ~]# ntpdate 192.168.2.10 10 Aug 13:35:59 ntpdate[10737]: adjust time server 192.168.2.10 offset 0.004937 sec 表示同步时间成功,多次执行offset会变小,要求不高的话,执行一次误差就已经在千分之几秒范围内了。 如果ntpdate 192.168.2.10返回错误no server suitable for synchronization found,可能是NTP server本身时间还没同步好。可以使用ntpdate –d serverIP指令查看。

代码语言:javascript
复制
[root@DB1 ~]# ntpdate -d 192.168.2.10
10 Aug 13:28:07 ntpdate[10719]: ntpdate 4.2.0a@1.1190-r Thu Oct  5 04:11:32 EDT 2006 (1)
Looking for host 192.168.2.10 and service ntp
host found : 192.168.2.10
transmit(192.168.2.10)
receive(192.168.2.10)
省略
192.168.2.10: Server dropped: strata too high
server 192.168.2.10, port 123
stratum 16, precision -20, leap 11, trust 000
refid [192.168.2.10], delay 0.02573, dispersion 0.00000
省略

有“Server dropped: strata too high”的提示,并且“stratum 16”。stratum的正常范围是“0~15”。这是我们什么都不用做,等一段时间再执行指令试试,就会变成stratum 11, precision -20, leap 00, trust 000。stratum 11是正常范围,此时执行ntpdate 192.168.2.10就成功校正时间了。 另外,A和B的防火墙都关闭,整个过程用root账号操作。没有配置A自动同步外网时间是出于安全考虑,B需要频繁校正时间的话,crontab配置ntpdate指令可以达到目的。 crontab -e 9 7 * * * /usr/sbin/ntpdate 192.168.2.10 系统时间同步到硬件时间也都可以根据需要设定。硬件时间保持和系统时间一致的好处是可以避免重启服务器后硬件时间覆盖系统时间导致误差。

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-08-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 MySQL从删库到跑路 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档