[精讚] [會員登入]
5434

[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

你可能感興趣的文章

[Linux] mongodb 初步--2/3 我使用mongo 的一些大小事:叢集

[Linux] mongodb 初步--1/3 我使用mongo 的一些大小事:安裝及基本操作

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

[CentOS] 設定和使用quota quota 的觀念已經很老了,不過每次都會忘記所以寫下來,有需要觀念的朋友請參看鳥哥的網站吧

[CentOS] 新增修改及刪除使用者 新增、修改、刪除使用者和群組

PHP for sphinx 函式庫安裝 PECL/sphinx PHP>= 5.2.2 已經能原生支援 sphinx,可是預設的沒有裝,我們得自己裝才能用

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

[Freebsd] 定時測試 ADSL 是否斷線並重連 中華電信 ADSL 雖有固定 ip,可是他卻會不定時「斷線」, 使用以下的 方法可以定時測試是否斷線,以及重新撥接。

[PHP] 檢查檔案是否是圖檔 使用getimagesize函數檢查檔案是否是圖檔

[Mysql/MariaDB] 查看資料庫所占空間 查看資料庫在磁碟中所占空間

抽油煙機濾網清洗方法 我發現的抽油煙機濾網清洗方法(限18+操作使用)

使用GPG檔案加解密 簡易的檔案加解密方式,可以讓你重要的資料包括文字檔加解密。