Linux 日期同步
时间:2020-07-11 18:14:14
参考:
环境:
- CentOS 7
名词解释:
- 硬件时间:时间写入到 BIOS 中,由主板电池供电,当系统关机之后时间也会一直走下去。
- 系统时间:开机时从BOIS读取时间,开机之后一直走下去直到关机。
注:文档里面的授时服务器不可用时,可以在网上搜索可用的授时服务器,然后通过 ntpdate 0.cn.pool.ntp.org
验证服务器是否可用。
注:同步时间命令 ntpdate -u cn.pool.ntp.org
NTP 时间同步#
在各服务器或集群中的不同机器之间需要保持时间一致以及时间的精确性(和实际时间相同),以避免因为时间不一致导致的各种问题。
小田有三台服务器,服务器A(192.168.0.201)
、服务器B(192.168.0.202)
和服务器C(192.168.0.202)
。需要三台服务器之间保持时间一致,以及保持时间精确。
服务器A
从标准授时服务器同步时间(对表)以保证自己时间的精确性。同时做为授时服务器向服务器B
和服务器C
提供授时服务。服务器B
和服务器C
从授时服务器A
同步时间以保证服务器之间时间的的一致性。
服务器A
从标准授时服务器获取时间,保证时间的精确性。服务器B
和服务器C
都从服务器A
同步时间,保证各服务器之间时间的一致性。
NTP (Network Time Protocol) 网络时间协议是用于服务器(计算机)之间时间同步的协议。在 CentOS 下有对应的软件可以通过 yum install ntp
安装,安装之后修改配置即可使用。
NTP 简介#
网络时间同步协议,基于UDP协议实现,端口号 123。 同步时间的过程是渐进式的,比如两个计算机之间从相差10秒到时间同步可能经历很长的时间,这样可以避免时间跳跃产生的穿越问题,因此启动服务之前需要先进行一次手动的时间同步。
NTP 安装和使用#
服务器A
作为授时服务器,服务器B
和服务器C
从服务器A
同步时间。
-
修改时区以及设置时间。
# 查看时间及时区信息 [root@test etc]# timedatectl Local time: Sun 2020-07-12 17:38:16 CST 系统时间 Universal time: Sun 2020-07-12 09:38:16 UTC UTC时间 RTC time: Sun 2020-07-12 09:40:40 硬件时间 Time zone: Asia/Chongqing (CST, +0800) 时区 NTP enabled: no ntp 服务是否开启 NTP synchronized: no ntp 时间是否同步 RTC in local TZ: no 硬件时间是否和本地时间时区相同 DST active: n/a # 修改时区 [root@test etc]# timedatectl set-timezone Asia/Shanghai # 设置本地时间 [root@test etc]# timedatectl set-time "2020-07-12 17:45:45"
-
安装 ntp
-
修改配置文件
/etc/ntf.conf
。-
修改服务器A的配置文件,只列出注释和修改的内容。
# Hosts on local network are less restricted. # 允许本地局域网内服务器和服务器A通信 # ignore 关闭所有联网服务 nomodify 不能修改 notrust 如果认证不通过则为不可信 noquery 不提供时间查询 restrict 192.168.0.0 mask 255.255.255.0 nomodify notrap # 限制上层服务器的权限 restrict 0.cn.pool.ntp.org nomodify notrap noquery restrict 1.cn.pool.ntp.org nomodify notrap noquery restrict 2.cn.pool.ntp.org nomodify notrap noquery restrict 3.cn.pool.ntp.org nomodify notrap noquery # Use public servers from the pool.ntp.org project. # 授时中心服务器地址 server 0.cn.pool.ntp.org server 1.cn.pool.ntp.org server 2.cn.pool.ntp.org server 3.cn.pool.ntp.org # 授时中心不可用时使用本机时间 server 127.127.1.0 fudge 127.127.1.0 stratum 10 # 同步时间后同步硬件时间(在最后一行添加) SYNC_HWCLOCK=yes
-
修改服务器B和服务器C的配置文件。
-
-
启动服务。