银河麒麟Linux同步时间:NTPd、Chrony、systemd-timesyncd 配置与使用

1.时间同步协议服务对比

在网络中,常用的时间同步协议服务主要有以下几种:

特性 NTPd systemd-timesyncd Chrony
协议 NTP SNTP NTP
说明 NTPd(Network Time Protocol daemon)是最经典的时间同步服务,使用NTP协议来同步系统时间。
它支持复杂的配置,能够处理多个时间源,并且具有较高的精度。
NTPd通常用于需要高精度时间同步的场景,如数据中心、科研机构等。
systemd-timesyncd是 systemd 项目的一部分,是一个轻量级的时间同步服务。
它使用SNTP(Simple Network Time Protocol)协议,配置简单,适合大多数桌面和服务器环境。
systemd-timesyncd 通常用于不需要高精度时间同步的场景,适合大多数普通用户和小型服务器。
Chrony是一个现代化的时间同步服务,支持NTP协议,并且具有更好的网络适应性。
它在不稳定的网络环境下表现更好,能够快速同步时间,并且支持离线模式。
Chrony 适合移动设备、虚拟机以及网络不稳定的环境。
精度 ⭐⭐⭐⭐ ⭐⭐ ⭐⭐⭐⭐
配置复杂度 ⭐⭐⭐⭐ ⭐⭐
网络适应性 ⭐⭐⭐ ⭐⭐ ⭐⭐⭐⭐⭐
适用场景 数据中心、科研机构 桌面、小型服务器 移动设备、虚拟机、不稳定网络
优点 高精度、支持复杂配置 轻量级、易于配置 快速同步、适应不稳定网络
缺点 配置复杂、对网络稳定性要求高 精度较低 功能相对较少

图标说明:

  • ⭐:表示程度,星星越多表示越强。
  • 协议:NTP(Network Time Protocol)、SNTP(Simple Network Time Protocol)。
  • 网络适应性:指在网络不稳定环境下的表现。

根据你的具体需求和环境,可以选择合适的时间同步服务;选择建议:

  • 如果你需要高精度的时间同步,且网络环境稳定,可以选择 NTPd
  • 如果你需要一个轻量级、易于配置的时间同步服务,且不需要极高的精度,可以选择 systemd-timesyncd
  • 如果你的网络环境不稳定,或者你需要在移动设备或虚拟机上同步时间,Chrony是一个更好的选择。

2.使用

操作环境(银河麒麟):

NAME="Kylin Linux Advanced Server"
VERSION="V10 (Halberd)"
ID="kylin"
VERSION_ID="V10"
PRETTY_NAME="Kylin Linux Advanced Server V10 (Halberd)"
ANSI_COLOR="0;31"

2.1 使用NTPd

2.1.1 安装 NTPd

先停止并禁用chronydsystemd-timesyncd这些服务,以避免冲突:

systemctl stop chronyd
systemctl disable chronyd
systemctl stop systemd-timesyncd
systemctl disable systemd-timesyncd安装命令:

安装:

yum install ntp

2.1.2 配置 NTPd

NTPd 的配置文件位于 /etc/ntp.conf,编辑配置文件:

vim /etc/ntp.conf

示例配置:

# 指定NTP服务器(可以添加多个)
server ntp.aliyun.com iburst
server ntp1.tencent.com iburst
server ntp.ubuntu.com iburst

# 允许本地客户端同步时间
restrict 127.0.0.1
restrict ::1

# 允许特定网段的客户端同步时间(可选)
restrict 192.168.1.0 mask 255.255.255.0 nomodify notrap

# 启用日志记录
logfile /var/log/ntp.log
  • server:指定 NTP 服务器地址,iburst 表示快速同步。
  • restrict:控制哪些客户端可以访问 NTP 服务。
    • nomodify:客户端不能修改服务器配置。
    • notrap:客户端不能使用陷阱服务。
  • logfile:指定日志文件路径。

2.1.3 启动并启用 NTPd 服务

启动 NTPd 服务:

systemctl start ntpd

设置开机自启动:

systemctl enable ntpd

2.1.4 检查 NTPd 状态

查看服务状态:

systemctl status ntpd

查看时间同步状态:

ntpq -p

输出示例:

     remote           refid      st t when poll reach   delay   offset  jitter
==============================================================================
*ntp.aliyun.com  10.137.38.86     2 u   10   64  377    10.234  -0.123   0.456
 ntp1.tencent.com 10.137.38.86     2 u   12   64  377    12.345  +0.456   0.789
  • remote:NTP 服务器的地址。
  • refid:参考时钟的标识符。
  • st:层级(Stratum),数值越小,精度越高。
  • delay:延迟时间(单位:毫秒)。
  • offset:时间偏移量(单位:毫秒)。
  • jitter:时间抖动(单位:毫秒)。

2.1.5 手动同步时间

如果需要立即同步时间,可以使用以下命令:

ntpdate ntp.aliyun.com

2.1.6 防火墙配置(可选)

如果启用了防火墙,需要允许 NTP 服务的端口(UDP 123):

sudo firewall-cmd --add-service=ntp --permanent
sudo firewall-cmd --reload

2.1.7 验证时间同步

使用以下命令查看当前系统时间:

date

或者查看详细的同步状态:

timedatectl

2.2 使用systemd-timesyncd

