centos 时间同步

一、安装部署时间同步

1、安装服务,并加入自启动

yum -y install chrony
systemctl enable chronyd
systemctl start chronyd

2、第一台master节点配置

Chrony的配置文件是/etc/chrony.conf

# 使用 pool.ntp.org 项目中的公共服务器。以server开,理论上想添加多少时间服务器都可以。
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
###默认是国外的,访问起来很慢
server 0.centos.pool.ntp.org iburst ###此处修改为阿里云的时间同步服务器 server ntp1.aliyun.com iburst
server 1.centos.pool.ntp.org iburst ###此处修改为阿里云的时间同步服务器 server ntp2.aliyun.com iburst


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


# 如果系统时钟的偏移量大于1秒,则允许系统时钟在前三次更新中步进。
# Allow the system clock to be stepped in the first three updates if its offset is larger than 1 second.
makestep 1.0 3


# 启用实时时钟(RTC)的内核同步。
# Enable kernel synchronization of the real-time clock (RTC).
rtcsync


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


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


# 指定 NTP 客户端地址,以允许或拒绝连接到扮演时钟服务器的机器
# Allow NTP client access from local network.
#allow 192.168.0.0/16 ####第一台master节点需要把这个注释解除,修改为客户端网段: allow 172.24.169.0/24 或者 allow all


# Serve time even if not synchronized to a time source.
local stratum 10     #这个配置需要打开


# 指定包含 NTP 身份验证密钥的文件。
# Specify file containing keys for NTP authentication.
#keyfile /etc/chrony.keys


# 指定日志文件的目录。
# Specify directory for log files.
logdir /var/log/chrony


# 选择日志文件要记录的信息。
# Select which information is logged.
#log measurements statistics tracking

3、其他节点配置为同步第一台mater的时间

/etc/chrony.conf


# 使用 pool.ntp.org 项目中的公共服务器。以server开,理论上想添加多少时间服务器都可以。
# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
server 172.24.169.96 iburst ####此处修改为第一台master节点的ip地址

4、重启服务

systemctl restart chronyd

5、查看时间同步状态

timedatectl status

6、开启网络时间同步,第一台master可以执行,也可以忽略

timedatectl set-ntp true

二、常用的chrony命令

## 查看 ntp_servers

[root@baidu-node98-ai ~]# chronyc sources -v
210 Number of sources = 2


.-- 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
===============================================================================
^+ baidu-node96-ai.com 3 6 377 35 -363us[ -365us] +/- 20ms
^* baidu-node97-ai.com 3 6 377 26 +372us[ +370us] +/- 20ms
^? baidu-node93-ai.com 3 6 377 26 +372us[ +300us] +/- 20ms


M 这表示信号源的模式。^表示服务器,=表示对等方,#表示本地连接的参考时钟
S  此列指示源的状态:
^+表示可接受的信号源,与选定的信号源组合在一起
^*表示从该服务器进行了时钟同步
^?则说明没有从该服务器同步时钟,有可能网络不通,不可达
^-表示被合并算法排除的可接受源
^x表示chronyd认为是虚假行情的时钟(即,其时间与大多数其他来源不一致)
^~表示时间似乎具有太多可变性的来源

##查看 ntp_servers 状态

[root@baidu-node98-ai ~]# chronyc sourcestats -v
210 Number of sources = 2
.- Number of sample points in measurement set.
/ .- Number of residual runs with same sign.
| / .- Length of measurement set (time).
| | / .- Est. clock freq error (ppm).
| | | / .- Est. error in freq.
| | | | / .- Est. offset.
| | | | | | On the -.
| | | | | | samples. \
| | | | | | |
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev ####Offset表示与服务器同步了多长时间
==============================================================================
baidu-node96-ai.com 7 4 389 -0.051 0.063 -365us 2805ns
baidu-node97-ai.com 12 10 711 +0.010 0.019 +368us 3112ns

##查看 ntp_servers 是否在线

[root@baidu-node98-ai ~]# chronyc activity -v
200 OK
2 sources online ##表示两个时间同步服务器在线
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address

##查看 ntp 详细信息

[root@baidu-node98-ai ~]# chronyc tracking -v
Reference ID : AC18A961 (baidu-node97-ai.com)
Stratum : 4
Ref time (UTC) : Wed Mar 23 07:16:09 2022
System time : 0.000019015 seconds slow of NTP time
Last offset : -0.000001610 seconds
RMS offset : 0.000022228 seconds
Frequency : 11.035 ppm fast
Residual freq : -0.000 ppm
Skew : 0.020 ppm
Root delay : 0.035532806 seconds
Root dispersion : 0.001703597 seconds
Update interval : 64.9 seconds
Leap status : Normal

##强制同步下系统时钟

[root@baidu-node98-ai ~]# chronyc -a makestep

三、timedatectl 命令

##查看日期时间、时区及 NTP 状态

[root@baidu-node98-ai ~]# timedatectl
Local time: 三 2022-03-23 15:26:41 CST
Universal time: 三 2022-03-23 07:26:41 UTC
RTC time: 三 2022-03-23 07:26:41
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a

##查看时区列表

timedatectl list-timezones
timedatectl list-timezones | grep -E "Asia/S.*"

##修改时区为上海时区

timedatectl set-timezone Asia/Shanghai

##修改日期时间(可以只修改其中一个)

timedatectl set-time "2022-03-23 15:50:20"

##开启 NTP

timedatectl set-ntp true/flase

四、系统时钟与硬件时钟之间同步

##查看时间

##查看系统时间
[root@baidu-node98-ai ~]# date
2022年 03月 23日 星期三 15:30:37 CST
##查看硬件时间
[root@baidu-node98-ai ~]# hwclock
2022年03月23日 星期三 15时30分36秒 -0.516669 秒

##设置硬件时钟

hwclock -w

##设置系统时间

hwclock -s

##修改配置文件方式
vim /etc/sysconfig/ntpd
将系统时间写入BIOS,与 hwclock -w 效果相同

SYNC_HWCLOCK=yes

##将硬件时钟调整为与系统时钟一致

[root@localhost ~]# timedatectl set-local-rtc 1
或者
[root@localhost ~]# hwclock --systohc --localtime

##将日期写入CMOS

[root@localhost ~]# clock –w
© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容