簡介
重要性
-
由于IT系統(tǒng)中,準確的計時非常重要,有很多種原因需要準確計時:
-
在網(wǎng)絡(luò)傳輸中,數(shù)據(jù)包括和日志需要準確的時間戳
-
各種應(yīng)用程序中,如訂單信息,交易信息等 都需要準確的時間戳
-
Linux的兩個時鐘
-
硬件時鐘RTC (Real Time Clock):即BIOS時鐘,也就是我們主板中用電池供電的時鐘,是將時間寫入到BIOS中,系統(tǒng)斷電后時間不會丟失,可以在開機時通過主板程序中進行設(shè)置
?# 查看硬件時間
?[root@server ~]# hwclock
?2023-05-30 09:48:32.535594+08:00
-
系統(tǒng)時鐘 (System Clock) :顧名思義也就是Linux系統(tǒng)內(nèi)核時鐘、軟件時鐘,是由Linux內(nèi)核來提供的,系統(tǒng)時鐘是基于內(nèi)存,如果系統(tǒng)斷電時間就會丟失
?# 查看系統(tǒng)時間
?[root@server ~]# date
?2023年 05月 30日 星期二 09:50:50 CST
??
?[root@server ~]# date -s 10:00 ? # 修改為錯誤的時間
?2023年 05月 30日 星期二 10:00:00 CST
?[root@server ~]# date
?2023年 05月 30日 星期二 10:00:01 CST
?[root@server ~]# hwclock -s ? ? ? # 向硬件時間同步
?[root@server ~]# date
?2023年 05月 30日 星期二 09:51:50 CST
設(shè)置日期時間
timedatectl命令設(shè)置
?# [root@server ~]# timedatectl # 顯示當前的日期和時間
? ? Local time: 三 2023-11-15 13:00:26 CST ? ? ? # 本地時間
? ? ? ? ? ? Universal time: 三 2023-11-15 05:00:26 UTC ? # 世界時間
? ? ? ? ? ? ? ? ? RTC time: 三 2023-11-15 06:16:16 ? ? ? # 硬件時間
? ? ? ? ? ? ? ? Time zone: Asia/Shanghai (CST, +0800) ? # 時區(qū)
?System clock synchronized: yes ? ? ? ? ? ? ? ? ? ? ? ? ?# 時間是否已同步
? ? ? ? ? ? ? NTP service: active ? ? ? ? ? ? ? ? ? ? ? # 時間同步服務(wù)已啟動
? ? ? ? ? RTC in local TZ: no # no表示硬件時鐘設(shè)置為協(xié)調(diào)世界時(UTC),yes表示硬件時鐘設(shè)置為本地時間
??
?[root@server ~]# systemctl status chronyd # 查看時間同步服務(wù)狀態(tài)(由于默認使用chrony服務(wù)同步時間,不再使用ntp服務(wù))
??
?[root@server ~]# timedatectl set-ntp no # 關(guān)閉時間同步,以方便修改日期時間
??
?[root@server ~]# systemctl status chronyd
??
?[root@server ~]# timedatectl set-time "2023-12-12" # 設(shè)置新日期
??
?[root@server ~]# timedatectl set-time "12:12:12"
??
?[root@server ~]# timedatectl
? ? ? ? ? ? ? ? Local time: 二 2023-12-12 12:12:22 CST
? ? ? ? ? ? Universal time: 二 2023-12-12 04:12:22 UTC
? ? ? ? ? ? ? ? ? RTC time: 二 2023-12-12 04:12:23
? ? ? ? ? ? ? ? Time zone: Asia/Shanghai (CST, +0800)
?System clock synchronized: no
? ? ? ? ? ? ? NTP service: inactive
? ? ? ? ? RTC in local TZ: no
??
?[root@server ~]# timedatectl list-timezones | grep Asia # 查看可用時區(qū)
??
?[root@server ~]# timedatectl set-timezone Asia/Shanghai # 設(shè)置時區(qū)
??
date命令設(shè)置
?[root@server ~]# date # 顯示
??
?[root@server ~]# date +"%Y-%m-%d %H:%M:%S" # 格式顯示
??
?# 設(shè)置日期時間
?[root@server ~]# date -s 2023-11-15
?2023年 11月 15日 星期三 00:00:00 CST
?[root@server ~]# date -s 14:33:33
?2023年 11月 15日 星期三 14:33:33 CST
?[root@server ~]# date
?2023年 11月 15日 星期三 14:33:34 CST
-
注意:以便于以后的實驗正常執(zhí)行,可恢復(fù)快照后繼續(xù)
NTP
-
NTP:(Network Time Protocol,網(wǎng)絡(luò)時間協(xié)議)是由RFC 1305定義的時間同步協(xié)議,用來在分布式時間服務(wù)器和客戶端之間進行時間同步。
-
NTP基于UDP報文進行傳輸,使用的UDP端口號為123
-
NTP可以對網(wǎng)絡(luò)內(nèi)所有具有時鐘的設(shè)備進行時鐘同步,使網(wǎng)絡(luò)內(nèi)所有設(shè)備的時鐘保持一致,從而使設(shè)備能夠提供基于統(tǒng)一時間的多種應(yīng)用,對于運行NTP的本地系統(tǒng),既可以接受來自其他時鐘源的同步,又可以作為時鐘源同步其他的時鐘,并且可以和其他設(shè)備互相同步。
-
NTP的其精度在局域網(wǎng)內(nèi)可達0.1ms,在互聯(lián)網(wǎng)上絕大多數(shù)的地方其精度可以達到1-50ms
Chrony介紹
-
chrony是一個開源的自由軟件,它能幫助你保持系統(tǒng)時鐘與時鐘服務(wù)器(NTP)同步,因此讓你的時間保持精確。
-
chrony由兩個程序組成,分別是chronyd和chronyc
-
chronyd:是一個后臺運行的守護進程,用于調(diào)整內(nèi)核中運行的系統(tǒng)時鐘和時鐘服務(wù)器同步。它確定計算機增減時間的比率,并對此進行補償。
-
chronyc:提供了一個用戶界面,用于監(jiān)控性能并進行多樣化的配置。它可以在chronyd實例控制的計算機上工作,也可以在一臺不同的遠程計算機上工作
-
-
注意:Chrony與NTP都是時間同步軟件,兩個軟件不能夠同時開啟,會出現(xiàn)時間沖突,openeuler中默認使用chrony作為時間服務(wù)器,不在支持NTP軟件包
?[root@server ~]# systemctl status ntp # 查看ntp狀態(tài)
安裝與配置
安裝:
?# 默認已安裝,若需要安裝則可執(zhí)行:
??
?[root@server ~]# yum install chrony -y
??
?[root@server ~]# systemctl start chronyd
??
?[root@server ~]# systemctl enable chronyd
Chrony配置文件分析
-
主配置文件:/etc/chrony.conf
?[root@server ~]# vim /etc/chrony.conf
??
?# 使用 pool.ntp.org 項目中的公共服務(wù)器。
?# 或者使用server開頭的服務(wù)器,理論上想添加多少時間服務(wù)器都可以
?# iburst表示的是首次同步的時候快速同步
?pool pool.ntp.org iburst
??
?# 根據(jù)實際時間計算出服務(wù)器增減時間的比率,然后記錄到一個文件中,在系統(tǒng)重啟后為系統(tǒng)做出最佳時間 補償調(diào)整。
?driftfile /var/lib/chrony/drift
??
?# 如果系統(tǒng)時鐘的偏移量大于1秒,則允許系統(tǒng)時鐘在前三次更新中步進。
?# Allow the system clock to be stepped in the first three updates if its offset is larger than 1 second.
?makestep 1.0 3
??
?# 啟用實時時鐘(RTC)的內(nèi)核同步。
?# Enable kernel synchronization of the real-time clock (RTC).
?rtcsync
??
?# 通過使用 hwtimestamp 指令啟用硬件時間戳
?# Enable hardware timestamping on all interfaces that support it.
?#hwtimestamp *
?# Increase the minimum number of selectable sources required to adjust the system clock.
?#minsources 2
??
?# 指定 NTP 客戶端地址,以允許或拒絕連接到扮演時鐘服務(wù)器的機器
?# Allow NTP client access from local network.
?#allow 192.168.48.0/24
??
?# Serve time even if not synchronized to a time source.
?# local stratum 10
??
?# 指定包含 NTP 身份驗證密鑰的文件。
?# Specify file containing keys for NTP authentication.
?# keyfile /etc/chrony.keys
??
?# 指定日志文件的目錄。
?# Specify directory for log files.
?logdir /var/log/chrony
??
?# 選擇日志文件要記錄的信息。
?# Select which information is logged.
?# log measurements statistics tracking
同步時間服務(wù)器
授時中心
?210.72.145.44 國家授時中心 ?ntp.aliyun.com 阿里云 ?s1a.time.edu.cn 北京郵電大學(xué) ?s1b.time.edu.cn 清華大學(xué) ?s1c.time.edu.cn 北京大學(xué) ?s1d.time.edu.cn 東南大學(xué) ?s1e.time.edu.cn 清華大學(xué) ?s2a.time.edu.cn 清華大學(xué) ?s2b.time.edu.cn 清華大學(xué) ?s2c.time.edu.cn 北京郵電大學(xué) ?s2d.time.edu.cn 西南地區(qū)網(wǎng)絡(luò)中心 ?s2e.time.edu.cn 西北地區(qū)網(wǎng)絡(luò)中心 ?s2f.time.edu.cn 東北地區(qū)網(wǎng)絡(luò)中心 ?s2g.time.edu.cn 華東南地區(qū)網(wǎng)絡(luò)中心 ?s2h.time.edu.cn 四川大學(xué)網(wǎng)絡(luò)管理中心 ?s2j.time.edu.cn 大連理工大學(xué)網(wǎng)絡(luò)中心 ?s2k.time.edu.cn CERNET桂林主節(jié)點 ?s2m.time.edu.cn 北京大學(xué) ?ntp.sjtu.edu.cn 202.120.2.101 上海交通大學(xué)
實驗1
-
同步時間
-
第一步:先修改成錯誤時間
[root@server ~]# date -s 10:30
2023年 05月 30日 星期二 10:30:00 CST
-
第二步:編制chrony的配置文件
[root@server ~]# vim /etc/chrony.conf
# 定位第3行,刪除后添加阿里的時間同步服務(wù)地址
server ntp.aliyun.com iburst
# 注意:也可以先清空chrony.conf內(nèi)容,將阿里開源提供的時間服務(wù)器推薦配置復(fù)制粘貼到該文件中
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
-
第三步:重啟服務(wù)
[root@server ~]# systemctl restart chronyd
-
第四步:時間同步
[root@server ~]# chronyc sources -v
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current best, '+' = combined, '-' = not combined,
| / 'x' = may be in error, '~' = too variable, '?' = unusable.
|| .- 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
==========================================================================
^* 203.107.6.88 2 6 17 56 +493us[ -335us] +/- 34ms
第五步:查看時間是否同步
[root@server ~]# timedatectl status
Local time: 二 2023-05-30 10:24:39 CST
Universal time: 二 2023-05-30 02:24:39 UTC
RTC time: 二 2023-05-30 02:24:40
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes # yes 表名已同步
NTP service: active
RTC in local TZ: no
[root@server ~]# date
2023年 05月 30日 星期二 10:26:14 CST
實驗2
-
搭建本地時間同步服務(wù)器
-
架構(gòu)
性質(zhì) | IP地址 | 同步對象 |
---|---|---|
服務(wù)端server | 192.168.48.130 | ntp.aliyun.com |
客戶端node1 | 192.168.48.131 | 192.168.48.130 |
-
要求
-
服務(wù)端server向阿里時間服務(wù)器進行時間同步
-
客戶端node1向服務(wù)端server進行時間同步
-
-
第一步:定位服務(wù)端server
# 安裝軟件
[root@server ~]# yum install chrony -y # 默認已安裝
# 編輯配置文件,定位第3行,修改為阿里的時間服務(wù)地址
[root@server ~]# vim /etc/chrony.conf
server ntp.aliyun.com iburst
# 重啟服務(wù)
[root@server ~]# systemctl restart chronyd
# 測試
[root@server ~]# chronyc sources -v
[root@server ~]# timedatectl status
# 設(shè)置允許客戶端時間同步
[root@server ~]# vim /etc/chrony.conf
26 allow 192.168.48.131/24 # 定位第26行,設(shè)置誰可以訪問本機進行同步
[root@server ~]# systemctl restart chronyd
-
第二步:定位客戶端node1
# 安裝軟件
[root@node1 ~]# yum install chrony -y
# 編輯配置文件
[root@node1 ~]# vim /etc/chrony.conf # 修改第3行為server的地址
server 192.168.48.130 iburst
# 重啟服務(wù)
[root@node1 ~]# systemctl restart chronyd
# 測試
[root@node1 ~]# chronyc sources -v
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current best, '+' = combined, '-' = not combined,
| / 'x' = may be in error, '~' = too variable, '?' = unusable.
|| .- 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
=========================================================================
^* 192.168.48.130 3 6 17 39 +20us[ +252us] +/- 38ms
[root@node1 ~]# timedatectl status
Local time: 二 2023-05-30 11:08:37 CST
Universal time: 二 2023-05-30 03:08:37 UTC
RTC time: 二 2023-05-30 03:08:38
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
-
注意:客戶端同步失敗的原因
-
檢查網(wǎng)絡(luò)連通性,需要能ping通
-
檢查服務(wù)端的allow參數(shù)
-
需要重啟服務(wù)
-
chronyc命令
查看時間服務(wù)器:
[root@server ~]# chronyc sources -v # -v參數(shù)表示顯示內(nèi)容是否有解釋
.-- Source mode '^' = server, '=' = peer, '#' = local clock.
/ .- Source state '*' = current best, '+' = combined, '-' = not combined,
| / 'x' = may be in error, '~' = too variable, '?' = unusable.
|| .- 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
======================================================================
chronyc sources輸出分析
-
M:這表示信號源的模式。^表示服務(wù)器,=表示對等方,#表示本地連接的參考時鐘。
-
S:此列指示源的狀態(tài)
* | chronyd當前同步到的源 |
---|---|
+ | 表示可接受的信號源,與選定的信號源組合在一起 |
- | 表示被合并算法排除的可接受源 |
? | 表示已失去連接的源 |
x | 表示chronyd認為是虛假行情的時鐘(即,其時間與大多數(shù)其他來源不一致) |
~ | 表示時間似乎具有太多可變性的來源 |
-
Name/IP address:顯示服務(wù)器源的名稱或IP地址
-
Stratum:表示源的層級,層級1表示本地連接的參考時鐘,第2層表示通過第1層級計算機的時鐘實現(xiàn)同步,依此類推
-
Poll:表示源輪詢頻率,以秒為單位,值是基數(shù)2的對數(shù),例如值6表示每64秒進行一次測量,chronyd會根據(jù)當時的情況自動改變輪詢頻率
-
Reach:表示源的可達性的鎖存值(八進制數(shù)值),該鎖存值有8位,并在當接收或丟失一次時進行一次更新,值377表示最后八次傳輸都收到了有效的回復(fù)
-
LastRx:表示從源收到最近的一次的時間,通常是幾秒鐘,字母m,h,d或y分別表示分鐘,小時,天或年
-
Last sample:表示本地時鐘與上次測量時源的偏移量,方括號左側(cè)的數(shù)字表示原始測量值,方括號右側(cè)表示偏差值,+/-指示器后面的數(shù)字表示測量中的誤差范圍。正偏移表示本地時鐘位于源時鐘之前
其它命令
-
查看時間服務(wù)器的狀態(tài)
[root@server ~]# chronyc sourcestats -v
查看時間服務(wù)器是否在線
[root@server ~]# chronyc activity -v
同步系統(tǒng)時鐘
[root@server ~]# chronyc -a makestep
常見時區(qū)
-
UTC 整個地球分為二十四時區(qū),每個時區(qū)都有自己的本地時間。在國際無線電通信場合,為了統(tǒng)一起見,使用一個統(tǒng)一的時間,稱為通用協(xié)調(diào)時(UTC, Universal Time Coordinated)。
-
GMT 格林威治標準時間 (Greenwich Mean Time)指位于英國倫敦郊區(qū)的格林尼治天文臺的標準時間,因為本初子午線被定義在通過那里的經(jīng)線。(UTC與GMT時間基本相同,本文中不做區(qū)分)
-
CST 中國標準時間 (China Standard Time)GMT + 8 = UTC + 8 = CST文章來源:http://www.zghlxwxcb.cn/news/detail-854293.html
-
DST夏令時(Daylight Saving Time) 指在夏天太陽升起的比較早時,將時間撥快一小時,以提早日光的使用。(中國不使用)文章來源地址http://www.zghlxwxcb.cn/news/detail-854293.html
到了這里,關(guān)于RHCE-2-chrony服務(wù)器的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!