linux上内网环境配置NTP时间同步详解~【基于chrony服务】、chrony不能同步时间排错详解、timedatactl命令修改时间和时区等说明

内网搭建说明

  • 我可气死了,因为我这是在内网环境搭建的ntp嘛,所以真的搭建起来一堆问题 费我一天时间了,各种百度找文献,没有一篇文章是说内网环境的,而且网上文章的搭建服务真的超级简单,也不会出现不同步这种情况,我这各种照着网上的方法来弄,就是不同步。。。

  • 网上不同步的的原因,翻来覆去就是那几种情况,可是我这都排除了。。。唉心累。

  • 但是 网上没人发布内网环境搭建chronyd服务,我这不就发布了嘛~ 过程可谓一波三折了,但是不要紧,反正我最终各种尝试,弄出了真正可行的内网搭建ntp服务方法,你如果内网搭建遇到问题,直接留言,一起沟通学习。

  • 另外,你在网上其他地方看的chrony,是不是感觉都很简单? 可是我用了2.5万字数来做说明,你觉得这个还简不简单?
    在这里插入图片描述

NTP说明

  • NTP 是 Network Time Protocol 的缩写,也即 网络时间协议,一种在 Linux 上保持准确时间的协议,它和网络上可用的 NTP 服务器保持着时钟同步

  • 在 Linux 中,保持准确的日期和时间至关重要,因为许多服务(如 cron 作业和脚本)依赖于准确的时间才能得到预期的结果

  • 用于同步日期和时间的 ntpd 服务,在新的Linux发行版 ( centos8、Ubuntu 20.04、Fedora 30 ) 中已经废弃了,取而代之的是chrony【低版本这个方法也是能用的】

  • 当然,选择 chrony 是有原因的,相比 ntpd , 它有如下的优点:

    • 1、时间同步的速度比 ntpd 更快

    • 2、chrony 很好的处理了同步延迟以及网络延迟

    • 3、即使出现网络降级,chrony 仍然能正常工作

    • 4、本地机器可以作为时间服务器,其他机器从这台服务器上同步时间

chrony说明

chrony的组成

  • chrony 是由 守护进程 chronyd 以及 命令行工具 chronyc 组成的,具体如下图
    在这里插入图片描述

  • chronyd 在后台静默运行并通过 123端口与时间服务器定时同步时间,默认的配置文件是 /etc/chrony.conf

  • chronyc 通过 323 端口与 chronyd 交互,可监控 chronyd 的性能并在运行时更改各种操作参数
    chronyc 通过下面的方式访问 chronyd

    • 1、通过 IPv4 或 IPv6 访问

    • 2、通过 Unix 域 socket, 但只能访问到本地的 chronyd,而且需要 root 用户或者 chrony 用户才能访问

  • 默认情况下,chronyc 先通过 Unix 域 socket 访问 chronyd,默认的 socket 文件是 /var/run/chrony/chronyd.sock, 如果失败(常见的原因是使用非 root 用户运行 chronyc ),将尝试通过 127.0.0.1 访问 chronyd

chrony安装和命令管理说明

  • 在新的 Linux 发行版(centos8、Ubuntu 20.04、Fedora 30)及以后的版本中,系统默认已经安装了 chrony,在这之前的版本是没有安装的,可以使用下面的命令进行安装
    yum install chrony -y
  • 安装完成后,chrony 服务默认会自动加到 systemctl 中管理,下面列出了一些常用的命令
#查询 chronyd 服务状态
systemctl status chronyd     

#启动 chronyd 服务
systemctl start chronyd   

#关闭 chronyd 服务
systemctl stop chronyd   

#重启 chronyd 服务
systemctl restart chronyd   

#设置 chronyd 服务开机自启
systemctl enable chronyd
systemctl daemon-reload

启动服务报错处理

另外,启动 chronyd 服务的时候如果出现下面截图中的错误,需要安装或者升级libsepolpolicycoreutils-python【最小化安装的系统可能会出现这种问题】
查看日志或者执行journalctl -xe均可查看报错内容~
在这里插入图片描述

  • 具体的问题说明详见Red Hat Bugzilla – Bug 1592775

  • 解决方法 安装 libsepol 和 policycoreutils-python
    yum install libsepol policycoreutils-python -y

chrony常用配置参数说明

  • 守护进程 chronyd 的默认配置文件是/etc/chrony.conf,其中可配置项很多,这里介绍一些常用的
