chrony 時間同步服務(wù)

架構(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ù)

  1. 安裝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文件是配置正確
  1. 檢驗敲长,也可以向服務(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 ~]#
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末郎嫁,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子祈噪,更是在濱河造成了極大的恐慌泽铛,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,546評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件钳降,死亡現(xiàn)場離奇詭異厚宰,居然都是意外死亡,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,224評論 3 395
  • 文/潘曉璐 我一進(jìn)店門铲觉,熙熙樓的掌柜王于貴愁眉苦臉地迎上來澈蝙,“玉大人,你說我怎么就攤上這事撵幽〉朴” “怎么了?”我有些...
    開封第一講書人閱讀 164,911評論 0 354
  • 文/不壞的土叔 我叫張陵盐杂,是天一觀的道長逗载。 經(jīng)常有香客問我,道長链烈,這世上最難降的妖魔是什么厉斟? 我笑而不...
    開封第一講書人閱讀 58,737評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮强衡,結(jié)果婚禮上擦秽,老公的妹妹穿的比我還像新娘。我一直安慰自己漩勤,他們只是感情好感挥,可當(dāng)我...
    茶點故事閱讀 67,753評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著越败,像睡著了一般触幼。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上究飞,一...
    開封第一講書人閱讀 51,598評論 1 305
  • 那天置谦,我揣著相機與錄音,去河邊找鬼噪猾。 笑死霉祸,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的袱蜡。 我是一名探鬼主播丝蹭,決...
    沈念sama閱讀 40,338評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼坪蚁!你這毒婦竟也來了奔穿?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,249評論 0 276
  • 序言:老撾萬榮一對情侶失蹤敏晤,失蹤者是張志新(化名)和其女友劉穎贱田,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體嘴脾,經(jīng)...
    沈念sama閱讀 45,696評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡男摧,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,888評論 3 336
  • 正文 我和宋清朗相戀三年蔬墩,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片耗拓。...
    茶點故事閱讀 40,013評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡拇颅,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出乔询,到底是詐尸還是另有隱情樟插,我是刑警寧澤,帶...
    沈念sama閱讀 35,731評論 5 346
  • 正文 年R本政府宣布竿刁,位于F島的核電站黄锤,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏食拜。R本人自食惡果不足惜鸵熟,卻給世界環(huán)境...
    茶點故事閱讀 41,348評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望负甸。 院中可真熱鬧旅赢,春花似錦、人聲如沸惑惶。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,929評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽带污。三九已至,卻和暖如春香到,著一層夾襖步出監(jiān)牢的瞬間鱼冀,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,048評論 1 270
  • 我被黑心中介騙來泰國打工悠就, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留千绪,地道東北人。 一個月前我還...
    沈念sama閱讀 48,203評論 3 370
  • 正文 我出身青樓梗脾,卻偏偏與公主長得像荸型,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子炸茧,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,960評論 2 355

推薦閱讀更多精彩內(nèi)容