Linux Chrony 设置服务器集群同步时间,chrony服务器集群


Linux 配置阿里 OPSX NTP服务

编辑文件 "/etc/ntp.conf",根据情况修改文件内容为:

互联网上的服务器:
driftfile  /var/lib/ntp/drift
pidfile   /var/run/ntpd.pid

Linux Chrony 设置服务器集群同步时间

导读

Chrony是一个开源的自由软件,像CentOS 7或基于RHEL 7操作系统,已经是默认服务,默认配置文件在 /etc/chrony.conf 它能保持系统时间与时间服务器(NTP)同步,让时间始终保持同步。相对于NTP时间同步软件,占据很大优势。其用法也很简单。

Chrony有两个核心组件,分别是:
chronyd:是守护进程,主要用于调整内核中运行的系统时间和时间服务器同步。它确定计算机增减时间的比率,并对此进行调整补偿。
chronyc:提供一个用户界面,用于监控性能并进行多样化的配置。它可以在chronyd实例控制的计算机上工作,也可以在一台不同的远程计算机上工作。

OS环境:

192.168.1.101 客户端
192.168.1.102 服务端
CentOS Linux release 7.4.1708 (Core)

情况说明:两台机器都是内网,将1.102作为NTP时间服务器,1.101到此机器上同步时间。

安装Chrony服务

1.安装Chrony

系统默认已经安装,如未安装,请执行以下命令安装:

$ yum install chrony -y

2.启动并加入开机自启动

$ systemctl enable chronyd.service
$ systemctl restart chronyd.service
$ systemctl status chronyd.service

3.Firewalld设置

$ firewall-cmd --add-service=ntp --permanent
$ firewall-cmd --reload

因NTP使用123/UDP端口协议,所以允许NTP服务即可。

4.配置Chrony

以下是系统默认配置文件,我对此加以说明:

$ cat /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.0.0/24
#deny 192.168/24

# 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

查看配置文件如下

[root@node1 ~]# egrep -v "#|^$" /etc/chrony.conf 
server ntp1.aliyun.com
server time1.aliyun.com
stratumweight 0
driftfile /var/lib/chrony/drift
rtcsync
makestep 10 3
allow 192.168.0.0/24
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
local stratum 10
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
noclientlog
logchange 0.5
logdir /var/log/chrony

5.设置时区

查看当前系统时区:

$ timedatectl
      Local time: Fri 2018-2-29 13:31:04 CST
  Universal time: Fri 2018-2-29 05:31:04 UTC
        RTC time: Fri 2018-2-29 08:17:20
       Time zone: Asia/Shanghai (CST, +0800)
     NTP enabled: yes
NTP synchronized: yes
 RTC in local TZ: no
      DST active: n/a

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

查看所有可用的时区:

$ timedatectl list-timezones

筛选式查看在亚洲S开的上海可用时区:

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

Asia/Sakhalin
Asia/Samarkand
Asia/Seoul
Asia/Shanghai
Asia/Singapore
Asia/Srednekolymsk

设置当前系统为Asia/Shanghai上海时区:

$ timedatectl set-timezone Asia/Shanghai

设置完时区后,强制同步下系统时钟:

$ chronyc -a makestep
200 OK
```shell

### 6.服务器集群之间的系统时间同步

在生产环境中,其网络都是内网结构,那么内网如何保证服务器之间的时间同步呢?其实这个问题很简单,只需要搭建一台内网时间服务器,然后让所有计算机都到服务端(192.168.1.102)去同步时间即可。

具体操作:在服务端注释以下内容:
```shell
#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

并添加以下内容:(表示与本机同步时间)

server 192.168.1.102 iburst

这样我们需求的一台内网时间服务器已经配置完毕。

同样在客户端注释掉其他server,并在客户端(192.168.1.101)添加以下:

server 192.168.1.102 iburst

到此已经完成系统时间的同步。如有多台机器,操作也是如此。

7.常用命令

查看时间同步源:

$ chronyc sources -v

查看时间同步源状态:

$ chronyc sourcestats -v

设置硬件时间

硬件时间默认为UTC:

$ timedatectl set-local-rtc 1

启用NTP时间同步:

$ timedatectl set-ntp yes

校准时间服务器:

$ chronyc tracking

accheck - 检查NTP访问是否对特定主机可用

activity - 该命令会显示有多少NTP源在线/离线

add server - 手动添加一台新的NTP服务器。

clients - 在客户端报告已访问到服务器

delete - 手动移除NTP服务器或对等服务器

settime - 手动设置守护进程时间

tracking - 显示系统时间信息

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

logfile /var/log/ntp.log restrict default kod nomodify notrap nopeer noquery restrict -6 default kod nomodify notrap nopeer noquery restrict 127.0.0.1 server 127.127.1.0 fudge 127.127.1.0 stratum 10 server ntp.aliyun.com iburst minpoll 4 maxpoll 10 restrict ntp.aliyun.com nomodify notrap nopeer noquery 阿里云 ECS 服务器:
driftfile  /var/lib/ntp/drift
pidfile   /var/run/ntpd.pid
logfile /var/log/ntp.log
restrict    default kod nomodify notrap nopeer noquery
restrict -6 default kod nomodify notrap nopeer noquery
restrict 127.0.0.1
server 127.127.1.0
fudge  127.127.1.0 stratum 10
server ntp.aliyun.com iburst minpoll 4 maxpoll 10
restrict ntp.aliyun.com nomodify notrap nopeer noquery

server ntp1.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
restrict ntp1.cloud.aliyuncs.com nomodify notrap nopeer noquery
server ntp2.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
restrict ntp2.cloud.aliyuncs.com nomodify notrap nopeer noquery
server ntp3.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
restrict ntp3.cloud.aliyuncs.com nomodify notrap nopeer noquery
server ntp4.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
restrict ntp4.cloud.aliyuncs.com nomodify notrap nopeer noquery
server ntp5.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
restrict ntp5.cloud.aliyuncs.com nomodify notrap nopeer noquery
server ntp6.cloud.aliyuncs.com iburst minpoll 4 maxpoll 10
restrict ntp6.cloud.aliyuncs.com nomodify notrap nopeer noquery

对于使用 chrony 客户端的 linux 主机

配置 '/etc/chrony.conf' 文件的内容为:
server ntp.aliyun.com iburst
stratumweight 0
driftfile /var/lib/chrony/drift
rtcsync
makestep 10 3
bindcmdaddress 127.0.0.1
bindcmdaddress ::1
keyfile /etc/chrony.keys
commandkey 1
generatecommandkey
logchange 0.5
logdir /var/log/chrony

相关内容