配置项 说明
server 客户端需找哪个服务器询问当前时间
pool 同 server 配置项
driftfile 本地时钟和服务器时钟的预估漂移保存到哪个文件中
makestep 纠正客户端时间的步进参数
rtcsync 是否允许内核同步实时时钟
allow 允许客户端通过内网地址同步时钟
logdir 日志目录

chrony配置时间同步

虚拟机使用说明

  • 守护进程 chronyd 既可作为客户端 与 服务器同步时间,又可作为一个服务器,接受其他客户端同步时间的请求

  • 下面以配置局域网时间服务器为例来说明 chronyd 的客户端和服务器的配置,虚拟机规划如下:

用途 ip 系统 配置前系统时间
服务端ip 192.168.59.128 CentOS Linux release 7.6.1810 (Core) 2022年 02月 28日 星期一 19:41:38 CST
客户端ip 192.168.59.129 CentOS Linux release 7.6.1810 (Core) Mon Feb 28 06:46:54 EST 2022

服务端配置

  • 注:我上面2台虚拟机没有通公网的,所以我下面安装服务用的是本地源方式搭建。

安装chrony

[root@ccx_server128 ~]# yum install -y chrony
[root@ccx_server128 ~]# 
[root@ccx_server128 ~]# systemctl enable chronyd --now
[root@ccx_server128 ~]# systemctl is-active chronyd
active
[root@ccx_server128 ~]# 

服务端配置

  • 安装 chrony 之后,默认的配置是客户端的启动配置的,要想作为一个时间服务器来运行的话, 需要在配置中增加 allow 配置项,它表示允许客户端通过该地址和服务器同步时间
    默认配置文件:/etc/chrony.conf

  • 另外,时间服务器的时间也需要从网络上其他的时间服务器进行同步,这里直接用默认的即可,具体的配置如下
    【其实我没做啥修改,就把allow取消注释,然后加上服务端的ip即可】
    我给没注释的代码都加上说明了,可以看看哦。

[root@ccx_server128 ~]# cat /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
#server 1.centos.pool.ntp.org iburst
#server 0.centos.pool.ntp.org iburst
#server 1.centos.pool.ntp.org iburst
#表示与本机IP同步时间,其他server注释或删除
#注:内网环境必须这么做,否则其他会无法同步成功。 如果通公网,可以就用上面默认的
server 127.127.1.0

# Record the rate at which the system clock gains/losses time.
# 为了在下次启动时稳定的同步,系统时钟的预估漂移需要保存到指定的文件中
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.
# 如果系统时钟由于某种原因与启动后的服务器时间相差甚远,允许 chronyd 
# 通过步进而不是回转来快速纠正它,这个过程将花费很长时间
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
# 为了使客户端实时时钟接近服务器的时钟,以便下次时钟启动时更接近真实的时间
# 增加了一种 rtcsync 模式,该模式下,系统时间会定期的拷贝到实时时钟里
rtcsync

# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# Allow NTP client access from local network.
# 允许指定网络的主机同步时间,不指定就是允许所有,默认不开启。
#如果有多个网段,则添加多个allow
allow 192.168.59.0/24
#allow ...

# Serve time even if not synchronized to a time source.
# 不去同步任何人的时间,时间同步服务级别
#注:内网环境必须开,否则其他虚拟机无法同步时间
local stratum 10

# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# Specify directory for log files.
# chrony日志存放路径
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking
[root@ccx_server128 ~]# 

pool说明

  • pool=server。 一般我们都直接用server,只是我这说明一下pool的意思罢了。
  • pool 配置项
    • 客户端需要去时间服务器获取时间,配置文件中 server 和 pool 配置项表示时间服务器的地址,支持域名或者IP
    • pool指的是进行时间同步的服务器IP地址或域名,作为服务器来说,其机器时间也需要从其他时间服务器同步,此时,服务器是作为一个客户端从网络服务器上获取时间
    • 建议配置多个时间服务器的地址,优先选择同步良好,网络稳定且靠近客户端的地址

防火墙设置

  • 可以先查看防火墙状态,如果是关闭的,那么下面操作不用做了,我的是测试机,是可以直接关闭防火墙的【如果客户端已配置,则关闭服务端防火墙后,客户端重启 systemctl restart chronyd
    以firewalld为例
[root@ccx_server128 ~]# ip a | grep 128
    inet6 ::1/128 scope host 
    inet 192.168.59.128/24 brd 192.168.59.255 scope global noprefixroute ens33
