CentOS7使用chronyd进行时间同步

本文介绍了在CentOS7中如何使用chronyd进行时间同步,包括设置ntp服务器为阿里云,以及在无法连接外网时如何配置内网主机作为时间服务器。通过调整chronyd配置文件和重启服务,确保了系统时间的准确同步。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

学习安装部署 ceph 时 ,在添加 mon 时报错了,搜索原因后发现是 时间同步问题。于是学习一下时间同步工具。

一般CentOS6 使用的时间同步工具是ntp。现在还有不少开源软件文档建议安装的时间同步工具是ntp。个人感觉 chrony使用应该和ntp 差不多。本人使用 ntp较少,因为一直CentOS7虚拟机较多。CentOS7系统自带安装了chrony,然后安装ntp 并设置开机自启后,经常性的看到ntpd服务并不能开机启动,和chronyd冲突了。感觉也没必要为了使用ntp 把 自带的 chrony 卸载了。。。

这篇博客只是一个特别简单的实验说明。。。可能并不能解决大多数问题,供自己复习使用。

使用的CentOS7 虚拟机。
实验准备 现在四台虚拟主机的IP 主机名分别是
192.168.199.80 ceph-admin
192.168.199.81 ceph-1
192.168.199.82 ceph-2
192.168.199.83 ceph-3

要求相互之间可以ping通。。。

查看 chronyd 服务运行情况。

默认情况下 CentOS7 的chronyd 服务时开机自启的。

systemctl status chronyd

chronyd 状态
正常运行

chronyc查看当前同步情况

chronyc sources

时间同步情况同步情况
可以看到有同步的服务器。CentOS7系统默认的,如果安装的CentOS7 可以联网 应该是可以同步的。不过这几个服务器应该在国外。效果应该一般。不然我部署ceph应该不会失败。。。

查看一下当前时间 没什么意义。。。因为最主要保证的是时间同步。如果时间同步了。即使服务器的时间与当前标准时间对不上也没关系(应该是这样)。。。

timedatectl

当前时间

设置ntp服务器为阿里,进行时间同步。

编辑 chronyd 配置文件。

vim /etc/chrony.conf

可以看到 原有的四个时间同步服务器

server 0.centos.pool.ntp.org iburst
server 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst

如果我们希望简单一点。那就直接设置成阿里或者其他国内的开放ntp服务器就可以。。。

ntp.aliyun.com
ntp1.aliyun.com
ntp2.aliyun.com
ntp3.aliyun.com
ntp4.aliyun.com
ntp5.aliyun.com
ntp6.aliyun.com
ntp7.aliyun.com

把四个主机的配置文件原有时间服务器注释掉。然后添加阿里的

server ntp.aliyun.com iburst
server ntp1.aliyun.com iburst
server ntp2.aliyun.com iburst
server ntp3.aliyun.com iburst

重启 chronyd 服务

systemctl restart chronyd

查看时间同步信息

chronyc sources

阿里云时间同步
时间同步成功。。。
现在四个主机都使用阿里云的ntp服务器作为时间同步的依据。。一般来说由于时间同步引发的问题都可以解决。。。

设置内网主机作为 ntp 服务器。

可能有各种各样的原因。比如,不能连接外网等。不愿意使用开放的ntp服务器。。。这时候也可以搭建一个简单地时间同步服务器。
比如192.168.199.80 ceph-admin 作为时间同步服务器。。。

依然是修改 chrony 配置文件。。。将server 改成192.168.199.80

vim /etc/chrony.conf
server 192.168.199.80   iburst

然后重启 chronyd。

systemctl restart chronyd

查看时间 同步状态。。。可以发现 ceph-1 ceph-2 ceph-3 时间状态是正常的

chronyc sources
timedatectl

时间同步状态
NTP sychronized 为 yes

但是有时候可能 ceph-admin 的时间同步状态不太对
ceph-admin 时间同步状态
NTP sychronized 为 no

chronyc sources -v

时间未同步
^表示服务,*代表同步成功,?代表未同步
也就是说服务器本身未同步???

继续修改ceph-admin 的 chrony 配置文件。。。
取消注释或添加

allow 192.168.199.0/24  
local stratum 10

如果要设置多个网段,设置多个allow。
192.168.199.0/24 是 内网网络 也不算内网。算本地网络?反正就那意思。。。
修改完成重启 chronyd

 systemctl restart chronyd

查看时间同步状态
时间同步成功

附。根据网络资料 有时候 要关闭本地时间使用
timedatectl set-local-rtc 0 

这样使用的就是utc时间
timedatectl 时间控制工具。查看使用帮助

timedatectl -h 
[root@storage-a002a2 ~]# timedatectl -h
timedatectl [OPTIONS...] COMMAND ...

Query or change system time and date settings.

  -h --help                Show this help message
     --version             Show package version
     --no-pager            Do not pipe output into a pager
     --no-ask-password     Do not prompt for password
  -H --host=[USER@]HOST    Operate on remote host
  -M --machine=CONTAINER   Operate on local container
     --adjust-system-clock Adjust system clock when changing local RTC mode

Commands:
  status                   Show current time settings
  set-time TIME            Set system time
  set-timezone ZONE        Set system time zone
  list-timezones           Show known time zones
  set-local-rtc BOOL       Control whether RTC is in local time
  set-ntp BOOL             Control whether NTP is enabled

简明扼要。。。
设置时间就用timedatectl set-time 在ntp同步下不可以设置时间
设置时区 timedatectl set-timezone ZONE
查看所有时区 timedatectl list-timezones
set-local-rtc 就是设置是否用本地时钟一般采用ntp同步时间的话都会设置为0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值