历史版本9 :Linux系统配置各个节点的时间一致性 返回文档
编辑时间: 内容长度:图片数:目录数: 修改原因:

目录:

方案一、使用 date 命令调整时间编辑

date #查看当前系统时间和日期
date -s 02/21/2019 #设置日期,例如将系统日期设定成 2019 年 02 月 21 日
date -s 19:21:45 #设置时分秒,例如将系统时间设定成 19 点 21 分 45 秒
hwclock -w #将当前时间和日期写入 BIOS,即设置为硬件时间,避免重启后失效

注意:手动设置时间会存在一定的误差,所以调整各个系统时间时,建议参照秒表设置,尽量降低误差值。

方案二、使用 NTP 调整系统时间编辑

NTP(Network Time Protocol,网络时间协议),是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源(如石英钟,GPS 等等)做同步化,并提供高精准度的时间校正(LAN 上与标准间差小于1毫秒,WAN 上几十毫秒),且可通过加密确认的方式来防止恶毒的协议攻击。

1. 检测 ntp 是否已安装编辑

rpm -qa | grep ntp #检查命令

若只有 ntpdate 而未见 ntp,如:

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

则需删除原有 ntpdate:

rpm -e ntpdate-4.2.6p5-22.el7.x86_64 #删除 ntpdate

2. 联网环境——用ntpdate自动更新系统时间编辑

2.1 采用微软的校时服务器调整系统时间

yum install -y ntp #安装 ntp 服务器
ntpdate time.windows.com #采用微软的校时服务器调整系统时间

出现如下图所示内容,说明已经同步成功了。

1.png

2.2 设置自动同步,同步频率:每十分钟同步一次

crontab -e #编辑crontab

按 i 键,进入编辑模式,增加如下内容:

*/10 * * * * /usr/sbin/ntpdate time.windows.com >> /tmp/crontab.log

按 ESC,然后 Shift+Q,再输出 wq 即可成功保存。

3. 内网环境——用 ntp 搭建自己的时间服务器编辑

示例环境:主节点 IP-192.168.61.250,子节点 IP-192.168.61.251

3.1 安装ntp

集群下各个节点均需要进行安装,离线安装方式:

RedHat&CentOS 的系统可直接使用以下 rpm 包, 或者自行下载相应的 rpm 包

安装包.rar 

下载安装包后,上传到服务器指定目录,例如上传到/usr/ntp目录下,然后执行以下安装命令:

cd /usr/ntp
rpm -Uvh *.rpm --nodeps --force #一键安装目录下所有的 rpm 包

3.2 修改所有节点的 /etc/ntp.conf

vi /etc/ntp.conf

找到 #restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap,去掉注释#,内容修改为:

restrict 192.168.6.2 mask 255.255.255.0 nomodify notrap
# 192.168.6.2 为示例,请按照实际集群所在网段的网关IP填写,255.255.255.0 为子网掩码,查询网段网关的命令为 IP route show,查询结果中default via 后面的 IP 即是网关 IP

3.3 配置 ntp 服务器

选择一个节点作为主节点即 ntp 服务器,修改其/etc/ntp.conf

vi /etc/ntp.conf

注释掉 server 0 ~ n,并在 server 部分添加 server 和 fudge行,如下:

#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 127.127.1.0 #使用本地时钟作为ntp服务器时间,这里的 127.127.1.0 在 ntp 中代表本机 
fudge 127.127.1.0 stratum 10 #127.127.1.0 为第10层,ntp 和127.127.1.0同步完后,就变成了 11 层

3.4 配置ntp客户端

其他节点作为子节点,需要配置 ntp 客户端,修改其/etc/ntp.conf

vi /etc/ntp.conf

注释掉 server 0 ~ n,并在 server 部分添加 server 行,如下:

#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 192.168.61.250 #使用主节点 192.169.61.250 的时间作为基准更新时间

3.5 ntp 启动命令

1)主节点启动命令
service ntpd start #打开 ntpd 服务 
chkconfig ntpd on #设置开机启动
2)子节点启动命令
ntpdate 192.168.61.250 #向主节点发送请求,同步时间 
service ntpd start #打开 ntpd 服务 
chkconfig ntpd on #设置开机启动
3)查看 ntp 服务器有无和上层 ntp 连通

使用 ntpstat 行查询,结果一般如下:

2.png

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

使用 ntpq -p 进行查询,结果一般如下:

3.png

4. 运维知识编辑

4.1 常用命令

service ntpd status  #查看 ntpd 服务状态
service ntpd start  #启动 ntpd 服务
service ntpd stop  #停止 ntpd 服务
service ntpd restart  #重启 ntpd 服务

4.2 检查同步是否成功

查看与时间同步服务器的时间偏差,offset 偏差小,同步成功。

111111111.png

5. FAQ文档编辑

5.1 内网环境安装后,使用 ntpstat 查看 ntp 服务器有无和上层 ntp 连通时,可能会出现

如下情况

1)unsynchronised time server re-starting polling server every 8 s

4.png

2)unsynchronised polling server every 8 s

5.png

这种情况属于正常,ntp 服务器配置完毕后,需要等待 3~5 分钟才能与/etc/ntp.conf中配置的标准时间进行同步,等一段时间之后,再次使用 ntpstat 命令查看状态,就会变成正常结果。

5.2 ntp 启动命令,启动完主节点,再启动子节点时,可能出现如下情况 no server 

suitable for synchronization found:

333333.png

在 ntp server上重新启动ntp服务后,ntp server 自身或者与其 server 的同步的需要一个时间段,大约三五分钟,在这个时间之内在客户端运行 ntpdate 命令时会产生 no server suitable for synchronization found 的错误。

5.3 手动更改主节点的时间,重启 ntp,子节点时间不同步

存在 3~5 分钟的生效时间.