Chronyd同步时间(Server/Client)配置

chronyd时间服务器,和之前NTP的功能一样,提供时间的。

基础概念我就不说了,不擅长。chronyd是NTP之后诞生的。

最近下载了最新的Centos Linux 8.1,发现里面没有ntp服务了,百度一搜发现改为了Chronyd,这篇文章就是部署一个服务器+客户端测试。

1、服务器端

Centos Linux 8.1 默认安装了chronyd服务。我们只需要配置即可。

配置文件配置

红色部分是需要服务器配置的,这是基本配置,主要在于实现基本功能。

vim /etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# pool 2.centos.pool.ntp.org iburst
# 同步哪台服务器上的时间
server 192.168.94.133 iburst
# Record the rate at which the system clock gains/losses time.
driftfile /var/lib/chrony/drift

# Allow the system clock to be stepped in the first three updates
# if its offset is larger than 1 second.
makestep 1.0 3

# Enable kernel synchronization of the real-time clock (RTC).
rtcsync

# Allow NTP client access from local network.
# allow 192.168.0.0/16
# 允许哪个网段的客户端同步时间,133这台是服务器。
allow 192.168.94.0/24

# Serve time even if not synchronized to a time source.
# 字面翻译,提供时间即使没有同步一个时间源。
local stratum 10
# Specify file containing keys for NTP authentication.
keyfile /etc/chrony.keys

# Get TAI-UTC offset and leap seconds from the system tz database.
leapsectz right/UTC

启动chronyd,并设置开机自启

[root@filesystem ~]# systemctl start chronyd
[root@filesystem ~]# systemctl enable chronyd

查看时间同步情况,本地能够同步才能够让其他节点进行同步

timedatectl是一个时间配置工具,用于设定时间、与时间相关的配置等。

[root@filesystem ~]# timedatectl
               Local time: 五 2020-05-29 11:17:46 EDT
           Universal time: 五 2020-05-29 15:17:46 UTC
                 RTC time: 五 2020-05-29 15:17:46
                Time zone: America/New_York (EDT, -0400)
# 当此值为yes代表同步成功,如果为no就代表未同步,或同步失败。
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@filesystem ~]#

查看当前时间是从哪里同步过来的,这里显示的是主机名。不影响。

[root@filesystem ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* filesystem                   10   7   377   537    +11us[  +25us] +/-   30us
[root@filesystem ~]#

防火墙配置

防火墙要能放行此服务,关闭防火墙或是放行相应的端口及服务。

关闭防火墙,并设置开机不启动

[root@filesystem ~]# systemctl stop firewalld
[root@filesystem ~]# systemctl disable firewalld
Removed /etc/systemd/system/multi-user.target.wants/firewalld.service.
Removed /etc/systemd/system/dbus-org.fedoraproject.FirewallD1.service.
[root@filesystem ~]#

设置防火墙放行规则

[root@filesystem ~]# netstat -antup | grep chrony
udp        0      0 127.0.0.1:323           0.0.0.0:*                           35191/chronyd
udp        0      0 0.0.0.0:123             0.0.0.0:*                           35191/chronyd
udp6       0      0 ::1:323                 :::*                                35191/chronyd
[root@filesystem ~]#
# 只有upd 123端口
[root@filesystem ~]# firewall-cmd --permanent --add-port=123/udp
success
[root@filesystem ~]# firewall-cmd --reload
success
[root@filesystem ~]#

2、客户端配置

配置文件配置

vim /etc/chrony.conf

# Use public servers from the pool.ntp.org project.
# Please consider joining the pool (http://www.pool.ntp.org/join.html).
# pool 2.centos.pool.ntp.org iburst
server 192.168.94.133 iburst

配置开机自启

[root@node1 ~]# systemctl restart chronyd
[root@node1 ~]# systemctl enable chronyd
[root@node1 ~]#

查看同步状态及信息

[root@node1 ~]# timedatectl
               Local time: 五 2020-05-29 11:28:12 EDT
           Universal time: 五 2020-05-29 15:28:12 UTC
                 RTC time: 五 2020-05-29 15:28:12
                Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@node1 ~]# chronyc sources
210 Number of sources = 1
MS Name/IP address         Stratum Poll Reach LastRx Last sample
===============================================================================
^* filesystem                   11   6    17    34   -520ns[+7955ns] +/-   15ms
[root@node1 ~]#

我们看到是yes,但是如何判断它是原来的时间还是同步好了。毕竟很多系统不同步时间也是近似的。

3、设置时间

我们对chronyd服务器端进行时间修改。

这里说明一点,时间的修改只能在时间不进行同步的状态下进行,也就是说,如果时间是同步中的,是不允许修改时间的。对时间进行保护。

[root@filesystem ~]# timedatectl
               Local time: 五 2020-05-29 11:30:40 EDT
           Universal time: 五 2020-05-29 15:30:40 UTC
                 RTC time: 五 2020-05-29 15:30:40
                Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@filesystem ~]#

我们测试效果,关掉ntp,修改时间

timedatectl set-ntp 0

[root@filesystem ~]# timedatectl set-ntp 0
[root@filesystem ~]# timedatectl set-time "2020-06-01 01:01:01"
[root@filesystem ~]# timedatectl set-ntp 1
[root@filesystem ~]# systemctl restart chronyd
[root@filesystem ~]# timedatectl
               Local time: 一 2020-06-01 01:01:20 EDT
           Universal time: 一 2020-06-01 05:01:20 UTC
                 RTC time: 一 2020-06-01 05:01:20
                Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@filesystem ~]#

客户端查看时间

[root@node1 ~]# systemctl restart chronyd
[root@node1 ~]#
[root@node1 ~]# timedatectl
               Local time: 一 2020-06-01 01:01:55 EDT
           Universal time: 一 2020-06-01 05:01:55 UTC
                 RTC time: 五 2020-05-29 15:46:52
                Time zone: America/New_York (EDT, -0400)
System clock synchronized: yes
              NTP service: active
          RTC in local TZ: no
[root@node1 ~]#

客户端不重启服务也是能同步的,如果你下去做实验的话,可以测试下。


读书和健身总有一个在路上

原文地址:https://www.cnblogs.com/Renqy/p/12987125.html