[精讚] [會員登入]
5425

[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

你可能感興趣的文章

[Centos7] 使用網路安裝 Centos7 使用網路來安裝Centos7,雖然這不是什麼新鮮事,但可以省你的磁碟空間或光碟片

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

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

[LINUX] 利用 usermod 修改使用者的參數和資料 使用指令 usermod 修改使用者的參數和資料

[Rocky9] codeignitor4+ nginx+ php8.1-fpm + docker compose 這次的目標是把服務裝在docker,裡面放入 nginx 和 php8.1-fpm的 docker container, 並採用codeignitor4。

[Centos6 7] 掛載磁碟 只是掛載新磁碟

我有話要說

>>

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

訪客留言

[無留言]

隨機好文

[MAC] 截取螢幕畫面的方法 截取螢幕畫面的方法,在MAC中叫作螢幕快照,英文是screenshot

為什麼要重造輪子? 什麼輪子?造什麼輪子?我為什麼要重造輪子?

維修冰箱 維修冰箱

Smarty安裝 smarty 是著名的樣版引擎,非常的好用,用多了突然發現拿掉smarty反而不會寫php了,以下是安裝過程..

[PHP] 檢查IP是否在某個網段內 mtachcidr 要檢查IP是否在某個網段內,要寫幾行?10行?5行? 不用,只要2行。以下是我寫的 code /** * matchCI