服务器时钟同步

服务器时钟同步

文章目录

背景

@运维,XXX服务器慢了2秒,导致XXX业务没有正常执行,请立即排查为啥会有时钟不同步的问题。

首先说下时间为啥会不准,时间不准的原因有很多,有以下几种可能

  1. 硬件时钟故障
  2. BIOS的电池故障
  3. 时钟同步问题

还有很多问题,总而言之就是时间不准确了,怎么能将时间尽可能的标准呢,来看看我们遇到的问题。

windows时钟同步

首先将自动设置时间要打开

设定要同步的的服务器,可以选择官方的也可以选择自己的同步服务器,但记得要先telnet一下,端口是123

默认同步时间是1天1次,本次同步结束后,24小时后再同步一次

如果机器对时间的要求比较严格,要进行定时同步时间,设置定时同步可以这么设置

找到任务计划程序(我不知道哪里进,点了win搜索就是了)

创建个目录或者在

\Microsoft\Windows\Time Synchronization

目录下设置时间同步

设置任务的目的主要是执行命令

设置触发器,选定你要同步的时间,跟Linux里的crontab有点相似,这个是界面的,更容易理解

主要是操作这些

同步时间的命令

复制代码
w32tm /resync

检查windows中时差的命令是

复制代码
# 203.107.6.88是阿里云的时钟服务器
cmd /k "w32tm /stripchart /computer:203.107.6.88 /samples:2 /dataonly"

也可以采用微软的服务器,自己公司内部的服务器

如果需要看到时间差的情况,可以使用钉钉、飞书、邮件等相关软件进行及时通知,或者超过某个阈值的时候进行通知,这些都是可行的。一般只要时间同步问题就不大。

Linux机器上的时钟同步

Centos时钟同步

采用Centos默认的时钟同步策略,在装系统时已经装了,查看其命令的

shell 复制代码
# 查看chronyd的状态
[root@localhost log]# systemctl status chronyd
● chronyd.service - NTP client/server
   Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
   Active: active (running) since Fri 2023-07-21 18:35:48 CST; 2 weeks 0 days ago
     Docs: man:chronyd(8)
           man:chrony.conf(5)
  Process: 1593 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
  Process: 1569 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
 Main PID: 1585 (chronyd)
    Tasks: 1
   CGroup: /system.slice/chronyd.service
           └─1585 /usr/sbin/chronyd

Jul 21 18:35:48 localhost systemd[1]: Starting NTP client/server...
Jul 21 18:35:48 localhost chronyd[1585]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +SECH...6 +DEBUG)
Jul 21 18:35:48 localhost  chronyd[1585]: Frequency 4.080 +/- 0.147 ppm read from /var/lib/chrony/drift
Jul 21 18:35:48 localhost systemd[1]: Started NTP client/server.
Jul 21 18:35:58 localhost chronyd[1585]: Selected source 193.182.111.12
Jul 21 18:35:59 localhost chronyd[1585]: Selected source 202.38.64.7
Jul 21 18:36:02 localhost chronyd[1585]: Source 193.182.111.12 replaced with 84.16.73.33
Jul 26 17:50:53 localhost chronyd[1585]: Source 202.112.29.82 replaced with 119.28.206.193
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost log]# 

配置文件在

shell 复制代码
[root@localhost etc]# vim /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 所需同步服务器的域名
server 0.centos.pool.ntp.org iburst
......

# Record the rate at which the system clock gains/losses time.
# driftfile指定了用于记录系统时钟的漂移量的文件路径。Chrony使用该文件来记录系统时钟的精度和准确度,以便在下次启动时进行校准。
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
# makestep配置指定了系统时钟在前三次更新时是否允许快速调整(stepping),当系统时钟的偏差超过1秒时,允许进行快速调整。
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
# rtcsync启用了对硬件RTC(实时时钟)的内核同步。Chrony将尝试让系统时钟和硬件RTC保持同步。

其中要注意,同步的服务器要保证和是能通信的

Ubuntu系统时钟同步

shell 复制代码
(base) test@localhost:~$ systemctl status systemd-timesyncd
● systemd-timesyncd.service - Network Time Synchronization
     Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
     Active: active (running) since Mon 2023-07-24 18:28:19 CST; 1 week 4 days ago
       Docs: man:systemd-timesyncd.service(8)
   Main PID: 838 (systemd-timesyn)
     Status: "Initial synchronization to time server 185.125.190.57:123 (ntp.ubuntu.com)."
      Tasks: 2 (limit: 57665)
     Memory: 964.0K
        CPU: 3.313s
     CGroup: /system.slice/systemd-timesyncd.service
             └─838 /lib/systemd/systemd-timesyncd

Warning: some journal files were not opened due to insufficient permissions.

Ubuntu 默认同步的主机是pool.ntp.org

默认同步时间最大是2048秒,最小同时间隔是32秒,大约每间隔1分钟同步一次

查看是否同步的命令

无论Ubuntu还是Centos查看是否同步的命令都是

乌班图查看

shell= 复制代码
(base) test@localhost:/etc$ timedatectl
               Local time: Fri 2023-08-04 19:30:36 CST
           Universal time: Fri 2023-08-04 11:30:36 UTC
                 RTC time: Fri 2023-08-04 11:30:36
                Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
(base) test@localhost:/etc$ 

Centos查看

shell 复制代码
[root@localhost ~]# timedatectl
      Local time: Fri 2023-08-04 19:31:20 CST
  Universal time: Fri 2023-08-04 11:31:20 UTC
        RTC time: Fri 2023-08-04 11:31:20
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a
[root@localhost ~]# 

计算机不是机械的,没有大自然的神奇,底层是电压的高低位模拟出来的,未免有偏差,偏差只要做好同步和监控,时间偏差会降到最低,防止因为时间偏差带来的麻烦。

相关推荐
ulias2123 小时前
Linux系统中的权限问题
linux·运维·服务器
青花瓷4 小时前
Ubuntu下OpenClaw的安装(豆包火山API版)
运维·服务器·ubuntu
问简5 小时前
docker 镜像相关
运维·docker·容器
Dream of maid6 小时前
Linux(下)
linux·运维·服务器
齐鲁大虾6 小时前
统信系统UOS常用命令集
linux·运维·服务器
Benszen6 小时前
Docker容器化技术实战指南
运维·docker·容器
ZzzZZzzzZZZzzzz…6 小时前
Nginx 平滑升级:从 1.26.3 到 1.28.0,用户无感知
linux·运维·nginx·平滑升级·nginx1.26.3·nginx1.28.0
一叶知秋yyds7 小时前
Ubuntu 虚拟机安装 OpenClaw 完整流程
linux·运维·ubuntu·openclaw
专吃海绵宝宝菠萝屋的派大星8 小时前
使用Dify对接自己开发的mcp
java·服务器·前端
斯普信云原生组8 小时前
Prometheus 环境监控虚机 Redis 方案(生产实操版)
运维·docker·容器