# Ubuntu 時間設定 ## apt-get proxy 問題 ### 單次繞過 Proxy 如果使用 apt-get 安裝、更新套件出現 Proxy 問題時,可以使用 `-o Acquire::http::proxy=false` 參數來單次性的繞過 Proxy * ref: [apt-get 暫時忽略 proxy](https://askubuntu.com/questions/344802/why-is-apt-get-always-using-proxy-although-no-proxy-is-configured ) ```bash 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 目前的校時工具與狀態 - 查看狀態 ```bash systemctl status systemd-timesyncd systemctl status ntp systemctl status chronyd ``` - 檢查哪個校時服務正在運行 ```bash systemctl | grep systemd-timesyncd systemctl | grep ntp systemctl | grep chronyd ``` ## 手動校時 :::warning 手動校時時,記得關閉 ntp / chrony 等套件的自動校時,不然校時命令會被拒絕。 ::: 1. 安裝必要套件 ```bash sudo apt-get install ntpdate -y ``` 2. 執行強制校時 ```bash sudo ntpdate time.stdtime.gov.tw ``` 3. 其它命令 - 檢查與校時伺服器的時間差 ```bash sudo ntpdate -q time.stdtime.gov.tw ``` - 手動設定時間 [參考資料](https://blog.gtwang.org/linux/howto-set-date-time-from-linux-command-prompt/) ```bash 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 #### 設定 - 手動調整設定檔 ```bash vi /etc/systemd/timesyncd.conf ``` ```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 ```bash sudo systemctl restart systemd-timesyncd ``` - 自動設定語法 設定語法這邊提供兩種做法,一個是修改現存的 timesyncd.conf 檔案,另外一個是直接覆蓋新的設定,則一使用即可 - 修改原始設定 ```bash 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 ``` - 複寫設定檔 ```bash 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 ``` - 原始設定檔備份 ```config # 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 ``` #### 其它命令 - 檢查狀態 ```bash sudo timedatectl ``` - 啟用自動校時 > 這邊命令會先關閉自動校時再重新啟動 ```bash sudo timedatectl set-ntp off sudo timedatectl set-ntp on ``` - 狀態檢查用命令 ```bash sudo timedatectl show-timesync sudo timedatectl timesync-status ``` ### ntp #### 安裝所需套件 - 安裝檢查 > 核心的同步服務是 ntp,所以裝 ntp 其實就夠了 ```bash #dpkg -l 可以列出已安裝套件 #dpkg -l | grep ntpdate dpkg -l | grep ntp ``` - 安裝語法 ```bash #sudo apt-get install ntpdate sudo apt-get install ntp ``` > 在 ubuntu 中,用 apt / apt-get 安裝套件時會預設啟用,不用特別另外執行 `systemctl start` 命令,安裝的服務就會自行啟動了,如果要另外在開機時啟動,仍建議使用 `systemctl enable <package-name>` 命令來明確指定啟用 #### 設定檔加入台灣的校時伺服器 - 編輯設定檔 (手動版) ```bash sudo vi /etc/ntp.conf ``` 把 server 區塊的設定調整一下,加入台灣的校時伺服器 - [校時伺服器參考](https://www.stdtime.gov.tw/chinese/bulletin/NTP%20promo.txt) - [這邊有另外一組可以嘗試](https://www.pool.ntp.org/zone/tw) - [其他參考](https://vector.cool/ntp-server-taiwan/) - [校時伺服器群 (pool)](https://www.cjkuo.net/public_ntp-server/) ```conf # 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 ``` - 編輯設定檔 (自動版) ```bash 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 ``` 確認設定檔中有沒有台灣的校時伺服器 ```bash sudo cat /etc/ntp.conf | grep stdtime.gov.tw ``` - 改完後重啟服務 ```bash sudo systemctl daemon-reload sudo systemctl restart ntp ``` - 確認服務狀態 ntp 在安裝後預設就會被啟用,因此可以用以下網路工具確認是否已開始監聽 UDP Port 123 ```bash netstat -tlunp # 如果沒有安裝網路工具,可以用以下命令檢查與安裝 dpkg -l | grep net-tools sudo apt-get install net-tools ``` - 其他參考命令 ```bash # 查看目前使用的校時伺服器狀態 sudo ntpq -p # 啟動 sudo /etc/init.d/ntp start # 重開 sudo /etc/init.d/ntp restart # 停止 sudo /etc/init.d/ntp stop ``` ### chrony - 安裝套件 ```bash sudo apt install chrony ``` - 編輯設定檔 (手動版) ```bash sudo vi /etc/chrony/chrony.conf ``` 把 server 區塊的設定調整一下,加入台灣的校時伺服器 ```conf # 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 ``` - 編輯設定檔 (自動版) ```bash 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 ``` - 其他參考 1. https://dywang.csie.cyut.edu.tw/dywang/rhel7/node70.html ### 參考 - [參考 1](https://nknuahuang.wordpress.com/tutorials/%E6%9C%89%E9%97%9Cubuntu%E8%88%87wordpress%E6%95%99%E5%AD%B8/ubuntu%E6%8E%92%E7%A8%8B%E6%A0%A1%E6%99%82%E8%A8%AD%E5%AE%9A/) - [參考 2](https://blog.gtwang.org/linux/linux-ntp-installation-and-configuration-tutorial/) - [參考 3](http://simple-is-beauty.blogspot.com/2017/04/ubuntu-ntp.html) - [參考 4 (timesync 設定檔)](https://unix.stackexchange.com/questions/655488/synchronizing-ntp-machines-with-a-high-root-time-server) - [參考 5 (timesync 設定檔2)](https://officeguide.cc/ubuntu-linux-timedatectl-time-synchronization-tutorial/)
×
Sign in
Email
Password
Forgot password
or
By clicking below, you agree to our
terms of service
.
Sign in via Facebook
Sign in via Twitter
Sign in via GitHub
Sign in via Dropbox
Sign in with Wallet
Wallet (
)
Connect another wallet
New to HackMD?
Sign up