
NTP(Network Time Protocol,网络时间协议)是一种用于同步计算机时间的协议。它是一种客户端-服务器协议,用于同步计算机的时钟。通过连接到网络上的时间服务器,计算机可以获取精确的时间信息,确保其时钟与网络上的其他设备保持同步。
NTP(Network Time Protocol,网络时间协议)服务器主要用于同步计算机和网络设备的时间,以确保系统的时间准确性和一致性。NTP服务器的使用场景包括以下几个方面:
1.本次主要部署NTP服务器;
2.业务服务器指定NTP服务器,保持时间同步。
hostname | IP地址 | 操作系统版本 | 角色 |
|---|---|---|---|
jeven | 192.168.3.166 | centos 7.6 | NTP服务器 |
docker | 192.168.3.127 | centos 7.6 | 业务服务器 |
[root@jeven ~]# yum repolist enabled
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
repo id repo name status
!base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 10,072
!extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 518
!gitlab-ce/7 Gitlab CE Repository 934
!pgdg-common/7/x86_64 PostgreSQL common RPMs for RHEL / CentOS 7 - x86_64 497
!pgdg11/7/x86_64 PostgreSQL 11 for RHEL / CentOS 7 - x86_64 1,595
!pgdg12/7/x86_64 PostgreSQL 12 for RHEL / CentOS 7 - x86_64 1,261
!pgdg13/7/x86_64 PostgreSQL 13 for RHEL / CentOS 7 - x86_64 1,021
!pgdg14/7/x86_64 PostgreSQL 14 for RHEL / CentOS 7 - x86_64 762
!pgdg15/7/x86_64 PostgreSQL 15 for RHEL / CentOS 7 - x86_64 473
!updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 5,367
repolist: 22,500curl -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直接安装chrony
yum -y install chrony
启动chronyd服务
systemctl enable --now chronyd检查当前chrony服务状态
[root@jeven ~]# systemctl status chronyd
● chronyd.service - NTP client/server
Loaded: loaded (/usr/lib/systemd/system/chronyd.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2023-11-01 16:36:53 CST; 28s ago
Docs: man:chronyd(8)
man:chrony.conf(5)
Process: 31095 ExecStartPost=/usr/libexec/chrony-helper update-daemon (code=exited, status=0/SUCCESS)
Process: 31090 ExecStart=/usr/sbin/chronyd $OPTIONS (code=exited, status=0/SUCCESS)
Main PID: 31092 (chronyd)
Tasks: 1
Memory: 464.0K
CGroup: /system.slice/chronyd.service
└─31092 /usr/sbin/chronydsystemctl stop firewalld && systemctl disable firewalldsed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config
setenforce 0
reboot # 重启系统[root@jeven ~]# getenforce
Disabled修改/etc/chrony.conf 文件
vim /etc/chrony.confserver ntp1.aliyun.com iburst
allow 192.168.3.0/24
allow 192.168.3.0/24
local stratum 10重启chronyd服务
systemctl restart chronyd直接安装chrony软件
yum -y install chrony启动chronyd服务
systemctl enable --now chronydchrony.conf配置文件
vim /etc/chrony.confpool 192.168.3.166 iburst
重启chronyd服务
systemctl restart chronyd执行以下命令,查看时间同步状态。
[root@docker ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address Stratum Poll Reach LastRx Last sample
===============================================================================
^* jeven.dhcp-route 3 6 17 10 +15us[ +52us] +/- 23ms执行以下命令,列出NTP服务器的详细信息。
[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
===============================================================================
^* jeven.dhcp-route 3 6 37 7 +14us[ +175us] +/- 19ms设置当前时间为错误时间
date -s "2012-12-12 12:12:12"查看当前时间,发现已经修改为错误时间。
[root@docker ~]# date
Wed Dec 12 12:12:13 CST 2012执行以下命令,手动立即同步时间。
[root@docker ~]# sudo chronyc makestep
200 OK查看当前时间,发现错误时间已经修改为正确时间。
[root@docker ~]# date
Wed Nov 1 17:09:22 CST 2023我正在参与2023腾讯技术创作特训营第三期有奖征文,组队打卡瓜分大奖!
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。