systemd-timesyncd
通过网络提供系统时钟同步的服务,通过实现 SNTP client
功能实现时钟同步。
介绍
systemd-timesyncd
与 ntp / Chrony 区别systemd-timesyncd
只实现 SNTP client
功能实现时钟同步,不能作为 NTP 服务器systemd-timesyncd
更精简和轻量级
使用
安装
sudo apt install systemd-timesyncd
配置文件
/etc/systemd/timesyncd.conf
[Time]
#NTP=
#FallbackNTP=ntp.ubuntu.com
#RootDistanceMaxSec=5
#PollIntervalMinSec=32
#PollIntervalMaxSec=2048
[Time]
NTP=ntp.aliyun.com ntp.ntsc.ac.cn cn.ntp.org.cn
FallbackNTP=ntp.ubuntu.com 0.pool.ntp.org 1.pool.ntp.org
启动服务
systemctl enable systemd-timesyncd.service
systemctl start systemd-timesyncd.service
systemctl status systemd-timesyncd.service
timedatectl 工具
help
$ timedatectl --help
timedatectl [OPTIONS...] COMMAND ...
Query or change system time and date settings.
Commands:
status Show current time settings
show Show properties of systemd-timedated
set-time TIME Set system time
set-timezone ZONE Set system time zone
list-timezones Show known time zones
set-local-rtc BOOL Control whether RTC is in local time
set-ntp BOOL Enable or disable network time synchronization
systemd-timesyncd Commands:
timesync-status Show status of systemd-timesyncd
show-timesync Show properties of systemd-timesyncd
Options:
-h --help Show this help message
--version Show package version
--no-pager Do not pipe output into a pager
--no-ask-password Do not prompt for password
-H --host=[USER@]HOST Operate on remote host
-M --machine=CONTAINER Operate on local container
--adjust-system-clock Adjust system clock when changing local RTC mode
--monitor Monitor status of systemd-timesyncd
-p --property=NAME Show only properties by this name
-a --all Show all properties, including empty ones
--value When showing properties, only print the value
See the timedatectl(1) man page for details.
示例
# 查看状态
$ timedatectl status
Local time: xxx
Universal time: xxx UTC
RTC time: xxx
Time zone: Asia/Shanghai (CST, +0800)
System clock synchronized: yes
NTP service: active # NTP 已启动
# 查看 systemd-timedated 属性
timedatectl show
# 激活 NTP
timedatectl set-ntp true
# 查看同步状态
timedatectl timesync-status
timedatectl show-timesync --all
扩展
Linux 系统中存在两个时钟:
RTC(Real Time Clock, 硬件时钟)
指主板上的时钟,BIOS 中可设置,独立电源,关机正常计算时间SC(System Clock, 系统时钟)
指 OS Kernel 的时钟,值是 1970-01-01 00:00:00+000
UTC 时间到当前时间的秒数总和- 系统启动时,从硬件获取时间,然后独立计时;系统时钟和硬件时钟会存在差异
F&Q
Unit file /etc/systemd/system/systemd-timesyncd.service is masked
$ systemctl enable systemd-timesyncd.service
Failed to enable unit: Unit file /etc/systemd/system/systemd-timesyncd.service is masked.
sudo apt install systemd-timesyncd
Failed to query server: Failed to activate service ‘org.freedesktop.timedate1’: timed out (service_start_timeout=25000ms)
systemctl restart systemd-timedated