欢迎光临
我们一直在努力

CE(Chrony服务器)

一、时间相关基础概念

1.1 时区知识

时区是地球上使用同一时间的区域,由于地球自转导致不同经度地区的地方时不同,全球共划分为 24 个时区,每个时区横跨经度 15°。

  • UTC(Universal Time Coordinated):协调世界时,是国际通用的时间标准
  • GMT(Greenwich Mean Time):格林威治标准时间,与 UTC 基本一致,本文不做区分
  • CST(China Standard Time):中国标准时间,等于 UTC+8
  • DST(Daylight Saving Time):夏令时,中国不使用该制度

1.2 Linux 系统中的两种时钟

Linux 系统存在两种时钟,各自承担不同的角色:

  • 硬件时钟(RTC – Real Time Clock)

    • 即 BIOS 时钟,由主板电池供电
    • 系统断电后仍能保持计时
    • 可通过主板设置程序修改

    # 查看硬件时间
    [root@server ~]# hwclock
    2023-05-30 09:48:32.535594+08:00

  • 系统时钟(System Clock)

    • 由 Linux 内核提供的软件时钟
    • 系统断电后会丢失
    • 是系统运行时实际使用的时钟

    # 查看系统时间
    [root@server ~]# date
    2023年 05月 30日 星期二 09:50:50 CST

  • 时钟同步操作

    # 修改系统时间为错误值
    [root@server ~]# date -s 10:00
    2023年 05月 30日 星期二 10:00:00 CST

    # 从硬件时钟同步到系统时钟
    [root@server ~]# hwclock -s

    # 验证时间已同步
    [root@server ~]# date
    2023年 05月 30日 星期二 09:51:50 CST

  • 二、时间设置工具详解

    2.1 timedatectl 命令

    timedatectl 是 systemd 系统中用于管理系统时间和日期的主要工具,功能强大且使用便捷:

    # 显示当前时间状态详情
    [root@server ~]# timedatectl
    Local time: 三 2023-11-15 13:00:26 CST # 本地时间
    Universal time: 三 2023-11-15 05:00:26 UTC # 世界时间
    RTC time: 三 2023-11-15 06:16:16 # 硬件时间
    Time zone: Asia/Shanghai (CST, +0800) # 时区信息
    System clock synchronized: yes # 时间是否已同步
    NTP service: active # 时间同步服务状态
    RTC in local TZ: no # 硬件时钟是否使用本地时区

    时间设置常用操作:

    # 关闭时间同步(修改时间前需要执行)
    [root@server ~]# timedatectl set-ntp no

    # 设置日期
    [root@server ~]# timedatectl set-time "2023-12-12"

    # 设置时间
    [root@server ~]# timedatectl set-time "12:12:12"

    # 查看可用时区
    [root@server ~]# timedatectl list-timezones | grep Asia

    # 设置时区为上海
    [root@server ~]# timedatectl set-timezone Asia/Shanghai

    2.2 date 命令

    date 命令是另一个常用的时间管理工具,适合快速查看和设置系统时间:

    # 显示当前时间
    [root@server ~]# date

    # 格式化显示时间
    [root@server ~]# date +"%Y-%m-%d %H:%M:%S"

    # 设置日期
    [root@server ~]# date -s 2023-11-15
    2023年 11月 15日 星期三 00:00:00 CST

    # 设置时间
    [root@server ~]# date -s 14:33:33
    2023年 11月 15日 星期三 14:33:33 CST

    三、NTP 协议基础

    NTP(Network Time Protocol,网络时间协议)是实现网络中时间同步的标准协议,具有以下特点:

    • 基于 UDP 协议传输,使用端口 123
    • 可在分布式网络中实现时钟同步
    • 局域网内精度可达 0.1ms,互联网上一般为 1-50ms
    • 支持层级结构,可形成时间同步网络
    • 既可以接受其他时钟源同步,也可以作为时钟源同步其他设备

    四、Chrony 时间同步工具

    4.1 Chrony 简介

    Chrony 是一个开源的时间同步工具,相比传统的 NTP 服务,具有更快的同步速度和更好的稳定性,尤其适合不稳定网络环境。

    Chrony 由两个主要组件构成:

  • chronyd:后台运行的守护进程,负责与时间服务器同步并调整系统时钟
  • chronyc:命令行界面工具,用于监控和配置 chronyd
  • 注意:在大多数现代 Linux 系统中,Chrony 已取代传统 NTP 成为默认的时间同步解决方案,两者不能同时运行,会导致时间冲突。

    # 查看chronyd服务状态
    [root@server ~]# systemctl status chronyd

    4.2 Chrony 安装与基础配置

    4.2.1 安装 Chrony

    # 安装chrony软件包
    [root@server ~]# yum install chrony -y

    # 设置开机自启并立即启动服务
    [root@server ~]# systemctl enable chronyd –now

    4.2.2 配置文件详解

    Chrony 的主配置文件为/etc/chrony.conf,包含了所有核心配置:

    # 使用公共NTP服务器池,iburst表示首次快速同步
    pool pool.ntp.org iburst

    # 记录时间调整比率的文件,用于系统重启后补偿
    driftfile /var/lib/chrony/drift

    # 允许系统时钟在前三次更新中步进调整(当偏移大于1秒时)
    makestep 1.0 3

    # 启用实时时钟(RTC)的内核同步
    rtcsync

    # 启用硬件时间戳(默认注释)
    #hwtimestamp *

    # 允许指定网络的客户端进行时间同步(默认注释)
    #allow 192.168.48.0/24

    # 即使未同步到时间源也提供时间服务(默认注释)
    # local stratum 10

    # NTP身份验证密钥文件(默认注释)
    # keyfile /etc/chrony.keys

    # 日志文件目录
    logdir /var/log/chrony

    # 日志记录选项(默认注释)
    # log measurements statistics tracking

    4.3 常用公共时间服务器

    以下是一些常用的公共 NTP 服务器,可根据网络情况选择:

    • 国家授时中心:210.72.145.44
    • 阿里云:ntp.aliyun.com
    • 北京邮电大学:s1a.time.edu.cn
    • 清华大学:s1b.time.edu.cn、s1e.time.edu.cn、s2a.time.edu.cn等
    • 北京大学:s1c.time.edu.cn、s2m.time.edu.cn
    • 东南大学:s1d.time.edu.cn
    • 上海交通大学:ntp.sjtu.edu.cn (202.120.2.101)

    五、Chrony 实战配置

    5.1 客户端配置:同步到公共时间服务器

    步骤 1:修改系统时间为错误值(测试用)

    [root@server ~]# date -s 10:30
    2023年 05月 30日 星期二 10:30:00 CST

    步骤 2:配置 Chrony 使用阿里云时间服务器

    [root@server ~]# vim /etc/chrony.conf

    # 修改或添加以下内容
    server ntp.aliyun.com iburst
    stratumweight 0
    driftfile /var/lib/chrony/drift
    rtcsync
    makestep 10 3
    bindcmdaddress 127.0.0.1
    bindcmdaddress ::1
    keyfile /etc/chrony.keys
    commandkey 1
    generatecommandkey
    logchange 0.5
    logdir /var/log/chrony

    步骤 3:重启 Chrony 服务

    [root@server ~]# systemctl restart chronyd

    步骤 4:查看同步状态

    [root@server ~]# chronyc sources -v

    .– Source mode '^' = server, '=' = peer, '#' = local clock.
    / .- Source state '*' = current best, '+' = combined, '-' = not combined,
    | / 'x' = may be in error, '~' = too variable, '?' = unusable.
    || .- 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
    ==========================================================================
    ^* 203.107.6.88 2 6 17 56 +493us[ -335us] +/- 34ms

    步骤 5:验证时间同步结果

    [root@server ~]# timedatectl status
    Local time: 二 2023-05-30 10:24:39 CST
    Universal time: 二 2023-05-30 02:24:39 UTC
    RTC time: 二 2023-05-30 02:24:40
    Time zone: Asia/Shanghai (CST, +0800)
    System clock synchronized: yes # 已同步
    NTP service: active
    RTC in local TZ: no

    [root@server ~]# date
    2023年 05月 30日 星期二 10:26:14 CST

    5.2 搭建本地时间同步服务器

    在企业环境中,通常会搭建本地时间服务器,让内部设备同步到本地服务器,而不是直接同步到外部公共服务器,这样可以提高同步效率并减少外部依赖。

    架构规划
    角色IP 地址同步对象
    服务端 server 192.168.168.130 ntp.aliyun.com
    客户端 node1 192.168.168.131 192.168.168.130
    服务端配置

    安装 chrony(如果尚未安装)

    [root@server ~]# yum install chrony -y

    配置服务端同步到阿里云时间服务器

    [root@server ~]# vim /etc/chrony.conf
    # 修改为阿里云时间服务器
    server ntp.aliyun.com iburst

    配置允许客户端同步的网络

    [root@server ~]# vim /etc/chrony.conf
    # 添加允许同步的客户端网段
    allow 192.168.168.0/24

    重启服务使配置生效

    [root@server ~]# systemctl restart chronyd

    验证服务端同步状态

    [root@server ~]# chronyc sources -v
    [root@server ~]# timedatectl status

    客户端配置

    安装 chrony

    [root@node1 ~]# yum install chrony -y

    配置客户端同步到本地服务端

    [root@node1 ~]# vim /etc/chrony.conf
    # 修改为本地时间服务器地址
    server 192.168.168.130 iburst

    重启服务

    [root@node1 ~]# systemctl restart chronyd

    验证客户端同步状态

    [root@node1 ~]# chronyc sources -v

    .– Source mode '^' = server, '=' = peer, '#' = local clock.
    / .- Source state '*' = current best, '+' = combined, '-' = not combined,
    | / 'x' = may be in error, '~' = too variable, '?' = unusable.
    || .- 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
    =========================================================================
    ^* 192.168.168.130 3 6 17 39 +20us[ +252us] +/- 38ms

    检查时间同步状态

    [root@node1 ~]# timedatectl status

    同步失败的排查方向

    如果客户端同步失败,可以从以下几个方面排查:

  • 网络连通性:确保客户端能 ping 通服务端
  • 服务端配置:检查服务端的allow参数是否正确配置了客户端网段
  • 服务状态:确保两端的 chronyd 服务都已正常启动
  • 防火墙:检查是否有防火墙规则阻止了 NTP 流量(UDP 123 端口)
  • 配置生效:修改配置后是否重启了 chronyd 服务
  • 六、chronyc 命令详解

    chronyc 是 Chrony 的命令行管理工具,提供了丰富的功能用于监控和管理时间同步服务。

    6.1 查看时间源状态

    [root@server ~]# chronyc sources -v

    输出结果分析:

    • M:源模式,^表示服务器,=表示对等方,#表示本地时钟
    • S:源状态
      • *:当前正在同步的最佳源
      • +:可接受的源,参与组合计算
      • -:被排除的可接受源
      • ?:已失去连接的源
      • x:被认为是错误的源
      • ~:时间 variability 过大的源
    • Name/IP address:时间源的名称或 IP 地址
    • Stratum:层级,1 表示本地参考时钟,数字越大层级越低
    • Poll:轮询频率(秒),值为 2 的对数(如 6 表示 64 秒)
    • Reach:可达性(八进制),377 表示最近 8 次都成功
    • LastRx:上次接收时间
    • Last sample:上次测量的时间偏移量

    6.2 查看时间源统计信息

    [root@server ~]# chronyc sourcestats -v

    6.3 查看时间服务器活动状态

    [root@server ~]# chronyc activity -v

    6.4 立即同步系统时钟

    [root@server ~]# chronyc -a makestep

    赞(0)
    未经允许不得转载:171主机测评 » CE(Chrony服务器)
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址