Linux Chrony 时间同步
Time plays an important role in Linux servers specially when they are used in banking, stock markets and other financial sectors. If we want all our Linux servers should have the correct time, then we must configure some NTP client which will fetch correct time always from remote NTP Servers and if needed makes the required adjustments for syncing the time.
In this article we will demonstrate how we can sync time with NTP servers in Linux Server using Chrony (NTP Client).
Chrony synchronize the system clock’s time faster and with better accuracy than the ntp. As of RHEL8/CentOS8, ntp is not available anymore, being replaced by the chrony.
安装 Chrony
Debian/Ubuntu 系统
sudo apt install -y chrony
sudo apt install chrony
RedHat/CentOS 系统
yum install -y chrony
Fedora 系统
dnf install -y chrony
安装完成后,Linux 系统会增加两个命令:
- chronyc : It is command line interface of chrony
- chronyd : It is daemon for chrony which start and enable chrony service across the reboot.
配置 Chrony
Chrony 的配置文件在不同发行版上的位置略有不同,通常是 /etc/chrony.conf 或者 /etc/chrony/chrony.conf,
# Welcome to the chrony configuration file. See chrony.conf(5) for more
# information about usuable directives.
# This will use (up to):
# - 4 sources from ntp.ubuntu.com which some are ipv6 enabled
# - 2 sources from 2.ubuntu.pool.ntp.org which is ipv6 enabled as well
# - 1 source from [01].ubuntu.pool.ntp.org each (ipv4 only atm)
# This means by default, up to 6 dual-stack and up to 2 additional IPv4-only
# sources will be used.
# At the same time it retains some protection against one of the entries being
# down (compare to just using one of the lines). See (LP: #1754358) for the
# discussion.
#
# About using servers from the NTP Pool Project in general see (LP: #104525).
# Approved by Ubuntu Technical Board on 2011-02-08.
# See http://www.pool.ntp.org/join.html for more information.
pool ntp.ubuntu.com iburst maxsources 4
pool 0.ubuntu.pool.ntp.org iburst maxsources 1
pool 1.ubuntu.pool.ntp.org iburst maxsources 1
pool 2.ubuntu.pool.ntp.org iburst maxsources 2
# This directive specify the location of the file containing ID/key pairs for
# NTP authentication.
keyfile /etc/chrony/chrony.keys
# This directive specify the file into which chronyd will store the rate
# information.
driftfile /var/lib/chrony/chrony.drift
# Uncomment the following line to turn logging on.
#log tracking measurements statistics
# Log files location.
logdir /var/log/chrony
# Stop bad estimates upsetting machine clock.
maxupdateskew 100.0
# This directive enables kernel synchronisation (every 11 minutes) of the
# real-time clock. Note that it can’t be used along with the 'rtcfile' directive.
rtcsync
# Step the system clock instead of slewing it if the adjustment is larger than
# one second, but only in the first three clock updates.
makestep 1 3
其中:
pool 0.europe.pool.ntp.org iburst
is the remote NTP server from where chrony will fetch the time.driftfile /var/lib/chrony/drift
is the drift file which contains drift datamakestep 1.0 3
is the parameter which will step system clock (speedup or slow down) if adjustment is larger than 1 second but only for first 3 clock updateskeyfile /etc/chrony.keys
as the name suggest this file contains keys for NTP authentication.logdir /var/log/chrony
, it is the log file which logs of Chrony.
Testing Chrony
Just like ntpdate command in NTP distribution, we can use chronyd to sync time of our Linux server with remote NTP server manually,
语法
chronyd -q 'server {ntp_server_name} iburst'
例如:
$ date
$ chronyd -q 'server 0.europe.pool.ntp.org iburst'
$ date
As we can see in above output, chrony has corrected the system time, before running the chronyd command system time was almost 2 hours behind from accurate time.
启动 Chronyd 服务
Run the following commands to start and enable chronyd daemon so that it will be available across the reboots.
sudo systemctl start chronyd
sudo systemctl enable chronyd
Run the beneath command to verify the chronys service status
systemctl status chronyd
验证时间同步
Verify and Track Chrony Synchronization
To verify whether your system’s time is synchronized using chrony, issue the following command,
$ chronyc tracking
Reference ID : 78197314 (120.25.115.20)
Stratum : 3
Ref time (UTC) : Wed Dec 14 07:14:30 2022
System time : 0.000070560 seconds fast of NTP time
Last offset : +0.000299871 seconds
RMS offset : 0.007765118 seconds
Frequency : 18.610 ppm fast
Residual freq : +0.004 ppm
Skew : 0.193 ppm
Root delay : 0.009446586 seconds
Root dispersion : 0.006204020 seconds
Update interval : 1041.1 seconds
Leap status : Normal