Try   HackMD

Ubuntu 時間設定

apt-get proxy 問題

單次繞過 Proxy

如果使用 apt-get 安裝、更新套件出現 Proxy 問題時,可以使用 -o Acquire::http::proxy=false 參數來單次性的繞過 Proxy

sudo apt-get -o Acquire::http::proxy=false update
sudo apt-get -o Acquire::http::proxy=false install <package-name>

全域控制繞開 Proxy

若要完全避開 Proxy 的話,可以到 /etc/apt/apt.conf.d/ 這個目錄找 **proxy.conf 的檔案,並修改內容

Acquire::http::Proxy "false";
Acquire::https::Proxy "false";

檢查 OS 目前的校時工具與狀態

  • 查看狀態
systemctl status systemd-timesyncd
systemctl status ntp
systemctl status chronyd
  • 檢查哪個校時服務正在運行
systemctl | grep systemd-timesyncd
systemctl | grep ntp
systemctl | grep chronyd

手動校時

手動校時時,記得關閉 ntp / chrony 等套件的自動校時,不然校時命令會被拒絕。

  1. 安裝必要套件
sudo apt-get install ntpdate -y
  1. 執行強制校時
sudo ntpdate time.stdtime.gov.tw
  1. 其它命令
  • 檢查與校時伺服器的時間差
    ​​​​sudo ntpdate -q time.stdtime.gov.tw
    
  • 手動設定時間
    參考資料
    ​​​​sudo date -s "2023/1/17 09:10:00"
    

自動校時

自動校時有以下三種設定方式

  1. ubuntu 內建
  2. ntp
  3. chrony

Ubuntu 內建

REF: https://www.gushiciku.cn/pl/p7xS/zh-tw

設定

  • 手動調整設定檔
vi /etc/systemd/timesyncd.conf
[Time]
NTP=time.stdtime.gov.tw
FallbackNTP=clock.stdtime.gov.tw
RootDistanceMaxSec=60
PollIntervalMinSec=1800
PollIntervalMaxSec=86400

設定檔說明:

欄位 說明
NTP 主要校時伺服器
FallbackNTP 備用校時伺服器
RootDistanceMaxSec 與伺服器的距離(時間)差距最高可以為幾秒
PollIntervalMinSec 至少幾秒同步一次
PollIntervalMaxSec 至多幾秒同步一次
  • 重啟 systemd-timesyncd

這邊沒有 daemon-reload 也會讀到新的設定檔,所以自行決定要不要執行 daemon-reload

sudo systemctl restart systemd-timesyncd
  • 自動設定語法

    設定語法這邊提供兩種做法,一個是修改現存的 timesyncd.conf 檔案,另外一個是直接覆蓋新的設定,則一使用即可

    • 修改原始設定

      ​​​​​​​​sudo sed -i '/[Time]/NTP=time.stdtime.gov.tw\nFallbackNTP=clock.stdtime.gov.tw\nRootDistanceMaxSec=60\n' /etc/systemd/timesyncd.conf
      ​​​​​​​​sudo systemctl restart systemd-timesyncd
      
    • 複寫設定檔

      ​​​​​​​​sudo cat <<EOF | sudo tee /etc/systemd/timesyncd.conf
      ​​​​​​​​[Time]
      ​​​​​​​​NTP=time.stdtime.gov.tw
      ​​​​​​​​FallbackNTP=clock.stdtime.gov.tw
      ​​​​​​​​RootDistanceMaxSec=60
      ​​​​​​​​PollIntervalMinSec=1800
      ​​​​​​​​PollIntervalMaxSec=86400
      ​​​​​​​​EOF
      
      ​​​​​​​​sudo systemctl restart systemd-timesyncd
      
  • 原始設定檔備份

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See timesyncd.conf(5) for details.

[Time]
#NTP=
#FallbackNTP=ntp.ubuntu.com
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048

其它命令

  • 檢查狀態
sudo timedatectl
  • 啟用自動校時

這邊命令會先關閉自動校時再重新啟動

sudo timedatectl set-ntp off
sudo timedatectl set-ntp on
  • 狀態檢查用命令
sudo timedatectl show-timesync
sudo timedatectl timesync-status

ntp

安裝所需套件

  • 安裝檢查

