centos时间配置详解

时间同步的两种方式chrony vs ntp

ntp:传统的时间同步配置,既可以当服务端,也可以当客户端

chrony:新式时间配置,采用微调修改同步时间

常见命令

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#查看时间
[root@master ~]# date
#查看时间详情
[root@master ~]# timedatectl
Local time: 三 2024-01-10 00:27:35 CST #本地时间,初始值来自于RTC,通常等于RTC+时区值(如本地时间= RTC + 8小时)
Universal time: 二 2024-01-09 16:27:35 UTC #世界时间,中国时区=UTC+8
RTC time: 二 2024-01-09 17:50:38 #BIOS时间,硬件时间
Time zone: Asia/Shanghai (CST, +0800) #时区
NTP enabled: yes #表示开启NTP同步
NTP synchronized: no #NTP是否同步完成
RTC in local TZ: no #no=RTC时间用UTC,yes=RTC时间用LOCAL time
DST active: n/a
# 默认情况下,系统配置为使用UTC,也可使用本地时间
[root@master ~]# timedatectl set-ntp true/false #开启关闭ntp
[root@master ~]# timedatectl set-local-rtc true # 将RTC设置为本地时间
[root@master ~]# timedatectl set-local-rtc false # 将RTC设置为UTC
[root@master ~]# chronyc tracking |grep System # 查看服务器时间和NTP server偏差
System time : 23735.990234375 seconds fast of NTP time
[root@master ~]# chronyc activity #显示有多少NTP源在线/离线
200 OK
4 sources online
0 sources offline
[root@master ~]# chronyc makestep #立即手动同步时间(需要chronyd服务运行)

[root@master ~]# hwclock -w #将当前时间和日期写入BIOS,避免重启后失效(改了之后恢复)
[root@master ~]# hwclock --systohc #将系统时间写入RTC(待观察)
[root@master ~]# hwclock --localtime #显示 BIOS 中实际的时间
2024年01月09日 星期二 18时10分55秒 -0.062371 秒
[root@master ~]# systemctl status chronyd.service #查看chronyd服务是否启动

[root@master ~]# ntpdate cn.pool.ntp.org #ntp同步时间
[root@master ~]# timedatectl set-time 2023-12-28 #手动设置时间,需要关闭自动同步时间
[root@master ~]# timedatectl set-ntp no
[root@master ~]# timedatectl set-time 2023-12-28
[root@master ~]# timedatectl set-time 16:06:17
[root@master ~]# timedatectl set-timezone Asia/Shanghai #设置时区

问题

  1. 修改时间过后,时间过一段时间又不对了

    分析,怀疑是chronyc慢慢修正成了错误时间

    解决过程:

    1. 通过把chronyc停用,换成ntpd进行同步时间,过了一天,时间还是改变了(RTC+8),发现ntpd服务在3点的时候停止了

    2. 添加时间ntpd服务是否运行的检测的脚本check_ntp.sh

      1
      2
      3
      4
      5
      6
      7
      8
      #!/bin/bash

      # 检查 NTP 服务是否正在运行
      if systemctl is-active --quiet ntpd; then
      echo "NTP service is running."
      else
      echo "NTP service is not running."
      fi
    3. 添加时间是否同步time_sync_status.sh

      1
      2
      3
      4
      5
      6
      7
      8
      #!/bin/bash

      # 检查时间是否同步
      if timedatectl | grep "NTP synchronized: yes" > /dev/null; then
      echo "System time is synchronized."
      else
      echo "System time is not synchronized."
      fi
    4. 整合服务检测,与时间同步

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      #!/bin/bash

      LOG_FILE="/var/log/sync_time.log"

      # 检查系统时间是否正确
      if timedatectl | grep "NTP synchronized: yes" > /dev/null; then
      echo "$(date): System time is synchronized." >> "$LOG_FILE"
      echo "$(timedatectl)" >> "$LOG_FILE"
      else
      echo "$(date): System time is not synchronized." >> "$LOG_FILE"
      echo "$(timedatectl)" >> "$LOG_FILE"
      # 检查 NTP 服务状态
      if systemctl is-active --quiet ntpd; then
      echo "$(date): NTP service is running. Synchronizing time..." >> "$LOG_FILE"
      echo "$(timedatectl)" >> "$LOG_FILE"
      systemctl stop ntpd # 停止 NTP 服务,以便手动同步时间
      ntpd -gq # 强制同步时间
      hwclock --systohc # 同步时间到 RTC
      systemctl start ntpd # 重新启动 NTP 服务
      echo "$(date): Time synchronized successfully." >> "$LOG_FILE"
      echo "$(timedatectl)" >> "$LOG_FILE"
      else
      echo "$(date): NTP service is not running. " >> "$LOG_FILE"
      echo "$(timedatectl)" >> "$LOG_FILE"
      ntpd -gq # 强制同步时间
      hwclock --systohc # 同步时间到 RTC
      systemctl start ntpd # 重新启动 NTP 服务
      echo "$(date): Time synchronized successfully." >> "$LOG_FILE"
      echo "$(timedatectl)" >> "$LOG_FILE"
      fi
      fi
    5. 创建一个定时任务crontab -e

      1
      */1 * * * * /root/sync_time.sh
    6. 后面观察日志tail -f /var/log/sync_time.log即可,执行more /var/log/sync_time.log | grep " is not " -A 28 -B 10进行查看什么时候通不过时间

参考:

chrony时间同步服务