如果使用 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 的話,可以到 /etc/apt/apt.conf.d/ 這個目錄找 **proxy.conf 的檔案,並修改內容
Acquire::http::Proxy "false";
Acquire::https::Proxy "false";
systemctl status systemd-timesyncd
systemctl status ntp
systemctl status chronyd
systemctl | grep systemd-timesyncd
systemctl | grep ntp
systemctl | grep chronyd
手動校時時,記得關閉 ntp / chrony 等套件的自動校時,不然校時命令會被拒絕。
sudo apt-get install ntpdate -y
sudo ntpdate time.stdtime.gov.tw
sudo ntpdate -q time.stdtime.gov.tw
sudo date -s "2023/1/17 09:10:00"
自動校時有以下三種設定方式
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 | 至多幾秒同步一次 |
這邊沒有 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 其實就夠了
#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
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
sudo systemctl daemon-reload
sudo systemctl restart chrony
chronyc tracking
chronyc sources
chronyc -a makestep