[精讚] [會員登入]
6106

[Centos8] 校時ntpdate?

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

分享此文連結 //n.sfs.tw/14899

分享連結 [Centos8] 校時ntpdate? @新精讚
(文章歡迎轉載,務必尊重版權註明連結來源)
2023-06-06 11:48:15 最後編修
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/

 

END

你可能感興趣的文章

[bc] linux 的計算機 bc 設定小數位數、計算π、次方根 linux 的計算機 bc 設定小數位數、計算π、次方根

[Centos7 8] postfix+DKIM 設定 信件總是被退?希望別的mail server相信我寄出的信?那麼你可以設定dkim。

[CENTOS7] 使用 logrotate 來整理mongo日誌檔 解決 mongo 的日誌檔不斷長大的問題

[Centos7] 安裝phpMyAdmin Centos 7有新的方式可以安裝phpmyadmin,不必再採用過去的tarball安裝法。

[CENTOS7 8] 查看記憶體 linux 中查看記憶體狀態指令整理

[Rocky Linux] 安裝gitlab gitlab+docker compose+ nginx 安裝Gitlab並結合nginx當網頁伺服器

我有話要說

>>

限制:留言最高字數1000字。 限制:未登入訪客,每則留言間隔需超過10分鐘,每日最多5則留言。

訪客留言

[無留言]

隨機好文

[HP DL380G7] 生效啟動第3,4片網卡/開啟或關閉內建的網卡 HP DL380G7 預設第3,4片網卡裝完系統後找不到,難道是壞了?要怎麼辦?

UTF-8 BOM (Byte Order Mark) 的問題 在 Michael Kaplan 那看到 Every character has a story #4: U+feff

[Freebsd] 使用 ADSL 撥接上網 Freebsd上要使用 ADSL 撥接上網,該如何設定?

世紀帝國征服者新版本--被遺忘的帝國 世紀帝國征服者新版本--被遺忘的帝國 世紀二代的征服者是精典遊戲中的精典,aofe更好玩...

詭異的創業思維 創業的思維中,有多少銀彈,有多少技術,有多少人脈,有多少時間等等,每個都要考慮進去,以熱忱建立的關係脆弱的像蘇打餅乾一樣..