(Linux系统之部署Chrony时间服务器)
一、Chrony介绍
1. Chrony简介
Chrony是一个开源自由的网络时间协议 NTP 的客户端和服务器软软件。它能让计算机保持系统时钟与时钟服务器(NTP)同步,因此让你的计算机保持精确的时间,Chrony也可以作为服务端软件为其他计算机提供时间同步服务。
2. Chrony的核心组件
- chronyd:是一个后台运行的守护进程,主要用于调整内核中运行的系统时间和时间服务器同步,它确定计算机增减时间的比率,并对此进行调整补偿。
- chronyc:提供一个用户界面,用于监控chronyd性能并进行多样化配置。它可以监控本机以及其他的计算机上运行的chronyd进程。
二、检查本地系统环境
1. 检查操作系统版本
检查操作系统版本,本次实践的操作系统版本为centos7.6。
[root@docker ~]# cat /etc/os-release
NAME="CentOS Linux"
VERSION="7 (Core)"
ID="centos"
ID_LIKE="rhel fedora"
VERSION_ID="7"
PRETTY_NAME="CentOS Linux 7 (Core)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:centos:centos:7"
HOME_URL="https://www.centos.org/"
BUG_REPORT_URL="https://bugs.centos.org/"
CENTOS_MANTISBT_PROJECT="CentOS-7"
CENTOS_MANTISBT_PROJECT_VERSION="7"
REDHAT_SUPPORT_PRODUCT="centos"
REDHAT_SUPPORT_PRODUCT_VERSION="7
2. 检查系统内核版本
检查系统内核版本
[root@docker ~]# uname -r
3.10.0-957.el7.x86_64
三、yum仓库的配置工作
1.配置yum仓库
本次使用的阿里的镜像源,直接使用以下命令直接配置即可。
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
sed -i -e '/mirrors.cloud.aliyuncs.com/d' -e '/mirrors.aliyuncs.com/d' /etc/yum.repos.d/CentOS-Base.repo
清空及生成缓存
yum clean all
yum makecache
2.检查yum仓库状态
检查本机上yum仓库所有镜像源的状态。
[root@docker ~]# yum repolist all
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
repo id repo name status
!base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com enabled: 10,072
centosplus/7/x86_64 CentOS-7 - Plus - mirrors.aliyun.com disabled
contrib/7/x86_64 CentOS-7 - Contrib - mirrors.aliyun.com disabled
docker-ce-nightly/7/x86_64 Docker CE Nightly - x86_64 disabled
docker-ce-nightly-debuginfo/7/x86_64 Docker CE Nightly - Debuginfo x86_64 disabled
docker-ce-nightly-source/7 Docker CE Nightly - Sources disabled
!docker-ce-stable/7/x86_64 Docker CE Stable - x86_64 enabled: 169
docker-ce-stable-debuginfo/7/x86_64 Docker CE Stable - Debuginfo x86_64 disabled
docker-ce-stable-source/7 Docker CE Stable - Sources disabled
docker-ce-test/7/x86_64 Docker CE Test - x86_64 disabled
docker-ce-test-debuginfo/7/x86_64 Docker CE Test - Debuginfo x86_64 disabled
docker-ce-test-source/7 Docker CE Test - Sources disabled
!extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com enabled: 515
!updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com enabled: 4,30
四、安装chrony软件
1.安装chrony
直接使用yum安装chrony软件。
[root@docker ~]# yum -y install chrony
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package chrony.x86_64 0:3.4-1.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
==================================================================================================================================================
Package Arch Version Repository Size
==================================================================================================================================================
Installing:
chrony x86_64 3.4-1.el7 base 251 k
Transaction Summary
==================================================================================================================================================
Install 1 Package
Total download size: 251 k
Installed size: 491 k
Downloading packages:
chrony-3.4-1.el7.x86_64.rpm | 251 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : chrony-3.4-1.el7.x86_64 1/1
Verifying : chrony-3.4-1.el7.x86_64 1/1
Installed:
chrony.x86_64 0:3.4-1.el7
Complete!
2.设置chrony服务开机自启
启动chronyd服务,并设置开机自启。
systemctl enable --now chronyd
3.查看chronyd服务状态
检查当前chronyd服务的状态,确保chronyd服务的正常运行。
[root@docker ~]# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Tue 2022-10-25 19:26:15 CST; 7min ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 42246 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
Process: 42240 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 42242 (chronyd)
Tasks: 1
Memory: 364.0K
CGroup: /system.slice/chronyd.service
└─42242 /usr/sbin/chronyd
Oct 25 19:26:15 docker systemd[1]: Starting NTP client/server...
Oct 25 19:26:15 docker chronyd[42242]: chronyd version 3.4 starting (+CMDMON +NTP +REFCLOCK +RTC +PRIVDROP +SCFILTER +SIGND +ASYNCDNS +... +DEBUG)
Oct 25 19:26:15 docker systemd[1]: Started NTP client/server.
Oct 25 19:26:21 docker chronyd[42242]: Selected source 108.59.2.24
Oct 25 19:26:21 docker chronyd[42242]: System clock wrong by 23.871267 seconds, adjustment started
Oct 25 19:26:45 docker chronyd[42242]: System clock was stepped by 23.871267 seconds
Oct 25 19:26:48 docker chronyd[42242]: Source 5.79.108.34 replaced with 162.159.200.123
Oct 25 19:27:51 docker chronyd[42242]: Selected source 116.203.151.74
Hint: Some lines were ellipsized, use -l to show in full.
五、修改/etc/chrony.conf文件
1.编辑/etc/chrony.conf
编辑chrony.conf文件配置文件,将外部时间同步源设置为阿里的ntp.aliyun.com。
[root@docker ~]# grep -iEv "^#|^ " /etc/chrony.conf
server ntp.aliyun.com iburst
driftfile /var/lib/chrony/drift
makestep 1.0 3
rtcsync
logdir /var/log/chrony
2.重启chrony服务
修改完chrony的配置文件后,需重启chronyd服务才可生效。
systemctl restart chronyd
六、查看服务器时间同步情况
1.查看时间同步源状态
检查时间是否正常同步,当出现^*时,表示,已正常同步了。
[root@docker ~]# 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
===============================================================================
^* 203.107.6.88 2 6 77 31 +762us[+1132us] +/- 26ms
2.查看系统时间参数信息
查看系统时间参数信息
[root@docker ~]# chronyc tracking
Reference ID : CB6B0658 (203.107.6.88)
Stratum : 3
Ref time (UTC) : Tue Oct 25 11:41:21 2022
System time : 0.000142983 seconds slow of NTP time
Last offset : +0.000602810 seconds
RMS offset : 0.002404696 seconds
Frequency : 6.137 ppm fast
Residual freq : -12.949 ppm
Skew : 6.782 ppm
Root delay : 0.047122456 seconds
Root dispersion : 0.003335197 seconds
Update interval : 64.8 seconds
Leap status : Normal
七、测试时间同步
1.修改服务器错误时间
为了测试时间是否正常同步,我们将服务器上的时间改为错误时间。
[root@docker ~]# date
Tue Oct 25 19:43:01 CST 2022
[root@docker ~]# date 110322092022.11
Thu Nov 3 22:09:11 CST 2022
2.手动时间同步
进行手动时间同步。
[root@docker ~]# chronyc -a makestep
200 OK
3.查看系统时间
查看当前系统时间,已经同步成功,修改为正确时间。
[root@docker ~]# chronyc -a makestep
200 OK
[root@docker ~]# date
Tue Oct 25 19:49:00 CST 2022
标签:00,x86,chronyd,chrony,Chrony,64,Linux,服务器,docker
From: https://blog.51cto.com/u_14664141/6238623