Ubuntu系統默認的時鍾同步服務器是ntp.ubuntu.com,Debian則是0.debian.pool.ntp.org等, 各Linux發行版都有自己的NTP官方服務器,在某些環境下,比如公司內網、雲上子網等,是與互聯網隔絕的。 這時要想做時鍾同步,就只能自己配置了。
安裝
一般timesync是預裝的,如果沒有,可以使用以下命令手動安裝
sudo apt install systemd-timesyncd
它和 NTP 是沖突的,二者只能安裝一個
更改時區
查看當前系統時區
timedatectl
列出所有可用時區
timedatectl list-timezones
將系統時區更為 Asia/Shanghai,運行以下命令
sudo timedatectl set-timezone Asia/Shanghai
再次執行 timedatectl 命令查看時區
配置
修改 /etc/systemd/timesyncd.conf,把NTP設為國內的或者自建的NTP服務器
[Time]
NTP=ntp7.aliyun.com
NTP=ntp6.aliyun.com
修改完成后,需要restart后這個配置才生效
sudo systemctl restart systemd-timesyncd.service
如果以上systemd-timesyncd.service因為什么原因而不存在,則可通過以下命令修復:
sudo dpkg-reconfigure systemd-timesyncd
再次查看驗證
timedatectl
以下給出一些阿里雲的NTP列表,可以通過ping擇優使用
ntp1.aliyun.com
ntp2.aliyun.com
ntp3.aliyun.com
ntp4.aliyun.com
ntp5.aliyun.com
ntp6.aliyun.com
ntp7.aliyun.com
本文參考自:
(1)https://note.qidong.name/2020/09/timesyncd
(2)https://www.myfreax.com/how-to-set-or-change-timezone-on-debian-9