MySQL, Oracle, Linux, 软件架构及大数据技术知识分享平台

网站首页 > linux / 正文

linux chronyd时间同步服务

2024-11-26 15:29 huorong linux 3 ℃ 0 评论

使用chronyc

可以通过运行chronyc命令来修改设置,命令如下:
accheck - 检查NTP访问是否对特定主机可用
activity - 该命令会显示有多少NTP源在线/离线
add server - 手动添加一台新的NTP服务器。
clients - 在客户端报告已访问到服务器
delete - 手动移除NTP服务器或对等服务器
settime - 手动设置守护进程时间
tracking - 显示系统时间信息

可以通过使用帮助命令查看完整的命令列表。

配置Chrony

以下是系统默认配置文件(/etc/chrony.conf)

# 使用pool.ntp.org项目中的公共服务器。以server开,理论上你想添加多少时间服务器都可以。
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
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

# 根据实际时间计算出服务器增减时间的比率,然后记录到一个文件中,在系统重启后为系统做出最佳时间补偿调整。
driftfile /var/lib/chrony/drift

# chronyd根据需求减慢或加速时间调整,
# 在某些情况下系统时钟可能漂移过快,导致时间调整用时过长。
# 该指令强制chronyd调整时期,大于某个阀值时步进调整系统时钟。
# 只有在因chronyd启动时间超过指定的限制时(可使用负值来禁用限制)没有更多时钟更新时才生效。
makestep 1.0 3

# 将启用一个内核模式,在该模式中,系统时间每11分钟会拷贝到实时时钟(RTC)。
rtcsync

# Enable hardware timestamping on all interfaces that support it.
# 通过使用hwtimestamp指令启用硬件时间戳
#hwtimestamp eth0
#hwtimestamp eth1
#hwtimestamp *

# Increase the minimum number of selectable sources required to adjust
# the system clock.
#minsources 2

# 指定一台主机、子网,或者网络以允许或拒绝NTP连接到扮演时钟服务器的机器
#allow 192.168.1.0/16
#deny 192.168/16

# Serve time even if not synchronized to a time source.
local stratum 10

# 指定包含NTP验证密钥的文件。
#keyfile /etc/chrony.keys

# 指定日志文件的目录。
logdir /var/log/chrony

# Select which information is logged.
#log measurements statistics tracking

设置时区

查看当前系统时区:$ timedatectl

如果你当前的时区不正确,请按照以下操作设置。

查看所有可用的时区:
$ timedatectl list-timezones

筛选式查看在亚洲S开的上海可用时区:
$ timedatectl list-timezones |  grep  -E "Asia/S.*"
Asia/Shanghai

设置当前系统为Asia/Shanghai上海时区:
$ timedatectl set-timezone Asia/Shanghai

设置完时区后,强制同步下系统时钟:
$ chronyc -a makestep
200 OK

常用命令

# 查看时间同步源
chronyc sources -v

# 查看时间同步源状态
chronyc sourcestats -v

# 查看 ntp_servers 是否在线
 chronyc activity -v
 
# 查看 ntp 详细信息
chronyc tracking -v

# 强制同步下系统时钟
chronyc -a makestep

# 硬件时间默认为UTC:
timedatectl set-local-rtc 1

# 启用NTP时间同步:
timedatectl set-ntp yes


注意:配置完/etc/chrony.conf后,需重启chrony服务,否则可能会不生效。

Tags:linux获取系统时间

控制面板
您好,欢迎到访网站!
  查看权限
网站分类
最新留言