Linux中時間設(shè)置
在Linux系統(tǒng)中存在兩個時鐘時間勉失,分別是
- 硬件時鐘RTC(Real Time Clock)
- 系統(tǒng)時鐘(System Clock)
硬件時鐘是指的在主板上的時鐘設(shè)備碍沐,也就是通忱涂樱可以在BIOS畫面設(shè)置的時鐘场斑,即使關(guān)機(jī)狀態(tài)也可以計算時間。
而系統(tǒng)時鐘則是指Kernel中的時鐘惰匙,其值是由1970年1月1日00:00:00 UTC時間至當(dāng)前時間所經(jīng)歷的秒數(shù)總和砂吞。當(dāng)Linux啟動的時候,系統(tǒng)時鐘會讀取硬件時鐘的設(shè)定隔显,之后系統(tǒng)時鐘獨立運(yùn)作却妨。長時間運(yùn)行兩者可能將會產(chǎn)生誤差。另外所有的Linux相關(guān)指令都是讀取系統(tǒng)時鐘指定的括眠,如date彪标。
我們這里討論的是系統(tǒng)時間。
NTP
NTP掷豺,網(wǎng)絡(luò)時間協(xié)議捞烟,使用 123/udp 端口進(jìn)行網(wǎng)絡(luò)時鐘同步薄声;NTP 是仍在使用中的最古老的網(wǎng)絡(luò)傳輸協(xié)議之一(1985 年前開始)。
以前Linux時間同步基本是使用 ntpdate 和 ntpd 這兩個工具實現(xiàn)的坷襟,但是這兩個工具已經(jīng)很古老了奸柬。
【注】ntpdate和ntpd是互斥的,兩者不能同時使用婴程。ntpd是步進(jìn)式平滑的逐漸調(diào)整時間廓奕,而ntpdate是斷點式更新時間。
RHEL/CentOS 7.x 已經(jīng)將 chrony 作為默認(rèn)時間同步工具了档叔。
其他Linux (如 ubuntu) 使用 systemd-timesyncd 服務(wù)桌粉。
chrony 簡介
chrony 是 RedHat 開發(fā)的,它是網(wǎng)絡(luò)時間協(xié)議(NTP)的另一種實現(xiàn)衙四;
RHEL/CentOS 7.x 的默認(rèn)時間同步工具铃肯;
chrony 可以同時做為 ntp 服務(wù)的客戶端和服務(wù)端;安裝完后有兩個程序 chronyd传蹈、chronyc:
chronyd 是一個 daemon 守護(hù)進(jìn)程押逼,chronyc 是用來監(jiān)控 chronyd 性能和配置參數(shù)的命令行工具。
實驗環(huán)境
系統(tǒng)版本:CentOS 7.5
chrony_server(relay):10.0.0.4
chrony_client:10.0.0.5
chrony_server
配置 chrony
Edit file /etc/chrony.conf
## 上游公共 ntp 服務(wù)器
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
## 允許 10.0.0.0/24 主機(jī)同步時間,此時它作為server
# Allow NTP client access from local network.
allow 10.0.0.0/24
運(yùn)行 chrony
默認(rèn)已經(jīng)啟動惦界,不需要調(diào)整
# systemctl enable chronyd
# systemctl start chronyd
chronyc 用法
## 查看 ntp_servers 狀態(tài)
chronyc sources -v
## 查看 ntp_sync 狀態(tài)
chronyc sourcestats -v
## 查看 ntp_servers 是否在線
chronyc activity -v
## 查看 ntp 詳細(xì)信息
chronyc tracking -v
example:
[royzeng@roy-time1 ~]$ chronyc sources -v
210 Number of sources = 4
.-- 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
===============================================================================
^- 203.95.213.129 2 6 17 7 +6589us[+6589us] +/- 128ms
^- ntp.hkg10.hk.leaseweb.net 2 6 17 9 -3166us[-4471us] +/- 230ms
^* ntp.xtom.com.hk 2 6 17 9 +446us[ -859us] +/- 44ms
^+ stratum2-01.hkg01.public> 2 6 17 9 +1465us[+1465us] +/- 49ms
[royzeng@roy-time1 ~]$ chronyc sourcestats -v
210 Number of sources = 4
.- Number of sample points in measurement set.
/ .- Number of residual runs with same sign.
| / .- Length of measurement set (time).
| | / .- Est. clock freq error (ppm).
| | | / .- Est. error in freq.
| | | | / .- Est. offset.
| | | | | | On the -.
| | | | | | samples. \
| | | | | | |
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
==============================================================================
203.95.213.129 5 3 71 -2.215 56.211 +6464us 197us
ntp.hkg10.hk.leaseweb.net 5 5 71 -0.455 43.785 -3413us 249us
ntp.xtom.com.hk 5 3 71 -4.793 32.118 -192us 138us
stratum2-01.hkg01.public> 5 3 71 +7.164 102.122 +2232us 529us
[royzeng@roy-time1 ~]$ chronyc activity -v
200 OK
4 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address
[royzeng@roy-time1 ~]$ chronyc tracking -v
Reference ID : 2D7D0114 (ntp.xtom.com.hk)
Stratum : 3
Ref time (UTC) : Fri Dec 14 07:04:03 2018
System time : 0.000712532 seconds fast of NTP time
Last offset : -0.000283856 seconds
RMS offset : 0.001270661 seconds
Frequency : 1.802 ppm slow
Residual freq : -0.221 ppm
Skew : 4.037 ppm
Root delay : 0.004713390 seconds
Root dispersion : 0.042513479 seconds
Update interval : 65.2 seconds
Leap status : Normal
chrony_client
配置 chrony
edit file: /etc/chrony.conf
## 內(nèi)網(wǎng) ntp_server IP
server 10.0.0.4 iburst
再次用chronyc 命令檢查挑格,比較它與chronyd server的差異
[royzeng@roy-time2 ~]$ 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
===============================================================================
^* 10.0.0.4 3 6 17 15 +28us[ +88us] +/- 22ms
[royzeng@roy-time2 ~]$ chronyc sourcestats -v
210 Number of sources = 1
.- Number of sample points in measurement set.
/ .- Number of residual runs with same sign.
| / .- Length of measurement set (time).
| | / .- Est. clock freq error (ppm).
| | | / .- Est. error in freq.
| | | | / .- Est. offset.
| | | | | | On the -.
| | | | | | samples. \
| | | | | | |
Name/IP Address NP NR Span Frequency Freq Skew Offset Std Dev
==============================================================================
10.0.0.4 4 3 6 +3.254 829.521 +121us 101us
[royzeng@roy-time2 ~]$ chronyc activity -v
200 OK
1 sources online
0 sources offline
0 sources doing burst (return to online)
0 sources doing burst (return to offline)
0 sources with unknown address
[royzeng@roy-time2 ~]$ chronyc tracking -v
Reference ID : 0A000004 (10.0.0.4)
Stratum : 4
Ref time (UTC) : Fri Dec 14 07:10:35 2018
System time : 0.000086407 seconds slow of NTP time
Last offset : -0.000094787 seconds
RMS offset : 0.000094787 seconds
Frequency : 50.500 ppm slow
Residual freq : -1.147 ppm
Skew : 0.190 ppm
Root delay : 0.005246780 seconds
Root dispersion : 0.019316599 seconds
Update interval : 65.3 seconds
Leap status : Normal
systemd-timesyncd 簡介
systemd-timesyncd 是一個用于跨網(wǎng)絡(luò)同步系統(tǒng)時鐘的守護(hù)服務(wù)。它實現(xiàn)了一個 SNTP 客戶端沾歪,但更輕量級漂彤,更集成systemd。
配置
systemd-timesyncd 啟動時會讀取 /etc/systemd/timesyncd.conf 配置文件,內(nèi)容如下:
[Time]
#NTP=
#FallbackNTP=ntp.ubuntu.com
NTP=time1.myserver.com
FallbackNTP=time2.myserver.com
你可以輸入你希望使用的其它時間服務(wù)器灾搏,比如你自己的本地 NTP 服務(wù)器挫望,在 NTP= 行上輸入一個以空格分隔的服務(wù)器列表。
如果服務(wù)器可以直接連接internet狂窑,不用修改默認(rèn)配置媳板;如果在內(nèi)網(wǎng),需要單獨指定蕾域。
例子
在最新的 Ubuntu 版本中拷肌,timedatectl 替代了老舊的 ntpdate。默認(rèn)情況下旨巷,timedatectl 在系統(tǒng)啟動的時候會立刻同步時間巨缘,并在稍后網(wǎng)絡(luò)連接激活后通過 socket 再次檢查一次。
timesyncd 替代了 ntpd 的客戶端的部分采呐。默認(rèn)情況下 timesyncd 會定期檢測并同步時間若锁。它還會在本地存儲更新的時間,以便在系統(tǒng)重啟時做時間單步調(diào)整斧吐。
通過 timedatectl 和 timesyncd 設(shè)置的當(dāng)前時間狀態(tài)和時間配置又固,可以使用 timedatectl status 命令來進(jìn)行確認(rèn)仲器。
由于 timedatectl 的存在,各發(fā)行版已經(jīng)棄用了 ntpdate仰冠,默認(rèn)不再進(jìn)行安裝乏冀。
timedatectl
timedatectl status
,查看時間同步狀態(tài)洋只;
timedatectl set-ntp true
辆沦,開啟網(wǎng)絡(luò)時間同步;
$ timedatectl status
Local time: Fri 2018-12-14 02:35:15 UTC
Universal time: Fri 2018-12-14 02:35:15 UTC
RTC time: Fri 2018-12-14 02:35:15
Time zone: Etc/UTC (UTC, +0000)
Network time on: yes
NTP synchronized: yes
RTC in local TZ: no
timedatectl set-timezone ZONE
识虚,設(shè)置時區(qū)肢扯。
$ timedatectl list-timezones | grep -i shanghai
Asia/Shanghai
$ sudo timedatectl set-timezone Asia/Shanghai
$ date
Wed Feb 20 14:13:06 CST 2019
$ timedatectl status | grep Time
Time zone: Asia/Shanghai (CST, +0800)
NTP synchronized: yes
表示時間是同步狀態(tài)。
查看服務(wù)狀態(tài)以及從哪個ntp server同步時間担锤。
$ sudo systemctl status systemd-timesyncd
● systemd-timesyncd.service - Network Time Synchronization
Loaded: loaded (/lib/systemd/system/systemd-timesyncd.service; enabled; vendor preset: enabled)
Drop-In: /lib/systemd/system/systemd-timesyncd.service.d
└─disable-with-time-daemon.conf
Active: active (running) since Tue 2018-12-04 02:48:13 UTC; 1 weeks 3 days ago
Docs: man:systemd-timesyncd.service(8)
Main PID: 1167 (systemd-timesyn)
Status: "Synchronized to time server 91.189.89.199:123 (ntp.ubuntu.com)."
Tasks: 2
Memory: 1.3M
CPU: 736ms
CGroup: /system.slice/systemd-timesyncd.service
└─1167 /lib/systemd/systemd-timesyncd
Dec 04 02:48:13 roy-time-vm systemd[1]: Starting Network Time Synchronization...
Dec 04 02:48:13 roy-time-vm systemd[1]: Started Network Time Synchronization.
Dec 04 02:48:43 roy-time-vm systemd-timesyncd[1167]: Synchronized to time server 91.189.89.199:
lines 1-17/17 (END)
擴(kuò)展:同步時間協(xié)議
NTP:軟件層面實現(xiàn)蔚晨,成本低。同步精度10ms左右肛循。
PTP:需要網(wǎng)絡(luò)接口具備在物理層提供時間戳的功能铭腕,同步精度優(yōu)于100ns,局域網(wǎng)的節(jié)點需要使用支持PTP功能的交換機(jī)多糠。局域網(wǎng)網(wǎng)絡(luò)接點不支持PTP的話谨履,只能同不到us,而且受網(wǎng)絡(luò)背景流量影響熬丧。