[root@ccx_server128 ~]# systemctl is-active firewalld.service 
active
[root@ccx_server128 ~]# systemctl stop firewalld.service
[root@ccx_server128 ~]# 
[root@ccx_server128 ~]# systemctl is-active firewalld.service 
inactive
[root@ccx_server128 ~]# 
  • 时间服务器如果有开启防火墙的话,需要开启 UDP 协议 的 123 端口,以允许客户端向服务器发送获取时间的请求
    以firewalld为例
[root@ccx_server128 ~]# firewall-cmd --permanent --add-port=123/udp
success
[root@ccx_server128 ~]# firewall-cmd --reload
success
  • 如果想要关闭防火墙的 123 端口,在服务器机器上执行下面的命令
    以firewalld为例
[root@ccx_server128 ~]# firewall-cmd --permanent --remove-port=123/udp
success
[root@ccx_server128 ~]# firewall-cmd --reload
success

重启chrony

为使配置生效,需要重启 chronyd 服务

[root@ccx_server128 ~]# systemctl restart chronyd
[root@ccx_server128 ~]# systemctl is-active chronyd
active
[root@ccx_server128 ~]# 

查看同步的客户端信息

  • 服务端命令:chronyc clients【查看哪些ip连接我的ntp时间了】
    注:我下面显示的是主机名而不是ip,是因为我添加解析了!!!如果没添加解析,显示的是ip。