核心的同步服務是 ntp,所以裝 ntp 其實就夠了

#dpkg -l 可以列出已安裝套件
#dpkg -l | grep ntpdate
dpkg -l | grep ntp
  • 安裝語法
#sudo apt-get install ntpdate
sudo apt-get install ntp

在 ubuntu 中,用 apt / apt-get 安裝套件時會預設啟用,不用特別另外執行 systemctl start 命令,安裝的服務就會自行啟動了,如果要另外在開機時啟動,仍建議使用 systemctl enable <package-name> 命令來明確指定啟用

設定檔加入台灣的校時伺服器

  • 編輯設定檔 (手動版)
sudo vi /etc/ntp.conf

把 server 區塊的設定調整一下,加入台灣的校時伺服器

# org linux time server
# 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

# taiwan time server pool
# pool tw.pool.ntp.org
# pool 0.tw.pool.ntp.org
# pool 1.tw.pool.ntp.org
# pool 2.tw.pool.ntp.org
# pool 3.tw.pool.ntp.org

server tick.stdtime.gov.tw prefer
server tock.stdtime.gov.tw prefer
server time.stdtime.gov.tw prefer
server clock.stdtime.gov.tw
server watch.stdtime.gov.tw

# Use Ubuntu's ntp server as a fallback.
# pool ntp.ubuntu.com
  • 編輯設定檔 (自動版)
sudo sed -i '/pool 0.ubuntu.pool.ntp.org iburst/ i# taiwan time server\nserver tock.stdtime.gov.tw prefer\nserver watch.stdtime.gov.tw prefer\nserver time.stdtime.gov.tw prefer\nserver clock.stdtime.gov.tw \nserver tick.stdtime.gov.tw \n' /etc/ntp.conf
sudo systemctl daemon-reload
sudo systemctl restart ntp

確認設定檔中有沒有台灣的校時伺服器

sudo cat /etc/ntp.conf | grep stdtime.gov.tw
  • 改完後重啟服務
sudo systemctl daemon-reload
sudo systemctl restart ntp
  • 確認服務狀態

ntp 在安裝後預設就會被啟用,因此可以用以下網路工具確認是否已開始監聽 UDP Port 123

netstat -tlunp

# 如果沒有安裝網路工具,可以用以下命令檢查與安裝
dpkg -l | grep net-tools
sudo apt-get install net-tools
  • 其他參考命令
# 查看目前使用的校時伺服器狀態
sudo ntpq -p
# 啟動
sudo /etc/init.d/ntp start
# 重開
sudo /etc/init.d/ntp restart
# 停止
sudo /etc/init.d/ntp stop

chrony

  • 安裝套件
sudo apt install chrony
  • 編輯設定檔 (手動版)
sudo vi /etc/chrony/chrony.conf

把 server 區塊的設定調整一下,加入台灣的校時伺服器

# See http://www.pool.ntp.org/join.html for more information.
pool ntp.ubuntu.com        iburst maxsources 4
pool 0.ubuntu.pool.ntp.org iburst maxsources 1
pool 1.ubuntu.pool.ntp.org iburst maxsources 1
pool 2.ubuntu.pool.ntp.org iburst maxsources 2

# taiwan time server
server time.stdtime.gov.tw prefer
server clock.stdtime.gov.tw

# This directive specify the location of the file containing ID/key pairs for
# NTP authentication.
keyfile /etc/chrony/chrony.keys
  • 編輯設定檔 (自動版)
sudo sed -i '/# This directive specify/ i# taiwan time server\nserver tock.stdtime.gov.tw prefer\nserver watch.stdtime.gov.tw prefer\nserver time.stdtime.gov.tw prefer\nserver clock.stdtime.gov.tw \nserver tick.stdtime.gov.tw \n' /etc/chrony/chrony.conf

  • 重啟 chrony
sudo systemctl daemon-reload
sudo systemctl restart chrony
  • 狀態檢查

REF: https://jfearn.fedorapeople.org/fdocs/en-US/Fedora_Draft_Documentation/0.1/html/System_Administrators_Guide/sect-Checking_if_chrony_is_synchronized.html

chronyc tracking
chronyc sources
  • 強制校時
chronyc -a makestep

參考