chrony 时间同步服务
2020-04-02 本文已影响0人
一枝哀
架构图
image.png一、环境准备
- 两或多台 Centos 7 系统主机
- 确保主机内网互通,作为服务端主机多添加一张网卡需能与外网互通
- 修改服务端主机名为ntp-server,客户端为ntp-client-node1
- 编辑各主机/etc/hosts文件,配置好主机名解析
- 关闭防火墙
二、通过 timedatectl 命令设置时区信息
简单的介绍timedatectl命令用法:
# timedatectl 查看主机中的时间时区等信息
[root@ntp-server ~]# timedatectl
Local time: Thu 2020-04-02 04:43:18 EDT
Universal time: Thu 2020-04-02 08:43:18 UTC
RTC time: Thu 2020-04-02 08:43:20
Time zone: America/New_York (EDT, -0400)
NTP enabled: n/a # ntp 是否自启
NTP synchronized: no # ntp 时间同步是否开启
RTC in local TZ: no
DST active: yes
Last DST change: DST began at
Sun 2020-03-08 01:59:59 EST
Sun 2020-03-08 03:00:00 EDT
Next DST change: DST ends (the clock jumps one hour backwards) at
Sun 2020-11-01 01:59:59 EDT
Sun 2020-11-01 01:00:00 EST
[root@ntp-server ~]#
# timedatectl list-timezones 查看当前可用的时区
[root@ntp-server ~]# timedatectl list-timezones
Africa/Abidjan
Africa/Accra
Africa/Addis_Ababa
Africa/Algiers
Africa/Asmara
Africa/Bamako
Africa/Bangui
Africa/Banjul
Africa/Bissau
Africa/Blantyre
[root@ntp-server ~]#
# timedatectl 设置本地时区为上海
[root@ntp-server ~]# timedatectl list-timezones |grep -i "shanghai" #过滤出上海时区
Asia/Shanghai
[root@ntp-server ~]#
[root@ntp-server ~]# timedatectl set-timezone Asia/Shanghai #设置本地时区为上海
[root@ntp-server ~]# timedatectl
Local time: Thu 2020-04-02 16:53:40 CST
Universal time: Thu 2020-04-02 08:53:40 UTC
RTC time: Thu 2020-04-02 08:53:42
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: n/a
NTP synchronized: no
RTC in local TZ: no
DST active: n/a
[root@ntp-server ~]#
# timedatectl set-time 设置时间日期
[root@ntp-server ~]# timedatectl set-time "2020-4-2 08:56:01"
# timedatectl set-local-rtc 设置硬件时钟为本地时区
[root@ntp-server ~]# timedatectl set-local-rtc 1
# timedatectl set-local-rtc 设置硬件时钟为协调世界时UTC
[root@ntp-server ~]# timedatectl set-local-rtc 0
# timedatectl set-ntp 手动设置是否使用ntp同步
[root@ntp-server ~]# timedatectl set-ntp true
注意:timedatectl 在这里我们只需要设置时区为上海即可,其他设置不用管
三、ntp-server 端安装和配置 chrony 服务
- 安装chrony
[root@ntp-server ~]# yum install chrony -y
2.修改chrony服务配置文件 /etc/chrony.conf
[root@ntp-server ~]# vim /etc/chrony.conf
# x.centos.pool.ntp.org 远程时间同步服务器地址,可自行更改,建议都注释掉改为国内时间同步地址
# 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 1.centos.pool.ntp.org iburst
server 2.centos.pool.ntp.org iburst
server 3.centos.pool.ntp.org iburst
...
# allow后面ip修改为允许同步的网段,这里得修改为ntp-client节点ip段
# Allow NTP client access from local network.
allow 192.168/16
...
3.开启chronyd服务,并设置开机自启
[root@ntp-server ~]# systemctl start chronyd.service
[root@ntp-server ~]# systemctl enable chronyd.service
4.检查一下是否自动开启了ntp同步
[root@ntp-server ~]# timedatectl
Local time: Thu 2020-04-02 09:44:45 CST
Universal time: Thu 2020-04-02 01:44:45 UTC
RTC time: Thu 2020-04-02 01:44:45
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes # 为yes就行
NTP synchronized: yes # yes
RTC in local TZ: no
DST active: n/a
[root@ntp-server ~]
5.验证ntp-server端时间是否同步了阿里云ntp
# 查看一下当前时间,这个是正确的时间
[root@ntp-server ~]# date
Thu Apr 2 09:57:45 CST 2020
[root@ntp-server ~]#
# 手动修改为一个错误的时间
[root@ntp-server ~]# date -s "2019/3/1 09:00:00"
Fri Mar 1 09:00:00 CST 2019
[root@ntp-server ~]
# 修改后timedatectl查看一下ntp状态
[root@ntp-server ~]# timedatectl
Local time: Fri 2019-03-01 09:00:53 CST
Universal time: Fri 2019-03-01 01:00:53 UTC
RTC time: Thu 2020-04-02 02:01:06
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: no #为no说明当前未同步远程ntp
RTC in local TZ: no
DST active: n/a
[root@ntp-server ~]#
# 先手动重新启下 chronyd 服务
[root@ntp-server ~]# systemctl restart chronyd
# 通过chronyc source 进行同步
[root@ntp-server ~]# 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
===============================================================================
^- 120.25.115.20 2 6 17 35 -202us[ -202us] +/- 18ms
^* 203.107.6.88 2 6 17 45 +2521us[ +935us] +/- 26ms
[root@ntp-server ~]#
# 最后查看当前时间,已经为同步为正确时间了
[root@ntp-server ~]# date
Thu Apr 2 10:05:39 CST 2020
[root@ntp-server ~]#
四、ntp-client-node1 客户端 同步
1.客户端节点安装chrony
[root@ntp-client-node1 ~]# yum install chronyd -y
2.编辑客户端chrony配置文件
[root@ntp-client-node1 ~]# vi /etc/chrony.conf
# 注释掉自带的远程ntp地址,使用我们ntp-server端的ip或主机名
# 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 1.centos.pool.ntp.org iburst
#server 2.centos.pool.ntp.org iburst
#server 3.centos.pool.ntp.org iburst
server ntp-server iburst
3.启动服务,设置开机自启
[root@ntp-client-node1 ~]# systemctl start chronyd
[root@ntp-client-node1 ~]# systemctl enable chronyd
4.查看并同步源
[root@ntp-client-node2 ~]# chronyc sources -v
210 Number of sources = 1
.-- 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
===============================================================================
^* ntp-server 3 6 37 40 -1204ns[ -312us] +/- 17ms
# 注意如果开头是 ^? 说明ntp地址不可达 则需要检测内网是否能通,或者防火墙是否关闭,或者hosts文件是配置正确
- 检验,也可以向服务端的检测方法一样先修改为错误时间在进行同步正确时间
[root@ntp-client-node2 ~]# timedatectl
Local time: Thu 2020-04-02 12:05:33 CST
Universal time: Thu 2020-04-02 04:05:33 UTC
RTC time: Thu 2020-04-02 04:05:33
Time zone: Asia/Shanghai (CST, +0800)
NTP enabled: yes
NTP synchronized: yes
RTC in local TZ: no
DST active: n/a
[root@ntp-client-node2 ~]#