UBUNTU時間同步
從UBUNTU16.04開始,就開始使用timedatectl/timesyncd服務進行時間同步,而不是常用的ntpdate/ntp。如果安裝了chrony,那麼timedatectl將會讓位於chrony,避免兩個同步服務出現競爭,這也同樣適用於ntpd。
ntpdate已被timedatectl或chrony拋棄,因此預設不會被安裝。timesyncd用於時間同步,chrony則用於更復雜的情況。
臨時一次同步:chronyd −q
臨時一次時間檢查,不設定時間:chronyd −Q
1.配置timedatectl和timesyncd
1.1.檢視timesyncd的狀態
檢視當前時間狀態及timedatectl/timesyncd的配置資訊:timedatectl status
Local time: 五 2021-10-22 14:00:35 CST
Universal time: 五 2021-10-22 06:00:35 UTC
RTC time: 五 2021-10-22 06:00:35
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active
RTC in local TZ: no
檢視timesyncd的執行情況:
systemctl status systemd-timesyncd
1.2.配置同步服務和選項
配置檔案可以出現在以下位置:
/etc/systemd/timesyncd.conf
/etc/systemd/timesyncd.conf.d/*.conf
/run/systemd/timesyncd.conf.d/*.conf
/usr/lib/systemd/timesyncd.conf.d/*.conf
配置檔案內容說明(man timesyncd.conf):
NTP=
用空格分隔的NTP伺服器主機名或IP地址列表。systemd-timesyncd將依次連線配置好NTP服務列表,直到找到一個有效的,預設為空
FallbackNTP=
用空格分隔的NTP伺服器主機名或IP地址列表,用作備用NTP伺服器。從systemd-network.service獲得的NTP伺服器優先於與通過NTP=上面設定的伺服器
RootDistanceMaxSec=
最大可接收時鐘偏差,預設為5秒。
PollIntervalMinSec=, PollIntervalMaxSec=
NTP訊息的最小和最大輪詢間隔(單位秒)。PollIntervalMinSec不能小於16秒,PollIntervalMaxSec必須大於PollIntervalMinSec,PollIntervalMinSec預設為32秒,PollIntervalMaxSec預設為2048秒
1.3.一些公用的NTP伺服器
使用以前請先ping相應的域名檢視網路是否可達,和相應的訪問速度
1.3.1.中國NTP時間源伺服器
ntp.ntsc.ac.cn 中國國家授時中心
cn.ntp.org.cn 中國授時
1.3.2.阿里雲提供的NTP服務
ntp1.aliyun.com
ntp2.aliyun.com
ntp3.aliyun.com
ntp4.aliyun.com
ntp5.aliyun.com
ntp6.aliyun.com
ntp7.aliyun.com
1.3.3.大學提供的NTP服務(國內)
s1a.time.edu.cn 北京郵電大學
s1b.time.edu.cn 清華大學
s1c.time.edu.cn 北京大學
s1d.time.edu.cn 東南大學
s1e.time.edu.cn 清華大學
s2a.time.edu.cn 清華大學
s2b.time.edu.cn 清華大學
s2c.time.edu.cn 北京郵電大學
s2d.time.edu.cn 西南地區網路中心
s2e.time.edu.cn 西北地區網路中心
s2f.time.edu.cn 東北地區網路中心
s2g.time.edu.cn 華東南地區網路中心
s2h.time.edu.cn 四川大學網路管理中心
s2j.time.edu.cn 大連理工大學網路中心
s2k.time.edu.cn CERNET桂林主節點
s2m.time.edu.cn 北京大學
1.3.4.蘋果提供的NTP服務(國外)
http://time1.apple.com
http://time2.apple.com
http://time3.apple.com
http://time4.apple.com
http://time5.apple.com
http://time6.apple.com
http://time7.apple.com
1.3.5.谷歌提供的NTP服務(國外)
http://time1.google.com
http://time2.google.com
http://time3.google.com
http://time4.google.com
另外推薦一個官方的站點:
2.時鐘伺服器
2.1.安裝服務
apt install chrony
它將安裝兩個bin檔案:
chronyd-同步守護程序,並提供NTP服務
chronyc- chrony守護程序的命令列介面
2.2.配置服務
配置檔案為:/etc/chrony/chrony.conf(man chrony.conf)。預設配置了以下服務:
pool 0.ubuntu.pool.ntp.org iburst
pool 1.ubuntu.pool.ntp.org iburst
pool 2.ubuntu.pool.ntp.org iburst
pool 3.ubuntu.pool.ntp.org iburst

