[Centos8] 校時ntpdate?

URL Link //n.sfs.tw/14899

2021-01-04 12:31:39 By 張○○

以前常用的ntpdate,在centos8竟然不見了?

# ntpdate
bash: ntpdate: command not found...
# dnf install ntpdate
Last metadata expiration check: 2:25:48 ago on Mon 04 Jan 2021 09:59:56 AM CST.
No match for argument: ntpdate
Error: Unable to find a match: ntpdate

找不到,那要怎麼校時?這可是個大麻煩...

到了centos8,ntpd service被chronyc.servic取代(注意chronyc後面有一個c)、ntpq指令被chronyc取代;而顯示設定時間、時區、ntpdate等功能,被timedatectl取代。

顯示及修改時區

查看時間

# date
Sun Jan  3 22:36:22 EST 2021

顯示時間相關設定
# timedatectl

# timedatectl
               Local time: Mon 2021-01-04 15:12:51 CST
           Universal time: Mon 2021-01-04 07:12:51 UTC
                 RTC time: Mon 2021-01-04 07:12:51
                Time zone: Asia/Taipei (CST, +0800)
System clock synchronized: yes
              NTP service: active 
          RTC in local TZ: no

顯示目前時區及部分參數

# timedatectl show

# timedatectl show
Timezone=Asia/Taipei
LocalRTC=no
CanNTP=yes
NTP=yes <== 啟動自動校時
NTPSynchronized=yes <== 已經自動校時完畢
TimeUSec=Mon 2021-01-04 15:12:59 CST
RTCTimeUSec=Mon 2021-01-04 15:12:59 CST

列出可用時區

# timedatectl list-timezones

設定時區

# timedatectl set-timezone Asia/Taipei

設定ntp 校時關閉/啟動

這個動作會關閉/啟動 chronyd.service 這個服務

# timedatectl set-ntp no

# timedatectl set-ntp yes

手動設定時間

# timedatectl set-time "2021-03-01 18:17:16"

如果有指定ntp yes的話會出錯,請將他關閉即可
Failed to set time: NTP unit is active
# timedatectl set-ntp no

指定自己的校時服務 Chronyc

上面說明使用自動校時的功能,可是以前有指定自己的校時server,怎麼現在都不用設定?

原來timedatectl的校時是透過 chronyc.service 來執行的,這個程式已內建安裝。

只要關掉chronyc.service的話,timedatectl的ntp就會自動關閉。

# systemctl stop chronyd.service
# timedatectl show
...
NTP=no <== 自動關閉了
...

開啟關閉校時服務

# systemctl stop chronyd.service
# systemctl start chronyd.service

# systemctl status chronyd.service

# service chronyd start
# service chronyd stop
# service chronyd restart

手動自動校時

# chronyc makestep

查看校時目的 server

# chronyc sources
210 Number of sources = 4
MS Name/IP address         Stratum Poll Reach LastRx Last sample               
===============================================================================
^? t1.time.tw1.yahoo.com         2   6     3     2   -354us[ -354us] +/-   11ms
^* t2.time.tw1.yahoo.com         2   6     7     1    -29us[ -449us] +/- 8976us
^+ time.cloudflare.com           4   6     7     1  +7036us[+6615us] +/-   74ms
^- lon-gb-1.cloud.coffeebit>     2   6     7     0    -15ms[  -15ms] +/-  127ms

MS欄目中 ^代表server,後面接的'*'目前使用中,'+' 侯補使用中, '?'代表連線狀態不明,'-'侯補但被演算法排除[4][5]

This column indicates the state of the sources. "*" indicates the source to which chronyd is currently synchronized. "+" indicates acceptable sources which are combined with the selected source. "-" indicates acceptable sources which are excluded by the combining algorithm. "?" indicates sources to which connectivity has been lost or whose packets do not pass all tests. "x" indicates a clock which chronyd thinks is a falseticker (its time is inconsistent with a majority of other sources). "~" indicates a source whose time appears to have too much variability. The "?" condition is also shown at start-up, until at least 3 samples have been gathered from it.

查看校時的詳情

# chronyc tracking
Reference ID    : 7AAA8222 (t2.time.tw1.yahoo.com)
Stratum         : 3
Ref time (UTC)  : Tue Mar 02 03:33:32 2021
System time     : 0.000036383 seconds slow of NTP time
Last offset     : -0.000037010 seconds
RMS offset      : 0.000037010 seconds
Frequency       : 25.924 ppm slow
Residual freq   : +0.055 ppm
Skew            : 2.008 ppm
Root delay      : 0.003752059 seconds
Root dispersion : 0.007501955 seconds
Update interval : 63.9 seconds
Leap status     : Normal

其它指令

# chronyc sourcestats

# chronyc ntpdata

crontab?

以前會有類似這樣的設定
0 5 * * * /usr/sbin/ntpdate time.stdtime.gov.tw

已不再需要,因為chronyd 會自動校時

指定校時伺服器

修改 /etc/chrony.conf

#pool 2.centos.pool.ntp.org iburst
# 設定指定伺服器
pool time.stdtime.gov.tw iburst

先測試確定ok[6]

# chronyd -q 'server time.stdtime.gov.tw iburst'

查看log

修改 /etc/chrony.conf

logdir /var/log/chrony
log measurements statistics tracking

log 會在 /var/log/chrony/ 中

 

結論

什麼都不想做的話,只要確定 chronyd 有啟動即可, 剩下都不必動,系統會自動校時的,預設就會。

參考資料

[1] https://qiita.com/thzking/items/c2bcf7d60963b4dab7b6

[2] https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/html/system_administrators_guide/chap-configuring_the_date_and_time

[3]  https://www.freedesktop.org/software/systemd/man/systemd-timedated.service.html

[4] Chronyc https://qiita.com/legitwhiz/items/5b2d56ce9a0ee29a24f9

[5] https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/8/html/configuring_basic_system_settings/using-chrony-to-configure-ntp

[6] https://www.linuxtechi.com/sync-time-in-linux-server-using-chrony/