# 注:要有客户端连了才有该信息,没人连是为空。
[root@ccx_server128 ~]# chronyc clients
Hostname                      NTP   Drop Int IntL Last     Cmd   Drop Int  Last
===============================================================================
ccx_server129                  16      0   6   -    15       0      0   -     -
[root@ccx_server128 ~]# 
[root@ccx_server128 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.59.129 ccx_server129
[root@ccx_server128 ~]# 

客户端配置

安装chrony

[root@ccx_server129 ~]# yum install -y chrony
[root@ccx_server129 ~]# 
[root@ccx_server129 ~]# systemctl enable chronyd --now
[root@ccx_server129 ~]# systemctl is-active chronyd
active
[root@ccx_server129 ~]# 

客户端配置

  • 安装好 chrony 之后,编辑 /etc/chrony.conf配置文件,客户端的配置比较简单
    我给没注释的代码都加上说明了,可以看看哦。
[root@ccx_server129 ~]# cat /etc/chrony.conf
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# 同步时间的服务器 IP 或 域名
# 这直接使用服务端的ip即可
server 192.168.59.128 iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

# Record the rate at which the system clock gains/losses time.
# 系统时钟的预估漂移保存到指定的文件中,是为了在下次启动时能稳定的同步
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.
# 如果系统时钟由于某种原因与启动后的服务器时间相差甚远,允许 chronyd 
# 通过步进而不是回转来快速纠正它
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
# 为了使客户端实时时钟接近服务器的时钟,以便下次时钟启动时更接近真实的时间
# 增加了一种 rtcsync 模式,该模式下,系统时间会定期的拷贝到实时时钟里
rtcsync

# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# Allow NTP client access from local network.
#allow 192.168.0.0/16

# Serve time even if not synchronized to a time source.
#local stratum 10

# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# Specify directory for log files.
# chrony日志存放路径
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking
[root@ccx_server129 ~]# 

重启chrony

为使配置生效,需要重启 chronyd 服务

[root@ccx_server129 ~]# systemctl restart chronyd
[root@ccx_server129 ~]# systemctl is-active chronyd
active
[root@ccx_server129 ~]# 

查看服务器信息

  • 客户端查看连接情况命令:chronyc sources -v【加-v显示详细信息,可以不加】
    如下,我没同步前时间为15:28,同步以后时间变成23:22了【和服务端一样】
    显示的是主机名而非ip是因为我添加解析了。。。没添加解析是显示的ip
    • ^?同步失败
    • ^*同步成功【可能不是立即显示,反正只要Reach有值就表示成功了,状态需要等一会才能更新】
[root@ccx_server129 ~]# 
[root@ccx_server129 ~]# date
Mon Feb 28 15:28:05 CST 2022
[root@ccx_server129 ~]# systemctl restart chronyd
[root@ccx_server129 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^? ccx_server128                10   6     3     0  +2317ns[+2317ns] +/-   90us
[root@ccx_server129 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^? ccx_server128                10   6     3     1  +2317ns[+2317ns] +/-   90us
[root@ccx_server129 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^? ccx_server128                10   6     3     1  +2317ns[+2317ns] +/-   90us
[root@ccx_server129 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* ccx_server128                10   6    17     0  -1762ns[-2940ns] +/-   92us
[root@ccx_server129 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* ccx_server128                10   6    17     2  -1762ns[-2940ns] +/-   92us
[root@ccx_server129 ~]# date
Mon Feb 28 23:22:20 CST 2022
[root@ccx_server129 ~]# 
[root@ccx_server129 ~]# 
[root@ccx_server129 ~]# cat /etc/hosts
127.0.0.1   localhost localhost.localdomain localhost4 localhost4.localdomain4
::1         localhost localhost.localdomain localhost6 localhost6.localdomain6
192.168.59.128 ccx_server128
[root@ccx_server129 ~]# 
[root@ccx_server129 ~]# chronyc sources -v
210 Number of sources = 1

  .-- Source mode  '^' = server, '=' = peer, '#' = local clock.
 / .- Source state '*' = current synced, '+' = combined , '-' = not combined,
| /   '?' = unreachable, 'x' = time may be in error, '~' = time too variable.
||                                                 .- xxxx [ yyyy ] +/- zzzz
||      Reachability register (octal) -.           |  xxxx = adjusted offset,
||      Log2(Polling interval) --.      |          |  yyyy = measured offset,
||                                \     |          |  zzzz = estimated error.
||                                 |    |           \
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* ccx_server128                10   9   377   386    -15us[  -19us] +/-  133us
[root@ccx_server129 ~]# 
  • 而且,当时间同步成功以后,ntp服务这些全是yes
    下面NTP synchronized状态如果为no,手动改为yes是不生效的。。成功以后状态会自动变成yes。
    开启ntp命令是:timedatectl set-ntp true/flase
[root@ccx_server129 ~]# timedatectl 
      Local time: Mon 2022-02-28 23:38:32 CST
  Universal time: Mon 2022-02-28 15:38:32 UTC
        RTC time: Mon 2022-02-28 15:38:32
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: yes
      DST active: n/a
# 这个警告是因为我吧时间改为本地了,下面有说改回RTC的方法哈。
Warning: The system is configured to read the RTC time in the local time zone.
         This mode can not be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.
[root@ccx_server129 ~]# 

没加解析的服务器端说明

  • 上面是因为我排错的过程中添加解析了,所以无论是客户端还是服务端看到的ip都是主机名。。。
    我下面呢重新用一台没加解析的虚拟机做服务端,重新试一下效果哦。

  • 我这不对代码做解释了哦,具体的代码说明看上面哈~

  • 服务端配置

[root@ciserver ~]# ip a | grep 151
    inet 192.168.59.151/24 brd 192.168.59.255 scope global noprefixroute ens33
[root@ciserver ~]# cat /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
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
#server 192.168.59.128
server 127.127.1.0

# Record the rate at which the system clock gains/losses time.
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 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# Allow NTP client access from local network.
#allow 192.168.0.0/16
allow 192.168.59.0/24

# Serve time even if not synchronized to a time source.
local stratum 10

# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking
[root@ciserver ~]# 
[root@ciserver ~]# systemctl restart chronyd
[root@ciserver ~]# 
[root@ciserver ~]# chronyc clients
Hostname                      NTP   Drop Int IntL Last     Cmd   Drop Int  Last
===============================================================================
[root@ciserver ~]# 
[root@ciserver ~]# date
Mon Feb 28 17:54:19 CST 2022
[root@ciserver ~]# 

# 下面客户端配置完毕以后再回到服务端,可以看到客户端的ip了哦
[root@ciserver ~]# chronyc clients
Hostname                      NTP   Drop Int IntL Last     Cmd   Drop Int  Last
===============================================================================
192.168.59.129                  5      0   4   -    34       0      0   -     -
[root@ciserver ~]# 
  • 客户端
    我还是用上面的那个客户端来做测试吧
[root@ccx_server129 ~]# cat /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 192.168.59.151 iburst
#server 192.168.59.128 iburst
#server 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst

# Record the rate at which the system clock gains/losses time.
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 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# Enable hardware timestamping on all interfaces that support it.
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# Allow NTP client access from local network.
#allow 192.168.0.0/16
#allow 192.168.59.128
#local stratum 10
# Serve time even if not synchronized to a time source.
#local stratum 10

# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys

# Specify directory for log files.
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking
[root@ccx_server129 ~]# 
[root@ccx_server129 ~]# date #之前服务端的时间
Mon Feb 28 17:48:46 CST 2022
[root@ccx_server129 ~]# 
[root@ccx_server129 ~]# systemctl restart chronyd #重启
[root@ccx_server129 ~]# 
[root@ccx_server129 ~]# chronyc sources #生效了哦,同步成功了
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* 192.168.59.151               10   6    17     3  -1053ns[-1698ns] +/-  150us
[root@ccx_server129 ~]#  #时间也变了
[root@ccx_server129 ~]# date
Mon Feb 28 17:54:56 CST 2022
[root@ccx_server129 ~]# 

chrony常用命令说明

chrony命令

  • 查看时间同步源状态
    客户端上执行,命令:chronyc sourcestats -v
[root@ccx_server129 ~]# chronyc sourcestats -v
210 Number of sources = 1
                             .- Number of sample points in measurement set.
                            /    .- Number of residual runs with same sign.
                           |    /    .- Length of measurement set (time).
                           |   |    /      .- Est. clock freq error (ppm).
                           |   |   |      /           .- Est. error in freq.
                           |   |   |     |           /         .- Est. offset.
                           |   |   |     |          |          |   On the -.
                           |   |   |     |          |          |   samples. \
                           |   |   |     |          |          |             |
Name/IP Address            NP  NR  Span  Frequency  Freq Skew  Offset  Std Dev
==============================================================================
ccx_server128              31  18   70m     +0.000      0.005     +4ns  7836ns
[root@ccx_server129 ~]# 
  • 下面timedatectl说明中有详细介绍哈
    • 设置硬件时间
      硬件时间默认为UTC:timedatectl set-local-rtc 1/0
      • 设置硬件时钟为本地时间:timedatectl set-local-rtc 1
      • 设置硬件时钟为UTC时间:timedatectl set-local-rtc 0
    • 启用NTP时间同步:timedatectl set-ntp yes
    • 开启 NTP:timedatectl set-ntp true/flase
    • 校准时间服务器:chronyc tracking

ntpdate客户端立即更新时间

  • 立即更新时间,我们可以使用ntpdate
    这个还是比较好用的,如果服务端时间更新了,客户端没有及时更新,可以执行这个命令
    如下,我在服务端更新时间了
[root@ccx_server128 ~]# date
2022年 03月 01日 星期二 01:10:04 CST
[root@ccx_server128 ~]# timedatectl set-time "2022-02-28 17:10:34"
Failed to set time: Automatic time synchronization is enabled
[root@ccx_server128 ~]# timedatectl 
      Local time: 二 2022-03-01 01:13:18 CST
  Universal time: 一 2022-02-28 17:13:18 UTC
        RTC time: 一 2022-02-28 17:13:18
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@ccx_server128 ~]# timedatectl set-ntp no
[root@ccx_server128 ~]# timedatectl set-time "2022-02-28 17:13:55"
[root@ccx_server128 ~]# date
2022年 02月 28日 星期一 17:13:57 CST
[root@ccx_server128 ~]# timedatectl 
      Local time: 一 2022-02-28 17:14:02 CST
  Universal time: 一 2022-02-28 09:14:02 UTC
        RTC time: 一 2022-02-28 09:14:02
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@ccx_server128 ~]# timedatectl set-ntp yes
[root@ccx_server128 ~]# 

# 然后我去客户端立即获取。。。多香啊
[root@ccx_server129 ~]# date
Tue Mar  1 01:15:13 CST 2022
[root@ccx_server129 ~]# 
[root@ccx_server129 ~]# ntpdate 192.168.59.128
28 Feb 17:15:37 ntpdate[46458]: step time server 192.168.59.128 offset -28789.784156 sec
[root@ccx_server129 ~]# date
Mon Feb 28 17:15:39 CST 2022
[root@ccx_server129 ~]# 
  • 当然,这也带来一个问题,好像chronyd状态出问题了,不是^*了,然后重启一下又恢复正常,所以,可能这个netdate和重启服务效果一样吧。
[root@ccx_server129 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^? ccx_server128                 0   6   377     -     +0ns[   +0ns] +/-    0ns
[root@ccx_server129 ~]# systemctl restart chronyd #重启了
[root@ccx_server129 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^? ccx_server128                10   6     1     2  -3874ns[-3874ns] +/-  106us
[root@ccx_server129 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* ccx_server128                10   6     7     1  +1884ns[-7831ns] +/-   99us
[root@ccx_server129 ~]# 

时间不同步可能的原因

1.chrony服务端、客户端 版本是否一致和服务是否正常【chrony各版本之间差异很大,确保版本一致】

  • 查看客户端和服务端版本是否一致
# 服务端
[root@ccx_server128 ~]# chronyc -v
chronyc (chrony) version 3.2 (+READLINE +IPV6 +DEBUG)
[root@ccx_server128 ~]# 
#客户端
[root@ccx_server129 ~]# chronyc -v
chronyc (chrony) version 3.2 (+READLINE +IPV6 +DEBUG)
[root@ccx_server129 ~]# 
  • 查看服务是否正常
# 服务端
[root@ccx_server128 ~]# systemctl is-active chronyd
active
[root@ccx_server128 ~]# 
# 客户端
[root@ccx_server129 ~]# systemctl is-active chronyd
active
[root@ccx_server129 ~]# 

2、服务端防火墙原因

  • 防火墙这个东西应该没啥好说的了,无论是iptables还是firewalld都最好关闭,如果不能关闭,则放开123端口【上面有说防火墙设置。】

3、检查服务端和客户端是否能互通

  • 也就是说客户端和服务端是不是在同一网络,能互相ssh就证明网络没问题。

4、看看时区是否正确

  • 执行date如果时区不是CST修改时区,我今天就遇到这个问题了,修改方式如下
    这种情况一般是要和服务端保持一致哈,如果服务端不是est,客户端单独改了也没用。
[root@ccx_server129 ~]# date
Mon Feb 28 07:58:21 EST 2022
[root@ccx_server129 ~]# 
[root@ccx_server129 ~]# timedatectl set-timezone Asia/Shanghai
[root@ccx_server129 ~]# date
Mon Feb 28 22:56:13 CST 2022
[root@ccx_server129 ~]# 

5、间隔2小时左右,chrony服务端机器ntp不同步

  • 意思是NTP synchronized这个出问题了,看下面注释后面说的
    但是此时同步状态是正常的【^*】
  • 未找到相应解决办法。临时解决方法 :在定时任务中 每隔一段时间重启改客户端的chrony。
[root@ccx_server129 ~]# timedatectl 
      Local time: Tue 2022-03-01 00:22:57 CST
  Universal time: Mon 2022-02-28 16:22:57 UTC
        RTC time: Mon 2022-02-28 16:22:57
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes # 就是这个变成no了
 RTC in local TZ: yes
      DST active: n/a

Warning: The system is configured to read the RTC time in the local time zone.
         This mode can not be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.
[root@ccx_server129 ~]# 
[root@ccx_server129 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^* ccx_server128                10   9   377   499    -15us[  -19us] +/-  133us
[root@ccx_server129 ~]# 

说明

  • 一般就上面几种情况导致不同步。
  • 如果你上面都排查了还是没用,仔细检查配置文件,配置文件有问题。

timedatectl说明

timedatectl是Linux下的一条命令,用于控制系统时间和日期。可以用来查询和更改系统时钟于设定,同时可以设定和修改时区信息。这篇文章简单介绍一下如何使用timedatectl进行常见操作。

查看系统时间timedatectl

[root@ccx_server128 ~]# timedatectl 
      Local time: 二 2022-03-01 00:47:50 CST #当地时间
  Universal time: 一 2022-02-28 16:47:50 UTC #伦敦时间
        RTC time: 一 2022-02-28 16:47:50 #硬件时间
       Time zone: Asia/Shanghai (CST, +0800) #时区:亚洲上海(东8区)UTC时间加上8等于当前时间
     NTP enabled: yes #NTP服务
NTP synchronized: no #NTP时间是否启用
 RTC in local TZ: no  #是否启用RTC硬件时间【设定系统时间计算方式,0表示使用utc时间计算方式,yes为0,no为1】【timedatectl set-local-rtc 1这个命令控制】
      DST active: n/a
[root@ccx_server128 ~]# 

常见用法

  • 使用timedatectl可以进行如下常见操作
    • 查看当前时间/日期/时区:timedatectl或者timedatectl status
    • 查看所有可用时区:timedatectl list-timezones
    • 设置时区:timedatectl set-timezone “时区信息”
      • 显示系统的所有时区: timedatectl list-timezones
      • 如:设定系统时区为亚洲上海timedatectl set-timezone “Asia/Shanghai”
    • 设置UTC:timedatectl set-timezone UTC【别乱操作,设置这个以后上面定义的上海+8就没了,变成+0了】
    • 设置硬件时钟为本地时间:timedatectl set-local-rtc 1【/etc/adjtime 文件中第三个为状态】
    • 设置硬件时钟为UTC时间:timedatectl set-local-rtc 0【/etc/adjtime 文件中第三个为状态】
    • 设置时间:timedatectl set-time HH:MM:SS
    • 设置日期:timedatectl set-time YYYY-MM-DD
    • 设置日期时间:timedatectl set-time “YYYY-MM-DD HH:MM:SS”【年月日时分秒】
      • 修改报错执行:timedatectl set-ntp no
      • 修改时间后执行:clock -w【写入硬件时间,一般2个时间同步修改】
    • 启动NTP时间同步(启用NTP服务或者Chrony服务):timedatectl set-ntp true
    • 禁用NTP时间同步(禁用NTP服务或者Chrony服务):timedatectl set-ntp false

使用示例

我觉得上面常见用法中已经很明显了,我这不做全部示例,只做部分。

设置时区

  • 设置时区:timedatectl set-timezone “时区信息”【“要不要都行】
    • 显示系统的所有时区: timedatectl list-timezones
    • 如:设定系统时区为亚洲上海timedatectl set-timezone “Asia/Shanghai”
[root@ccx_server129 ~]# date
Mon Feb 28 07:58:21 EST 2022
[root@ccx_server129 ~]# 
[root@ccx_server129 ~]# timedatectl list-timezones | grep hai
Asia/Shanghai
[root@ccx_server129 ~]# 
[root@ccx_server129 ~]# timedatectl set-timezone Asia/Shanghai
[root@ccx_server129 ~]# date
Mon Feb 28 22:56:13 CST 2022
[root@ccx_server129 ~]# 

设置时间

  • 设置时间:timedatectl set-time HH:MM:SS

  • 设置日期:timedatectl set-time YYYY-MM-DD

  • 设置日期时间:timedatectl set-time “YYYY-MM-DD HH:MM:SS”【年月日时分秒】

    • 修改报错执行:timedatectl set-ntp no
    • 修改时间后执行:clock -w【写入硬件时间,一般2个时间同步修改】
  • 我这以我ntp服务端时间为例

[root@ccx_server128 ~]# date
2022年 03月 01日 星期二 01:10:04 CST
[root@ccx_server128 ~]# timedatectl set-time "2022-02-28 17:10:34"
Failed to set time: Automatic time synchronization is enabled
[root@ccx_server128 ~]# timedatectl 
      Local time: 二 2022-03-01 01:13:18 CST
  Universal time: 一 2022-02-28 17:13:18 UTC
        RTC time: 一 2022-02-28 17:13:18
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@ccx_server128 ~]# timedatectl set-ntp no
[root@ccx_server128 ~]# timedatectl set-time "2022-02-28 17:13:55"
[root@ccx_server128 ~]# date
2022年 02月 28日 星期一 17:13:57 CST
[root@ccx_server128 ~]# timedatectl 
      Local time: 一 2022-02-28 17:14:02 CST
  Universal time: 一 2022-02-28 09:14:02 UTC
        RTC time: 一 2022-02-28 09:14:02
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: no
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@ccx_server128 ~]# timedatectl set-ntp yes
[root@ccx_server128 ~]# 

# 然后我去客户端立即获取。。。多香啊
[root@ccx_server129 ~]# date
Tue Mar  1 01:15:13 CST 2022
[root@ccx_server129 ~]# 
[root@ccx_server129 ~]# ntpdate 192.168.59.128
28 Feb 17:15:37 ntpdate[46458]: step time server 192.168.59.128 offset -28789.784156 sec
[root@ccx_server129 ~]# date
Mon Feb 28 17:15:39 CST 2022
[root@ccx_server129 ~]# 

设置硬件时间

  • 设置硬件时钟为本地(LOCAL)时间:timedatectl set-local-rtc 1【/etc/adjtime 文件中第三个为状态】

  • 设置硬件时钟为UTC时间:timedatectl set-local-rtc 0【/etc/adjtime 文件中第三个为状态】

  • 没有加入ntp服务测试:

[root@ccx_server130 ~]# cat /etc/adjtime  #没有加入ntp显示是这样的
0.0 0 0.0
0
UTC
[root@ccx_server130 ~]# timedatectl 
      Local time: Tue 2022-03-01 01:27:44 CST
  Universal time: Mon 2022-02-28 17:27:44 UTC
        RTC time: Mon 2022-02-28 17:27:44
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@ccx_server130 ~]# 
[root@ccx_server130 ~]# timedatectl set-local-rtc 1 #设置为本地
[root@ccx_server130 ~]# timedatectl 
      Local time: Tue 2022-03-01 01:27:55 CST
  Universal time: Mon 2022-02-28 17:27:55 UTC
        RTC time: Tue 2022-03-01 01:27:56
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: yes
      DST active: n/a

# 改成local以后会有一串这样的警告,也不知道有啥副作用。。..
Warning: The system is configured to read the RTC time in the local time zone.
         This mode can not be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.
[root@ccx_server130 ~]# cat /etc/adjtime 
0.0 0 0.0
0
LOCAL  #显示local
[root@ccx_server130 ~]# 
[root@ccx_server130 ~]# timedatectl set-local-rtc 0
[root@ccx_server130 ~]# cat /etc/adjtime 
0.0 0 0.0
0
UTC
[root@ccx_server130 ~]# timedatectl 
      Local time: Tue 2022-03-01 01:28:27 CST
  Universal time: Mon 2022-02-28 17:28:27 UTC
        RTC time: Mon 2022-02-28 17:28:27
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@ccx_server130 ~]# 
  • 加入ntp
    就文件中显示不一样而已,我也不知道有啥区别。。。。反正其他倒是一样的,改为local会有一串警告
[root@ccx_server129 ~]# cat /etc/adjtime 
0.000000 1646040080 0.000000
1646040080
UTC
[root@ccx_server129 ~]# 

UTC测试

  • 设置UTC:timedatectl set-timezone UTC【别乱操作,设置这个以后上面定义的上海+8就没了,变成+0了】
    哈哈 我一开始还没搞懂这个UTC是啥,我以为这个UTC和上面设置硬件时间是一样的【改变的是UTC/LOCAL】
    下面是我测试,你先看看,能发现啥不~
[root@ccx_server130 ~]# timedatectl  #设置为utc前
      Local time: Tue 2022-03-01 01:28:27 CST
  Universal time: Mon 2022-02-28 17:28:27 UTC
        RTC time: Mon 2022-02-28 17:28:27
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@ccx_server130 ~]# 
[root@ccx_server130 ~]# 
[root@ccx_server130 ~]# cat /etc/adjtime 
0.0 0 0.0
0
UTC
[root@ccx_server130 ~]# timedatectl set-local-rtc 1 
[root@ccx_server130 ~]# cat /etc/adjtime 
0.0 0 0.0
0
LOCAL
[root@ccx_server130 ~]# timedatectl set-timezone UTC 
[root@ccx_server130 ~]# cat /etc/adjtime 
0.0 0 0.0
0
LOCAL
[root@ccx_server130 ~]# 
[root@ccx_server130 ~]# timedatectl 
      Local time: Mon 2022-02-28 17:31:43 UTC
  Universal time: Mon 2022-02-28 17:31:43 UTC
        RTC time: Mon 2022-02-28 17:31:43
       Time zone: UTC (UTC, +0000)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: yes
      DST active: n/a

Warning: The system is configured to read the RTC time in the local time zone.
         This mode can not be fully supported. It will create various problems
         with time zone changes and daylight saving time adjustments. The RTC
         time is never updated, it relies on external facilities to maintain it.
         If at all possible, use RTC in UTC by calling
         'timedatectl set-local-rtc 0'.
[root@ccx_server130 ~]# timedatectl set-local-rtc 0
[root@ccx_server130 ~]# timedatectl 
      Local time: Mon 2022-02-28 17:32:07 UTC
  Universal time: Mon 2022-02-28 17:32:07 UTC
        RTC time: Mon 2022-02-28 17:32:08
       Time zone: UTC (UTC, +0000)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@ccx_server130 ~]# 
  • 解密,后面我发现我设置为LOCAL以后,再设置UTC,但我发现配置文件中LOCAL没变,于是确认这个不是设置硬件时间的。
    然后执行timedatectl set-timezone LOCAL报错了,并且我在timedatectl list-timezones 这里面看到了UTC,发现不对劲,仔细一看timedatectl中的Time zone选项,发现后面变成UTC,并且时间为+0了,哈哈,我之前设置的亚洲上海+8没了。
    所以,这个UTC就是恢复默认的哦,为+0,所以我还是需要改回去的哈
[root@ccx_server130 ~]# timedatectl set-timezone UTC 
[root@ccx_server130 ~]# timedatectl 
      Local time: Mon 2022-02-28 17:32:12 UTC
  Universal time: Mon 2022-02-28 17:32:12 UTC
        RTC time: Mon 2022-02-28 17:32:12
       Time zone: UTC (UTC, +0000)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@ccx_server130 ~]# 
[root@ccx_server130 ~]# timedatectl set-timezone Asia/Shanghai
[root@ccx_server130 ~]# timedatectl 
      Local time: Tue 2022-03-01 01:43:26 CST
  Universal time: Mon 2022-02-28 17:43:26 UTC
        RTC time: Mon 2022-02-28 17:43:27
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: no
 RTC in local TZ: no
      DST active: n/a
[root@ccx_server130 ~]# 

猜你喜欢

转载自blog.csdn.net/cuichongxin/article/details/123178318
今日推荐