2.2.1 检查systemd-timesyncd状态

注意:systemd-timesyncd 通常是作为 systemd的一部分预装的

先停止并禁用 NTPdchronyd这些服务,以避免冲突:

systemctl stop ntpd
systemctl disable ntpd
systemctl stop chronyd
systemctl disable chronyd

查看状态:

systemctl status systemd-timesyncd

启动:

systemctl start systemd-timesyncd

设置开机自启动:

systemctl enable systemd-timesyncd

2.2.2 配置 systemd-timesyncd

systemd-timesyncd的配置文件位于 /etc/systemd/timesyncd.conf,编辑配置文件:

vim /etc/systemd/timesyncd.conf

示例配置:

[Time]
# 指定NTP服务器(可以添加多个,用空格分隔)
NTP=ntp.aliyun.com ntp1.tencent.com

# 是否启用NTP服务
NTP=yes

# 是否启用FallbackNTP(备用NTP服务器)
FallbackNTP=ntp.ubuntu.com

# 是否启用RootDistanceMaxSec(最大根距离)
RootDistanceMaxSec=5
  • NTP:指定时间服务器(例如阿里云、腾讯云等公共NTP服务器)。
  • FallbackNTP:当主NTP服务器不可用时,使用的备用服务器。
  • RootDistanceMaxSec:设置最大时间同步误差(单位:秒)。

2.2.3 重启服务使配置生效

修改配置文件后,需要重启 systemd-timesyncd服务:

systemctl restart systemd-timesyncd

2.2.4 检查时间同步状态

查看时间同步状态:

timedatectl timesync-status

输出示例:

       Server: 203.107.6.88 (time1.aliyun.com)
Poll interval: 4min 16s (min: 32s; max 34min 8s)
         Leap: normal
      Version: 4
      Stratum: 2
    Reference: A8937B5
    Precision: 1us (-25)
Root distance: 8.338ms (max: 5s)
       Offset: -3.593ms
        Delay: 50.081ms
       Jitter: 21.013ms
 Packet count: 4
    Frequency: +69.004ppm

2.2.5 其他常用命令

查看当前系统时间状态:

timedatectl status

手动同步时间:

sudo systemctl restart systemd-timesyncd

禁用 systemd-timesyncd

sudo systemctl stop systemd-timesyncd
sudo systemctl disable systemd-timesyncd

2.3 使用Chrony

2.3.1 安装 Chrony

先停止并禁用 NTPdsystemd-timesyncd这些服务,以避免冲突:

sudo systemctl stop ntpd
sudo systemctl disable ntpd
sudo systemctl stop systemd-timesyncd
sudo systemctl disable systemd-timesyncd

安装命令:

yum install chrony

2.3.2 配置 Chrony

Chrony 的配置文件位于 /etc/chrony.conf,编辑配置文件:

sudo vim /etc/chrony.conf

示例配置:

# 指定NTP服务器(可以添加多个)
server ntp.aliyun.com iburst
server ntp1.tencent.com iburst
server ntp.ubuntu.com iburst

# 允许本地客户端同步时间
allow 127.0.0.1
allow ::1

# 允许特定网段的客户端同步时间(可选)
allow 192.168.1.0/24

# 启用日志记录
logdir /var/log/chrony
  • server:指定 NTP 服务器地址,iburst 表示快速同步。
  • allow:允许哪些客户端访问 Chrony 服务。
  • logdir:指定日志文件目录。

2.3.3 启动并启用 Chrony 服务

启动 Chrony 服务:

systemctl start chronyd

设置开机自启动:

systemctl enable chronyd

2.3.4 检查 Chrony 状态

查看服务状态:

systemctl status chronyd

查看时间同步状态:

chronyc tracking

输出示例:

Reference ID    : C0A80101 (192.168.1.1)
Stratum         : 3
Ref time (UTC)  : Thu Oct 12 12:34:56 2023
System time     : 0.000123456 seconds fast of NTP time
Last offset     : +0.000123456 seconds
RMS offset      : 0.000123456 seconds
Frequency       : 1.234 ppm fast
Residual freq   : +0.123 ppm
Skew            : 0.123 ppm
Root delay      : 0.0123456 seconds
Root dispersion : 0.0123456 seconds
Update interval : 64.0 seconds
Leap status     : Normal
  • Reference ID:参考时钟的标识符。
  • Stratum:层级,数值越小,精度越高。
  • System time:系统时间与 NTP 时间的偏差。

2.3.5 手动同步时间

如果需要立即同步时间,可以使用以下命令:

chronyc makestep

2.3.6 防火墙配置(可选)

如果启用了防火墙,需要允许 NTP 服务的端口(UDP 123):

sudo firewall-cmd --add-service=ntp --permanent
sudo firewall-cmd --reload

2.3.7 验证时间同步

使用以下命令查看当前系统时间:

date

或者查看详细的同步状态:

timedatectl

2.4 手动设置时间

手动设置时间命令:

# 设置时间为:2024年03月08日00:44
date 010403442025

手动使用工具同步时间:

# 安装ntpdate
yum install ntpdate
# 设置时区
timedatectl set-timezone Asia/Shanghai
# 手动同步时间(这里用的阿里的)
ntpdate ntp.aliyun.com
posted @ 2025-01-04 17:03  youngyajun  阅读(4444)  评论(1)    收藏  举报