架構(gòu)圖
image.png
一马澈、環(huán)境準(zhǔn)備
- 兩或多臺 Centos 7 系統(tǒng)主機
- 確保主機內(nèi)網(wǎng)互通,作為服務(wù)端主機多添加一張網(wǎng)卡需能與外網(wǎng)互通
- 修改服務(wù)端主機名為ntp-server蔗喂,客戶端為ntp-client-node1
- 編輯各主機/etc/hosts文件律罢,配置好主機名解析
- 關(guān)閉防火墻
二婚陪、通過 timedatectl 命令設(shè)置時區(qū)信息
簡單的介紹timedatectl命令用法:
# timedatectl 查看主機中的時間時區(qū)等信息
[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 查看當(dāng)前可用的時區(qū)
[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 設(shè)置本地時區(qū)為上海
[root@ntp-server ~]# timedatectl list-timezones |grep -i "shanghai" #過濾出上海時區(qū)
Asia/Shanghai
[root@ntp-server ~]#
[root@ntp-server ~]# timedatectl set-timezone Asia/Shanghai #設(shè)置本地時區(qū)為上海
[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 設(shè)置時間日期
[root@ntp-server ~]# timedatectl set-time "2020-4-2 08:56:01"
# timedatectl set-local-rtc 設(shè)置硬件時鐘為本地時區(qū)
[root@ntp-server ~]# timedatectl set-local-rtc 1
# timedatectl set-local-rtc 設(shè)置硬件時鐘為協(xié)調(diào)世界時UTC
[root@ntp-server ~]# timedatectl set-local-rtc 0
# timedatectl set-ntp 手動設(shè)置是否使用ntp同步
[root@ntp-server ~]# timedatectl set-ntp true
注意:timedatectl 在這里我們只需要設(shè)置時區(qū)為上海即可,其他設(shè)置不用管
三渠牲、ntp-server 端安裝和配置 chrony 服務(wù)
- 安裝chrony
[root@ntp-server ~]# yum install chrony -y
2.修改chrony服務(wù)配置文件 /etc/chrony.conf
[root@ntp-server ~]# vim /etc/chrony.conf
# x.centos.pool.ntp.org 遠(yuǎn)程時間同步服務(wù)器地址旋炒,可自行更改,建議都注釋掉改為國內(nèi)時間同步地址
# 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修改為允許同步的網(wǎng)段签杈,這里得修改為ntp-client節(jié)點ip段
# Allow NTP client access from local network.
allow 192.168/16
...
3.開啟chronyd服務(wù)瘫镇,并設(shè)置開機自啟
[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
# 查看一下當(dāng)前時間,這個是正確的時間
[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狀態(tài)
[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說明當(dāng)前未同步遠(yuǎn)程ntp
RTC in local TZ: no
DST active: n/a
[root@ntp-server ~]#
# 先手動重新啟下 chronyd 服務(wù)
[root@ntp-server ~]# systemctl restart chronyd
# 通過chronyc source 進(jìn)行同步
[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 ~]#
# 最后查看當(dāng)前時間答姥,已經(jīng)為同步為正確時間了
[root@ntp-server ~]# date
Thu Apr 2 10:05:39 CST 2020
[root@ntp-server ~]#
四铣除、ntp-client-node1 客戶端 同步
1.客戶端節(jié)點安裝chrony
[root@ntp-client-node1 ~]# yum install chronyd -y
2.編輯客戶端chrony配置文件
[root@ntp-client-node1 ~]# vi /etc/chrony.conf
# 注釋掉自帶的遠(yuǎn)程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.啟動服務(wù)鹦付,設(shè)置開機自啟
[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地址不可達(dá) 則需要檢測內(nèi)網(wǎng)是否能通尚粘,或者防火墻是否關(guān)閉,或者h(yuǎn)osts文件是配置正確
- 檢驗敲长,也可以向服務(wù)端的檢測方法一樣先修改為錯誤時間在進(jìn)行同步正確時間
[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 ~]#